github.com/henvic/wedeploycli@v1.7.6-0.20200319005353-3630f582f284/cmd/lcp/main.go (about)

     1  /*
     2  cli.cmd
     3  
     4  	https://github.com/wedeploy/cli
     5  
     6  */
     7  
     8  package main
     9  
    10  import (
    11  	"crypto/tls"
    12  	"fmt"
    13  	"math/rand"
    14  	"net/http"
    15  	"os"
    16  	"time"
    17  
    18  	wedeploy "github.com/henvic/wedeploy-sdk-go"
    19  	cmd "github.com/henvic/wedeploycli/command"
    20  	"github.com/henvic/wedeploycli/command/gitcredentialhelper"
    21  	"github.com/henvic/wedeploycli/envs"
    22  	"gopkg.in/src-d/go-git.v4/plumbing/transport/client"
    23  	githttp "gopkg.in/src-d/go-git.v4/plumbing/transport/http"
    24  )
    25  
    26  func maybeSetCustomTimezone() {
    27  	timezone := os.Getenv(envs.TZ)
    28  
    29  	if timezone == "" {
    30  		return
    31  	}
    32  
    33  	l, err := time.LoadLocation(timezone)
    34  
    35  	if err != nil {
    36  		_, _ = fmt.Fprintf(os.Stderr, "failure setting a custom timezone: %+v\n", err)
    37  		return
    38  	}
    39  
    40  	time.Local = l
    41  }
    42  
    43  func maybeShortcutCredentialHelper() {
    44  	if len(os.Args) < 2 || os.Args[1] != "git-credential-helper" {
    45  		return
    46  	}
    47  
    48  	var err = gitcredentialhelper.Run(os.Args)
    49  
    50  	if err != nil {
    51  		_, _ = fmt.Fprintf(os.Stderr, "%v\n", err)
    52  		os.Exit(1)
    53  	}
    54  
    55  	os.Exit(0)
    56  }
    57  
    58  func maybeSetSkipTLSVerification() {
    59  	skipTLSVerification := os.Getenv(envs.SkipTLSVerification)
    60  
    61  	if skipTLSVerification == "" {
    62  		return
    63  	}
    64  
    65  	wedeployClient := wedeploy.Client()
    66  	dt := http.DefaultTransport.(*http.Transport)
    67  
    68  	// create new Transport that ignores self-signed SSL
    69  	t := &http.Transport{
    70  		// deep copy values from net/http DefaultTransport
    71  		Proxy:                 dt.Proxy,
    72  		DialContext:           dt.DialContext,
    73  		MaxIdleConns:          dt.MaxIdleConns,
    74  		IdleConnTimeout:       dt.IdleConnTimeout,
    75  		ExpectContinueTimeout: dt.ExpectContinueTimeout,
    76  		TLSHandshakeTimeout:   dt.TLSHandshakeTimeout,
    77  
    78  		// With an unsafe TLS config, #nosec G402
    79  		TLSClientConfig: &tls.Config{
    80  			InsecureSkipVerify: true,
    81  		},
    82  	}
    83  
    84  	c := &http.Client{
    85  		Transport: t,
    86  	}
    87  
    88  	// override only the wedeploy HTTP client, instead of http.DefaultTransport,
    89  	// as it is less risky than for any clients
    90  	wedeployClient.SetHTTP(c)
    91  
    92  	// Install it as default client for https URLs.
    93  	client.InstallProtocol("https", githttp.NewClient(c))
    94  }
    95  
    96  func main() {
    97  	maybeSetCustomTimezone()
    98  	maybeSetSkipTLSVerification()
    99  
   100  	rand.Seed(time.Now().UTC().UnixNano())
   101  
   102  	maybeShortcutCredentialHelper()
   103  	cmd.Execute()
   104  }