This repository was archived by the owner on Aug 15, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathcli.go
More file actions
227 lines (182 loc) · 4.83 KB
/
Copy pathcli.go
File metadata and controls
227 lines (182 loc) · 4.83 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package cli
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"sort"
"strconv"
"strings"
"koding/kites/metrics"
"koding/klientctl/config"
"koding/klientctl/helper"
"koding/klientctl/util"
"github.com/koding/logging"
"github.com/spf13/cobra"
)
// PrintJSON converts provided object to formatted JSON string and writes it to w.
func PrintJSON(w io.Writer, v interface{}) {
enc := json.NewEncoder(w)
enc.SetEscapeHTML(false)
enc.SetIndent("", "\t")
enc.Encode(v)
}
// AskList returns a function which either asks to select one of provided items
// or returns error when interactive mode is disabled. This function is no-op
// when there is only one provided item.
func AskList(c *CLI, cmd *cobra.Command) func([]string, []string) (string, error) {
noninteractive, _ := cmd.Flags().GetBool("force")
if _, ok := os.LookupEnv("KD_NONINTERACTIVE"); ok {
noninteractive = true
}
return func(items, descriptions []string) (string, error) {
if len(items) == 0 {
return "", fmt.Errorf("no items provided")
}
if len(items) == 1 {
return items[0], nil
}
if noninteractive {
return "", fmt.Errorf("ambiguous identifier (matches: %s)", strings.Join(items, ","))
}
if len(items) != len(descriptions) {
return "", fmt.Errorf("invalid number of item descriptions")
}
for i, desc := range descriptions {
fmt.Fprintf(c.Out(), "[%d] %s\n", i+1, desc)
}
value, err := helper.Fask(c.In(), c.Out(),
"More than one items match provided identifier. Which one did you mean? [1-%d]: ", len(items))
if err != nil {
return "", err
}
idx, err := strconv.Atoi(value)
if err != nil {
return "", fmt.Errorf("invalid option provided %q", value)
}
if idx < 0 || idx >= len(items) {
return "", fmt.Errorf("value %d not in provided range", idx)
}
return items[idx], nil
}
}
// CLI represents the kd command line client that stores data streams and basic
// information about kd state.
type CLI struct {
in io.ReadCloser // input stream.
out io.Writer // output stream.
err io.Writer // error stream.
m *metrics.Metrics // usage metrics.
debug bool
log logging.Logger
mds map[string][]func() string // debug info about middlewares.
}
// NewCLI creates a new CLI client.
func NewCLI(in io.ReadCloser, out, err, logHandler io.Writer) *CLI {
c := &CLI{
in: in,
out: out,
err: err,
debug: isDebug(),
log: newLogger(logHandler),
mds: make(map[string][]func() string),
}
if !config.Konfig.DisableMetrics {
if m, err := metrics.New("kd"); err != nil {
c.Log().Warning("Metrics will not be collected: %v", err)
} else {
c.m = m
}
}
return c
}
// In returns CLI's input stream. Defaults to standard input when nil.
func (c *CLI) In() io.Reader {
if c.in != nil {
return c.in
}
return os.Stdin
}
// Out returns CLI's output stream. Defaults to standard output when nil.
func (c *CLI) Out() io.Writer {
if c.out != nil {
return c.out
}
return os.Stdout
}
// Err return CLI's error stream. Defaults to standard error when nil.
func (c *CLI) Err() io.Writer {
if c.err != nil {
return c.err
}
return os.Stderr
}
// Log returns CLI's logger. Defaults to discard logger when nil.
func (c *CLI) Log() logging.Logger {
if c.log != nil {
return c.log
}
return newLogger(nil)
}
// Metrics returns metrics client or nil if not enabled.
func (c *CLI) Metrics() *metrics.Metrics {
return c.m
}
// IsDebug returns true when debug mode is enabled.
func (c *CLI) IsDebug() bool {
return isDebug()
}
// IsAdmin checks whether or not the current user has admin privileges.
func (c *CLI) IsAdmin() (bool, error) {
return util.NewPermissions().IsAdmin()
}
// Middlewares return debug information about middlewares and commands they wrap.
func (c *CLI) Middlewares() map[string][]string {
all := make(map[string][]string)
for m, fs := range c.mds {
for _, f := range fs {
all[m] = append(all[m], f())
}
}
for m := range all {
sort.Strings(all[m])
}
return all
}
func (c *CLI) registerMiddleware(name string, cmd *cobra.Command) {
f := func() (desc string) {
desc = cmd.CommandPath()
if aliasPath := cmd.Annotations[AliasAnnotation]; len(aliasPath) != 0 {
desc += " (alias: " + aliasPath + ")"
}
return desc
}
c.mds[name] = append(c.mds[name], f)
}
// Close closes all resources managed by CLI object.
func (c *CLI) Close() (err error) {
if c.in != nil {
err = c.in.Close()
}
if c.m != nil {
if ee := c.m.Close(); ee != nil && err == nil {
err = ee
}
}
return
}
func newLogger(w io.Writer) logging.Logger {
if w == nil {
w = ioutil.Discard
}
handler := logging.NewWriterHandler(w)
// Make handler writer accept all incoming log messages.
handler.SetLevel(logging.DEBUG)
logger := logging.NewCustom("kd", isDebug())
logger.SetHandler(handler)
return logger
}
func isDebug() bool {
return os.Getenv("KD_DEBUG") == "1" || config.Konfig.Debug
}