Skip to content

Commit

Permalink
Merge pull request #4 from gomicro/verbose-output
Browse files Browse the repository at this point in the history
Verbose and Insecure Flags
  • Loading branch information
dan9186 authored Jan 30, 2018
2 parents f084098 + f55fa35 commit 4f76c37
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,23 @@ import (
"github.com/spf13/cobra"
)

var (
verbose bool
skipVerify bool
)

func init() {
cobra.OnInitialize(initEnvs)

RootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "show more verbose output")
RootCmd.Flags().BoolVarP(&skipVerify, "insecure", "k", false, "permit operations for servers otherwise considered insecure")
}

func initEnvs() {
}

var RootCmd = &cobra.Command{
Use: "probe",
Use: "probe [flags] URL",
Short: "Lightweight healthchecker for scratch containers",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
Expand All @@ -32,31 +40,49 @@ var RootCmd = &cobra.Command{

func Execute() {
if err := RootCmd.Execute(); err != nil {
fmt.Printf("Failed to execute: %v\n", err.Error())
printf("Failed to execute: %v\n", err.Error())
os.Exit(1)
}
}

func probe(cmd *cobra.Command, args []string) {
pool, err := gocertifi.CACerts()
if err != nil {
fmt.Printf("Error: failed to create cert pool: %v\n", err.Error())
printf("Error: failed to create cert pool: %v\n", err.Error())
}

tlsConfig := &tls.Config{
RootCAs: pool,
}

if skipVerify {
tlsConfig.InsecureSkipVerify = true
}

transport := &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: pool,
InsecureSkipVerify: true,
},
TLSClientConfig: tlsConfig,
}

client := &http.Client{Transport: transport}

resp, err := client.Get(args[0])
if err != nil {
verbosef("error: http get: %v", err.Error())
os.Exit(1)
}

if resp.StatusCode != http.StatusOK {
verbosef("error: status %v", resp.StatusCode)
os.Exit(1)
}
}

func printf(f string, args ...interface{}) {
fmt.Println(fmt.Sprintf(f, args...))
}

func verbosef(f string, args ...interface{}) {
if verbose {
fmt.Println(fmt.Sprintf(f, args...))
}
}

0 comments on commit 4f76c37

Please sign in to comment.