From a84fa5b520bd37cac5e13f43998872a62c64473e Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Sun, 2 Aug 2020 01:38:45 +0300 Subject: [PATCH] add a --proxy flag to resolve #3 --- cmd/cmd.go | 31 ++++++++++++++++++++++++++++++- cmd/init.go | 1 + registry.yml | 1 + utils/http.go | 12 +++--------- 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index f49f21c..e099418 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -1,13 +1,20 @@ package cmd import ( + "crypto/tls" "encoding/json" "io/ioutil" + "net/http" + "net/url" "os" + "github.com/kataras/iris-cli/utils" + "github.com/spf13/cobra" ) +var proxyAddr string + // New returns the root command. func New(buildRevision, buildTime string) *cobra.Command { rootCmd := &cobra.Command{ @@ -20,7 +27,28 @@ Complete documentation is available at https://github.com/kataras/iris-cli`, SilenceUsage: true, TraverseChildren: true, SuggestionsMinimumDistance: 1, - Run: func(cmd *cobra.Command, args []string) { + Run: func(cmd *cobra.Command, args []string) {}, + PersistentPreRun: func(cmd *cobra.Command, args []string) { + transport := &http.Transport{ + DisableCompression: true, + DisableKeepAlives: true, + TLSClientConfig: &tls.Config{ + InsecureSkipVerify: utils.IsInsideDocker(), + }, + } + + if proxyAddr != "" { + if proxyAddr == "env" { + transport.Proxy = http.ProxyFromEnvironment + } else { + u := &url.URL{Scheme: "http", Host: proxyAddr} + transport.Proxy = func(req *http.Request) (*url.URL, error) { + return u, nil + } + } + } + + utils.DefaultClient.Transport = transport }, } @@ -30,6 +58,7 @@ Complete documentation is available at https://github.com/kataras/iris-cli`, ShowGoRuntimeVersion: true, } rootCmd.SetHelpTemplate(helpTemplate.String()) + rootCmd.PersistentFlags().StringVar(&proxyAddr, "proxy", proxyAddr, "--proxy=env to load from system or ip:port form, e.g. 51.158.178.4:3128") // Commands. rootCmd.AddCommand(initCommand()) diff --git a/cmd/init.go b/cmd/init.go index ae6a691..0a4d04c 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -14,6 +14,7 @@ import ( "github.com/spf13/cobra" "gopkg.in/src-d/go-git.v4" "gopkg.in/src-d/go-git.v4/plumbing" + // this does not work: "gopkg.in/src-d/go-git.v4/plumbing/format/gitignore" "gopkg.in/src-d/go-git.v4/plumbing/object" // we use that instead. diff --git a/registry.yml b/registry.yml index e113bac..65a363c 100644 --- a/registry.yml +++ b/registry.yml @@ -3,4 +3,5 @@ Projects: basic: "iris-contrib/basic-template", svelte: "iris-contrib/svelte-template", react-typescript: "iris-contrib/react-typescript-template", + go-admin: "iris-contrib/go-admin-template", } diff --git a/utils/http.go b/utils/http.go index 53c3417..ed510d5 100644 --- a/utils/http.go +++ b/utils/http.go @@ -2,7 +2,6 @@ package utils import ( "compress/gzip" - "crypto/tls" "encoding/json" "fmt" "io" @@ -26,13 +25,8 @@ func Download(url string, body io.Reader, options ...DownloadOption) ([]byte, er return ioutil.ReadAll(r) } -var httpClient = &http.Client{ - Transport: &http.Transport{ - TLSClientConfig: &tls.Config{ - InsecureSkipVerify: IsInsideDocker(), - }, - }, -} +// DefaultClient is the default client all http requests are fired from. +var DefaultClient = http.DefaultClient // DownloadReader returns a response reader. func DownloadReader(url string, body io.Reader, options ...DownloadOption) (io.ReadCloser, error) { @@ -48,7 +42,7 @@ func DownloadReader(url string, body io.Reader, options ...DownloadOption) (io.R } } - resp, err := httpClient.Do(req) + resp, err := DefaultClient.Do(req) if err != nil { return nil, err }