Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add client debug flag #1234

Merged
merged 1 commit into from
Apr 28, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"strconv"
"text/tabwriter"
"time"

"github.com/alibaba/pouch/client"

Expand All @@ -20,8 +21,9 @@ var pouchDescription = "pouch is a client side tool pouch to interact with daemo

// Option uses to define the global options.
type Option struct {
host string
TLS client.TLSConfig
host string
Debug bool
TLS client.TLSConfig
}

// Cli is the client's core struct, it will be used to manage all subcommand, send http request
Expand Down Expand Up @@ -51,6 +53,7 @@ func NewCli() *Cli {
func (c *Cli) SetFlags() *Cli {
flags := c.rootCmd.PersistentFlags()
flags.StringVarP(&c.Option.host, "host", "H", "unix:///var/run/pouchd.sock", "Specify connecting address of Pouch CLI")
flags.BoolVarP(&c.Option.Debug, "debug", "D", false, "Switch client log level to DEBUG mode")
flags.StringVar(&c.Option.TLS.Key, "tlskey", "", "Specify key file of TLS")
flags.StringVar(&c.Option.TLS.Cert, "tlscert", "", "Specify cert file of TLS")
flags.StringVar(&c.Option.TLS.CA, "tlscacert", "", "Specify CA file of TLS")
Expand All @@ -68,6 +71,20 @@ func (c *Cli) NewAPIClient() {
c.APIClient = client
}

// InitLog initializes log Level and log format of client.
func (c *Cli) InitLog() {
if c.Option.Debug {
logrus.SetLevel(logrus.DebugLevel)
logrus.Infof("start client at debug level")
}

formatter := &logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: time.RFC3339Nano,
}
logrus.SetFormatter(formatter)
}

// Client returns API client torwards daemon.
func (c *Cli) Client() client.CommonAPIClient {
return c.APIClient
Expand All @@ -91,6 +108,7 @@ func (c *Cli) AddCommand(parent, child Command) {
childCmd.DisableFlagsInUseLine = true

childCmd.PreRun = func(cmd *cobra.Command, args []string) {
c.InitLog()
c.NewAPIClient()
}

Expand Down