diff --git a/cmd/root.go b/cmd/root.go index 219c3d9..2aa2806 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -17,13 +17,19 @@ package cmd import ( "flag" "fmt" + "io" + "io/ioutil" + "net/http" + "net/url" "os" + "strings" nova_helm "github.com/fairwindsops/nova/pkg/helm" "github.com/fairwindsops/nova/pkg/output" "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/spf13/viper" + "k8s.io/apimachinery/pkg/api/errors" "k8s.io/klog" ) @@ -84,6 +90,16 @@ func initConfig() { return } + if strings.HasPrefix(cfgFile, "https://") || strings.HasPrefix(cfgFile, "http://") { + klog.V(2).Infof("detected URL for config location") + var err error + cfgFile, err = downloadConfig(cfgFile) + if err != nil { + klog.Fatalf("failed to download config: %s", err.Error()) + } + defer os.Remove(cfgFile) + } + // Read config viper.SetConfigFile(cfgFile) klog.V(2).Infof("using config file: %s", cfgFile) @@ -92,6 +108,43 @@ func initConfig() { } } +func downloadConfig(cfgURL string) (string, error) { + fileURL, err := url.Parse(cfgURL) + if err != nil { + return "", err + } + path := fileURL.Path + segments := strings.Split(path, "/") + fileName := segments[len(segments)-1] + + file, err := ioutil.TempFile("", fmt.Sprintf("*-%s", fileName)) + if err != nil { + return "", err + } + + client := http.Client{ + CheckRedirect: func(r *http.Request, via []*http.Request) error { + r.URL.Opaque = r.URL.Path + return nil + }, + } + // Put content on file + resp, err := client.Get(cfgURL) + if err != nil { + return "", &errors.StatusError{} + } + defer resp.Body.Close() + + size, err := io.Copy(file, resp.Body) + + defer file.Close() + + tmpConfig := file.Name() + klog.V(2).Infof("downloaded config file %s with size %d", tmpConfig, size) + + return tmpConfig, nil +} + var rootCmd = &cobra.Command{ Use: "nova", Short: "nova", diff --git a/go.mod b/go.mod index 81432a7..a29a5ff 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( // required for dependency issue: https://github.com/kubernetes/client-go/issues/628 github.com/Azure/go-autorest v12.2.0+incompatible github.com/DATA-DOG/go-sqlmock v1.4.1 // indirect + github.com/cavaliercoder/grab v2.0.0+incompatible github.com/fsnotify/fsnotify v1.4.9 // indirect github.com/jmoiron/sqlx v1.2.0 // indirect github.com/lib/pq v1.5.2 // indirect @@ -28,6 +29,7 @@ require ( helm.sh/helm v2.16.6+incompatible helm.sh/helm/v3 v3.1.2 k8s.io/apiextensions-apiserver v0.18.1 // indirect + k8s.io/apimachinery v0.18.1 k8s.io/client-go v1.5.1 k8s.io/helm v2.16.5+incompatible k8s.io/klog v1.0.0 diff --git a/go.sum b/go.sum index 367a8d6..898f645 100644 --- a/go.sum +++ b/go.sum @@ -90,6 +90,9 @@ github.com/bugsnag/bugsnag-go v0.0.0-20141110184014-b1d153021fcd/go.mod h1:2oa8n github.com/bugsnag/osext v0.0.0-20130617224835-0dd3f918b21b/go.mod h1:obH5gd0BsqsP2LwDJ9aOkm/6J86V6lyAXCoQWGw3K50= github.com/bugsnag/panicwrap v0.0.0-20151223152923-e2c28503fcd0/go.mod h1:D/8v3kj0zr8ZAKg1AQ6crr+5VwKN5eIywRkfhyM/+dE= github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ= +github.com/cavaliercoder/grab v1.0.0 h1:H6VQ1NiLO7AvXM6ZyaInnoZrRLeo2FoUTQEcXln4bvQ= +github.com/cavaliercoder/grab v2.0.0+incompatible h1:wZHbBQx56+Yxjx2TCGDcenhh3cJn7cCLMfkEPmySTSE= +github.com/cavaliercoder/grab v2.0.0+incompatible/go.mod h1:tTBkfNqSBfuMmMBFaO2phgyhdYhiZQ/+iXCZDzcDsMI= github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=