forked from AliyunContainerService/pouch
/
cli.go
171 lines (139 loc) · 4.16 KB
/
cli.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"fmt"
"os"
"strconv"
"text/tabwriter"
"time"
"github.com/alibaba/pouch/client"
"github.com/fatih/structs"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
// pouchDescription is used to describe pouch command in detail and auto generate command doc.
var pouchDescription = "pouch is a client side tool pouch to interact with daemon side process pouchd. " +
"Flags and arguments can be input to do what actually you wish. " +
"Then pouch parses the flags and arguments and sends a RESTful request to daemon side pouchd."
// Option uses to define the global options.
type Option struct {
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
// to server and so on. it's a framework.
type Cli struct {
Option
rootCmd *cobra.Command
APIClient client.CommonAPIClient
padding int
}
// NewCli creates an instance of 'Cli'.
func NewCli() *Cli {
return &Cli{
rootCmd: &cobra.Command{
Use: "pouch",
Short: "An efficient container engine",
Long: pouchDescription,
// disable displaying auto generation tag in cli docs
DisableAutoGenTag: true,
},
padding: 3,
}
}
// SetFlags sets all global options.
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")
flags.BoolVar(&c.Option.TLS.VerifyRemote, "tlsverify", false, "Use TLS and verify remote")
return c
}
// NewAPIClient initializes the API client in Cli.
func (c *Cli) NewAPIClient() {
client, err := client.NewAPIClient(c.Option.host, c.Option.TLS)
if err != nil {
logrus.Fatal(err)
}
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
}
// Run executes the client program.
func (c *Cli) Run() error {
return c.rootCmd.Execute()
}
// AddCommand add a subcommand.
func (c *Cli) AddCommand(parent, child Command) {
child.Init(c)
parentCmd := parent.Cmd()
childCmd := child.Cmd()
// make command error not return command usage and error
childCmd.SilenceUsage = true
childCmd.SilenceErrors = true
childCmd.DisableFlagsInUseLine = true
childCmd.PreRun = func(cmd *cobra.Command, args []string) {
c.InitLog()
c.NewAPIClient()
}
parentCmd.AddCommand(childCmd)
}
// NewTableDisplay creates a display instance, and uses to format output with table.
func (c *Cli) NewTableDisplay() *Display {
w := tabwriter.NewWriter(os.Stdout, 0, 0, c.padding, ' ', 0)
return &Display{w}
}
// Print outputs the obj's fields.
func (c *Cli) Print(obj interface{}) {
display := c.NewTableDisplay()
kvs := structs.Map(obj)
for k, v := range kvs {
line := []string{k + ":"}
switch v.(type) {
case string:
line = append(line, v.(string))
case int:
line = append(line, strconv.FormatInt(int64(v.(int)), 10))
case int32:
line = append(line, strconv.FormatInt(int64(v.(int32)), 10))
case int64:
line = append(line, strconv.FormatInt(v.(int64), 10))
case bool:
if v.(bool) {
line = append(line, "true")
} else {
line = append(line, "false")
}
default:
line = append(line, fmt.Sprintf("%v", v))
}
display.AddRow(line)
}
display.Flush()
}
// ExitError defines exit error produce by cli commands.
type ExitError struct {
Code int
Status string
}
// Error inplements error interface.
func (e ExitError) Error() string {
return fmt.Sprintf("Exit Code: %d, Status: %s", e.Code, e.Status)
}