Skip to content

Commit

Permalink
add a --proxy flag to resolve #3
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Aug 1, 2020
1 parent 93c7300 commit a84fa5b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
31 changes: 30 additions & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -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{
Expand All @@ -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
},
}

Expand All @@ -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())
Expand Down
1 change: 1 addition & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions registry.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
12 changes: 3 additions & 9 deletions utils/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package utils

import (
"compress/gzip"
"crypto/tls"
"encoding/json"
"fmt"
"io"
Expand All @@ -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) {
Expand All @@ -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
}
Expand Down

0 comments on commit a84fa5b

Please sign in to comment.