/
root.go
228 lines (191 loc) · 7.77 KB
/
root.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
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
228
// Copyright © 2018 Humio Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"net/url"
"os"
"path"
"github.com/humio/cli/api"
"github.com/humio/cli/cmd/humioctl/internal/viperkey"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var cfgFile, tokenFile, token, address, caCertificateFile, profileFlag, proxyOrganization string
var insecure bool
var printVersion bool
// rootCmd represents the base command when called without any subcommands
var rootCmd *cobra.Command
func init() {
rootCmd = &cobra.Command{
Use: "humioctl [subcommand] [flags] [arguments]",
Short: "A management CLI for Humio.",
Long: `
Sending Data:
Humio's CLI is not a replacement for fully-featured data-shippers like
LogStash, FileBeat or MetricBeat. It can be handy to easily send logs
to Humio, e.g examine a local log file or test a parser on test input.
To stream the content of "/var/log/system.log" data to Humio:
$ tail -f /var/log/system.log | humioctl ingest -o
or
$ humioctl ingest -o --tail=/var/log/system.log
Common Management Commands:
users <subcommand>
parsers <subcommand>
views <subcommand>
status
`,
Run: func(cmd *cobra.Command, args []string) {
if printVersion {
cmd.Printf("humioctl %s (%s on %s)\n", version, commit, date)
os.Exit(0)
}
// If no token or address flags are passed
// and no configuration file exists, run login.
if viper.GetString(viperkey.Token) == "" && viper.GetString(viperkey.Address) == "" {
if err := newWelcomeCmd().Execute(); err != nil {
fmt.Println(fmt.Errorf("error printing welcome message: %v", err))
}
} else {
if err := cmd.Help(); err != nil {
fmt.Println(fmt.Errorf("error printing help: %v", err))
}
}
},
}
cobra.OnInitialize(initConfig)
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
rootCmd.PersistentFlags().StringVarP(&profileFlag, "profile", "u", "", "Name of the config profile to use")
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "Config file (default is $HOME/.humio/config.yaml)")
rootCmd.PersistentFlags().StringVarP(&token, "token", "t", "", "The API token to use when talking to Humio. Overrides the value in your config file.")
rootCmd.PersistentFlags().StringVar(&tokenFile, "token-file", "", "File path to a file containing the API token. Overrides the value in your config file and the value of --token.")
rootCmd.PersistentFlags().StringVarP(&address, "address", "a", "", "The HTTP address of the Humio cluster. Overrides the value in your config file.")
rootCmd.PersistentFlags().StringVar(&caCertificateFile, "ca-certificate-file", "", "File path to a file containing the CA certificate in PEM format. Overrides the value in your config file.")
rootCmd.PersistentFlags().BoolVar(&insecure, "insecure", false, "By default, all encrypted connections will verify that the hostname in the TLS certificate matches the name from the URL. Set this to true to ignore hostname validation.")
rootCmd.PersistentFlags().StringVar(&proxyOrganization, "proxy-organization", "", "Commands are executed in the specified organization.")
rootCmd.PersistentFlags().String("format", "", "Change output format of commands, if supported. Valid formats: json")
_ = viper.BindPFlag(viperkey.Address, rootCmd.PersistentFlags().Lookup("address"))
_ = viper.BindPFlag(viperkey.Token, rootCmd.PersistentFlags().Lookup("token"))
_ = viper.BindPFlag(viperkey.TokenFile, rootCmd.PersistentFlags().Lookup("token-file"))
_ = viper.BindPFlag(viperkey.CACertificateFile, rootCmd.PersistentFlags().Lookup("ca-certificate-file"))
_ = viper.BindPFlag(viperkey.Insecure, rootCmd.PersistentFlags().Lookup("insecure"))
_ = viper.BindPFlag(viperkey.ProxyOrganization, rootCmd.PersistentFlags().Lookup("proxy-organization"))
rootCmd.Flags().BoolVarP(&printVersion, "version", "v", false, "Print the client version")
rootCmd.AddCommand(newUsersCmd())
rootCmd.AddCommand(newParsersCmd())
rootCmd.AddCommand(newIngestCmd())
rootCmd.AddCommand(newProfilesCmd())
rootCmd.AddCommand(newIngestTokensCmd())
rootCmd.AddCommand(newViewsCmd())
rootCmd.AddCommand(newCompletionCmd())
rootCmd.AddCommand(newLicenseCmd())
rootCmd.AddCommand(newReposCmd())
rootCmd.AddCommand(newSearchCmd())
rootCmd.AddCommand(newStatusCmd())
rootCmd.AddCommand(newHealthCmd())
rootCmd.AddCommand(newClusterCmd())
rootCmd.AddCommand(newActionsCmd())
rootCmd.AddCommand(newAlertsCmd())
rootCmd.AddCommand(newPackagesCmd())
rootCmd.AddCommand(newGroupsCmd())
rootCmd.AddCommand(newFilesCmd())
rootCmd.AddCommand(newFeatureFlagsCmd())
// Hidden Commands
rootCmd.AddCommand(newWelcomeCmd())
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
cfgFile = path.Join(home, ".humio", "config.yaml")
viper.SetConfigFile(cfgFile)
viper.SetConfigType("yaml")
}
viper.SetEnvPrefix("HUMIO")
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
_ = viper.ReadInConfig()
// If the user has specified a profile flag, load it.
if profileFlag != "" {
profile, err := loadProfile(profileFlag)
exitOnError(rootCmd, err, "Failed to load profile")
// Explicitly bound address or token have precedence
if address == "" {
viper.Set(viperkey.Address, profile.address)
}
if token == "" {
viper.Set(viperkey.Token, profile.token)
}
if caCertificateFile == "" {
viper.Set(viperkey.CACertificate, profile.caCertificate)
}
if !insecure {
viper.Set(viperkey.Insecure, profile.insecure)
}
}
if tokenFile != "" {
// #nosec G304
tokenFileContent, err := os.ReadFile(tokenFile)
exitOnError(rootCmd, err, "Error loading token file")
viper.Set(viperkey.Token, string(tokenFileContent))
}
if caCertificateFile != "" {
// #nosec G304
caCertificateFileContent, err := os.ReadFile(caCertificateFile)
exitOnError(rootCmd, err, "Error loading CA certificate file")
viper.Set(viperkey.CACertificate, string(caCertificateFileContent))
}
if insecure {
viper.Set(viperkey.Insecure, insecure)
}
}
func NewApiClient(cmd *cobra.Command, opts ...func(config *api.Config)) *api.Client {
client, err := newApiClientE(opts...)
exitOnError(cmd, err, "Error creating HTTP client")
return client
}
func newApiClientE(opts ...func(config *api.Config)) (*api.Client, error) {
config := api.DefaultConfig()
addr := viper.GetString(viperkey.Address)
parsedURL, err := url.Parse(addr)
if err != nil {
return nil, err
}
config.Address = parsedURL
config.Token = viper.GetString(viperkey.Token)
config.CACertificatePEM = viper.GetString(viperkey.CACertificate)
config.Insecure = viper.GetBool(viperkey.Insecure)
config.ProxyOrganization = viper.GetString(viperkey.ProxyOrganization)
config.UserAgent = fmt.Sprintf("humioctl/%s (%s on %s)", version, commit, date)
for _, opt := range opts {
opt(&config)
}
return api.NewClient(config), nil
}
func main() {
SetVersion(version, commit, date)
err := rootCmd.Execute()
exitOnError(rootCmd, err, "Unable to execute command")
}