-
Notifications
You must be signed in to change notification settings - Fork 21
/
pluginutil.go
154 lines (137 loc) · 4.14 KB
/
pluginutil.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
package pluginutil
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"text/template"
"github.com/dcos/client-go/dcos"
"github.com/dcos/dcos-cli/pkg/httpclient"
"github.com/dcos/dcos-cli/pkg/log"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
// HTTPClient returns an HTTP client from a plugin runtime.
func HTTPClient(baseURL string, opts ...httpclient.Option) *httpclient.Client {
if baseURL == "" {
baseURL, _ = os.LookupEnv("DCOS_URL")
}
var baseOpts []httpclient.Option
if acsToken, _ := os.LookupEnv("DCOS_ACS_TOKEN"); acsToken != "" {
baseOpts = append(baseOpts, httpclient.ACSToken(acsToken))
}
if verbosity, _ := os.LookupEnv("DCOS_VERBOSITY"); verbosity != "" {
baseOpts = append(baseOpts, httpclient.Logger(Logger()))
}
tlsInsecure, _ := os.LookupEnv("DCOS_TLS_INSECURE")
if tlsInsecure == "1" {
baseOpts = append(baseOpts, httpclient.TLS(&tls.Config{InsecureSkipVerify: true})) //nolint: gosec
} else {
tlsCAPath, _ := os.LookupEnv("DCOS_TLS_CA_PATH")
if tlsCAPath != "" {
rootCAsPEM, err := ioutil.ReadFile(tlsCAPath)
if err == nil {
certPool := x509.NewCertPool()
if certPool.AppendCertsFromPEM(rootCAsPEM) {
baseOpts = append(baseOpts, httpclient.TLS(&tls.Config{RootCAs: certPool, MinVersion: tls.VersionTLS12}))
}
}
}
}
return httpclient.New(strings.TrimRight(baseURL, "/"), append(baseOpts, opts...)...)
}
// NewHTTPClient returns an HTTP client from a plugin runtime.
// It is created with the `client-go` package and will eventually
// replace the httpClient of the CLI.
func NewHTTPClient() *http.Client {
dcosConfig := dcos.NewConfig(nil)
dcosConfig = SetConfigFromEnv(dcosConfig)
client := dcos.NewHTTPClient(dcosConfig)
client.Transport.(*dcos.DefaultTransport).Logger = Logger()
return client
}
// SetConfigFromEnv updates the fields of a DC/OS Config depending
// on the environment where it is being run.
func SetConfigFromEnv(dcosConfig *dcos.Config) *dcos.Config {
if acsToken, _ := os.LookupEnv("DCOS_ACS_TOKEN"); acsToken != "" {
dcosConfig.SetACSToken(acsToken)
}
var tls dcos.TLS
tlsInsecure, _ := os.LookupEnv("DCOS_TLS_INSECURE")
if tlsInsecure == "1" {
tls.Insecure = true
} else {
tls.RootCAsPath, _ = os.LookupEnv("DCOS_TLS_CA_PATH")
if tls.RootCAsPath != "" {
rootCAsPEM, err := ioutil.ReadFile(tls.RootCAsPath)
if err == nil {
certPool := x509.NewCertPool()
if certPool.AppendCertsFromPEM(rootCAsPEM) {
tls.RootCAs = certPool
}
}
}
}
dcosConfig.SetTLS(tls)
return dcosConfig
}
// Logger returns a logger for a given plugin runtime.
func Logger() *logrus.Logger {
logger := &logrus.Logger{
Out: os.Stderr,
Formatter: &log.Formatter{},
Hooks: make(logrus.LevelHooks),
}
verbosity, _ := os.LookupEnv("DCOS_VERBOSITY")
switch verbosity {
case "0":
logger.SetLevel(logrus.WarnLevel)
case "1":
logger.SetLevel(logrus.InfoLevel)
case "2":
logger.SetLevel(logrus.DebugLevel)
os.Setenv("DCOS_DEBUG", "1")
}
return logger
}
// Usage defines a help template following the UX guidelines
func Usage(command *cobra.Command) error {
tpl := template.New("top")
template.Must(tpl.Parse(`Usage:{{if .HasAvailableSubCommands}}
{{.CommandPath}} [command]{{else if .Runnable}}
{{.UseLine}}{{end}}{{if .HasExample}}
Examples:
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}
Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
{{.Name}}
{{.Short}}{{end}}{{end}}{{end}}
`))
err := tpl.Execute(os.Stdout, command)
if err != nil {
return err
}
if command.HasAvailableLocalFlags() {
fmt.Println("\nOptions:")
command.LocalFlags().VisitAll(func(f *pflag.Flag) {
if f.Hidden {
return
}
if f.Shorthand != "" && f.Name != "" {
fmt.Printf(" -%s, --%s\n", f.Shorthand, f.Name)
} else if f.Shorthand != "" {
fmt.Printf(" -%s\n", f.Shorthand)
} else {
fmt.Printf(" --%s\n", f.Name)
}
fmt.Println(" " + f.Usage)
})
}
if command.HasAvailableSubCommands() {
fmt.Println("\n" + `Use "` + command.CommandPath() + ` [command] --help" for more information about a command.`)
}
return nil
}