Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Ciena Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package commands |
| 17 | |
| 18 | import ( |
David K. Bainbridge | 1d94644 | 2021-03-19 16:45:52 +0000 | [diff] [blame] | 19 | "context" |
| 20 | "crypto/tls" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 21 | "encoding/json" |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 22 | "errors" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 23 | "fmt" |
serkant.uluderya | d3daa90 | 2020-05-21 22:49:25 -0700 | [diff] [blame] | 24 | "log" |
serkant.uluderya | d3daa90 | 2020-05-21 22:49:25 -0700 | [diff] [blame] | 25 | "os" |
David K. Bainbridge | 238e625 | 2021-06-28 09:49:16 -0700 | [diff] [blame] | 26 | "os/user" |
serkant.uluderya | d3daa90 | 2020-05-21 22:49:25 -0700 | [diff] [blame] | 27 | "path/filepath" |
| 28 | "reflect" |
| 29 | "regexp" |
| 30 | "strconv" |
| 31 | "strings" |
| 32 | "time" |
| 33 | |
Akash Reddy Kankanala | c001463 | 2025-05-21 17:12:20 +0530 | [diff] [blame^] | 34 | "github.com/golang/protobuf/jsonpb" //nolint:staticcheck |
| 35 | "github.com/golang/protobuf/proto" //nolint:staticcheck |
David K. Bainbridge | 1d94644 | 2021-03-19 16:45:52 +0000 | [diff] [blame] | 36 | configv1 "github.com/opencord/voltctl/internal/pkg/apis/config/v1" |
| 37 | configv2 "github.com/opencord/voltctl/internal/pkg/apis/config/v2" |
David K. Bainbridge | 9189c63 | 2021-03-26 21:52:21 +0000 | [diff] [blame] | 38 | configv3 "github.com/opencord/voltctl/internal/pkg/apis/config/v3" |
Scott Baker | 2b0ad65 | 2019-08-21 14:57:07 -0700 | [diff] [blame] | 39 | "github.com/opencord/voltctl/pkg/filter" |
| 40 | "github.com/opencord/voltctl/pkg/format" |
| 41 | "github.com/opencord/voltctl/pkg/order" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 42 | "google.golang.org/grpc" |
David K. Bainbridge | 1d94644 | 2021-03-19 16:45:52 +0000 | [diff] [blame] | 43 | "google.golang.org/grpc/credentials" |
| 44 | yaml "gopkg.in/yaml.v2" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 45 | ) |
| 46 | |
| 47 | type OutputType uint8 |
| 48 | |
| 49 | const ( |
| 50 | OUTPUT_TABLE OutputType = iota |
| 51 | OUTPUT_JSON |
| 52 | OUTPUT_YAML |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame] | 53 | |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame] | 54 | supportedKvStoreType = "etcd" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 55 | ) |
| 56 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 57 | var ( |
| 58 | ParamNames = map[string]map[string]string{ |
| 59 | "v1": { |
| 60 | "ID": "voltha.ID", |
| 61 | }, |
| 62 | "v2": { |
| 63 | "ID": "common.ID", |
| 64 | }, |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 65 | "v3": { |
Dinesh Belwalkar | c9aa6d8 | 2020-03-04 15:22:17 -0800 | [diff] [blame] | 66 | "ID": "common.ID", |
| 67 | "port": "voltha.Port", |
| 68 | "ValueSpecifier": "common.ValueSpecifier", |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 69 | }, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | CharReplacer = strings.NewReplacer("\\t", "\t", "\\n", "\n") |
| 73 | |
David K. Bainbridge | 9189c63 | 2021-03-26 21:52:21 +0000 | [diff] [blame] | 74 | GlobalConfig = configv3.NewDefaultConfig() |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 75 | |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 76 | GlobalCommandOptions = make(map[string]map[string]string) |
| 77 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 78 | GlobalOptions struct { |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame] | 79 | Config string `short:"c" long:"config" env:"VOLTCONFIG" value-name:"FILE" default:"" description:"Location of client config file"` |
David K. Bainbridge | 9189c63 | 2021-03-26 21:52:21 +0000 | [diff] [blame] | 80 | Stack string `short:"v" long:"stack" env:"STACK" value-name:"STACK" default:"" description:"Name of stack to use in multistack deployment"` |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame] | 81 | Server string `short:"s" long:"server" default:"" value-name:"SERVER:PORT" description:"IP/Host and port of VOLTHA"` |
| 82 | Kafka string `short:"k" long:"kafka" default:"" value-name:"SERVER:PORT" description:"IP/Host and port of Kafka"` |
| 83 | KvStore string `short:"e" long:"kvstore" env:"KVSTORE" value-name:"SERVER:PORT" description:"IP/Host and port of KV store (etcd)"` |
| 84 | |
David K. Bainbridge | 2b62761 | 2020-02-18 14:50:13 -0800 | [diff] [blame] | 85 | // nolint: staticcheck |
serkant.uluderya | d3daa90 | 2020-05-21 22:49:25 -0700 | [diff] [blame] | 86 | Debug bool `short:"d" long:"debug" description:"Enable debug mode"` |
| 87 | Timeout string `short:"t" long:"timeout" description:"API call timeout duration" value-name:"DURATION" default:""` |
| 88 | UseTLS bool `long:"tls" description:"Use TLS"` |
| 89 | CACert string `long:"tlscacert" value-name:"CA_CERT_FILE" description:"Trust certs signed only by this CA"` |
| 90 | Cert string `long:"tlscert" value-name:"CERT_FILE" description:"Path to TLS vertificate file"` |
| 91 | Key string `long:"tlskey" value-name:"KEY_FILE" description:"Path to TLS key file"` |
| 92 | Verify bool `long:"tlsverify" description:"Use TLS and verify the remote"` |
| 93 | K8sConfig string `short:"8" long:"k8sconfig" env:"KUBECONFIG" value-name:"FILE" default:"" description:"Location of Kubernetes config file"` |
| 94 | KvStoreTimeout string `long:"kvstoretimeout" env:"KVSTORE_TIMEOUT" value-name:"DURATION" default:"" description:"timeout for calls to KV store"` |
| 95 | CommandOptions string `short:"o" long:"command-options" env:"VOLTCTL_COMMAND_OPTIONS" value-name:"FILE" default:"" description:"Location of command options default configuration file"` |
| 96 | MaxCallRecvMsgSize string `short:"m" long:"maxcallrecvmsgsize" description:"Max GRPC Client request size limit in bytes (eg: 4MB)" value-name:"SIZE" default:"4M"` |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 97 | } |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 98 | |
| 99 | Debug = log.New(os.Stdout, "DEBUG: ", 0) |
| 100 | Info = log.New(os.Stdout, "INFO: ", 0) |
| 101 | Warn = log.New(os.Stderr, "WARN: ", 0) |
| 102 | Error = log.New(os.Stderr, "ERROR: ", 0) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 103 | ) |
| 104 | |
| 105 | type OutputOptions struct { |
David K. Bainbridge | 2b62761 | 2020-02-18 14:50:13 -0800 | [diff] [blame] | 106 | Format string `long:"format" value-name:"FORMAT" default:"" description:"Format to use to output structured data"` |
| 107 | Quiet bool `short:"q" long:"quiet" description:"Output only the IDs of the objects"` |
| 108 | // nolint: staticcheck |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 109 | OutputAs string `short:"o" long:"outputas" default:"table" choice:"table" choice:"json" choice:"yaml" description:"Type of output to generate"` |
| 110 | NameLimit int `short:"l" long:"namelimit" default:"-1" value-name:"LIMIT" description:"Limit the depth (length) in the table column name"` |
| 111 | } |
| 112 | |
Maninder | 045921e | 2020-09-29 16:46:02 +0530 | [diff] [blame] | 113 | type FlowIdOptions struct { |
| 114 | HexId bool `short:"x" long:"hex-id" description:"Output Ids in hex format"` |
| 115 | } |
| 116 | |
Himani Chawla | 3c161c6 | 2021-05-13 16:36:51 +0530 | [diff] [blame] | 117 | type GroupListOptions struct { |
| 118 | Bucket bool `short:"b" long:"buckets" description:"Display Buckets"` |
| 119 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 120 | type ListOutputOptions struct { |
| 121 | OutputOptions |
| 122 | Filter string `short:"f" long:"filter" default:"" value-name:"FILTER" description:"Only display results that match filter"` |
| 123 | OrderBy string `short:"r" long:"orderby" default:"" value-name:"ORDER" description:"Specify the sort order of the results"` |
| 124 | } |
| 125 | |
| 126 | type OutputOptionsJson struct { |
David K. Bainbridge | 2b62761 | 2020-02-18 14:50:13 -0800 | [diff] [blame] | 127 | Format string `long:"format" value-name:"FORMAT" default:"" description:"Format to use to output structured data"` |
| 128 | Quiet bool `short:"q" long:"quiet" description:"Output only the IDs of the objects"` |
| 129 | // nolint: staticcheck |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 130 | OutputAs string `short:"o" long:"outputas" default:"json" choice:"table" choice:"json" choice:"yaml" description:"Type of output to generate"` |
| 131 | NameLimit int `short:"l" long:"namelimit" default:"-1" value-name:"LIMIT" description:"Limit the depth (length) in the table column name"` |
| 132 | } |
| 133 | |
| 134 | type ListOutputOptionsJson struct { |
| 135 | OutputOptionsJson |
| 136 | Filter string `short:"f" long:"filter" default:"" value-name:"FILTER" description:"Only display results that match filter"` |
| 137 | OrderBy string `short:"r" long:"orderby" default:"" value-name:"ORDER" description:"Specify the sort order of the results"` |
| 138 | } |
| 139 | |
| 140 | func toOutputType(in string) OutputType { |
| 141 | switch in { |
| 142 | case "table": |
| 143 | fallthrough |
| 144 | default: |
| 145 | return OUTPUT_TABLE |
| 146 | case "json": |
| 147 | return OUTPUT_JSON |
| 148 | case "yaml": |
| 149 | return OUTPUT_YAML |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | type CommandResult struct { |
| 154 | Format format.Format |
| 155 | Filter string |
| 156 | OrderBy string |
| 157 | OutputAs OutputType |
| 158 | NameLimit int |
| 159 | Data interface{} |
| 160 | } |
| 161 | |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 162 | func GetCommandOptionWithDefault(name, option, defaultValue string) string { |
| 163 | if cmd, ok := GlobalCommandOptions[name]; ok { |
| 164 | if val, ok := cmd[option]; ok { |
| 165 | return CharReplacer.Replace(val) |
| 166 | } |
| 167 | } |
| 168 | return defaultValue |
| 169 | } |
| 170 | |
serkant.uluderya | d3daa90 | 2020-05-21 22:49:25 -0700 | [diff] [blame] | 171 | var sizeParser = regexp.MustCompile(`^([0-9]+)([PETGMK]?)I?B?$`) |
| 172 | |
| 173 | func parseSize(size string) (uint64, error) { |
| 174 | |
| 175 | parts := sizeParser.FindAllStringSubmatch(strings.ToUpper(size), -1) |
| 176 | if len(parts) == 0 { |
| 177 | return 0, fmt.Errorf("size: invalid size '%s'", size) |
| 178 | } |
| 179 | value, err := strconv.ParseUint(parts[0][1], 10, 64) |
| 180 | if err != nil { |
| 181 | return 0, fmt.Errorf("size: invalid size '%s'", size) |
| 182 | } |
| 183 | switch parts[0][2] { |
| 184 | case "E": |
| 185 | value = value * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 |
| 186 | case "P": |
| 187 | value = value * 1024 * 1024 * 1024 * 1024 * 1024 |
| 188 | case "T": |
| 189 | value = value * 1024 * 1024 * 1024 * 1024 |
| 190 | case "G": |
| 191 | value = value * 1024 * 1024 * 1024 |
| 192 | case "M": |
| 193 | value = value * 1024 * 1024 |
| 194 | case "K": |
| 195 | value = value * 1024 |
| 196 | default: |
| 197 | } |
| 198 | return value, nil |
| 199 | } |
| 200 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 201 | func ProcessGlobalOptions() { |
David K. Bainbridge | 9189c63 | 2021-03-26 21:52:21 +0000 | [diff] [blame] | 202 | ReadConfig() |
| 203 | |
| 204 | // If a stack is selected via command line set it |
| 205 | if GlobalOptions.Stack != "" { |
| 206 | if GlobalConfig.StackByName(GlobalOptions.Stack) == nil { |
| 207 | Error.Fatalf("stack specified, '%s', not found in configuration", |
| 208 | GlobalOptions.Stack) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 209 | } |
David K. Bainbridge | 9189c63 | 2021-03-26 21:52:21 +0000 | [diff] [blame] | 210 | GlobalConfig.CurrentStack = GlobalOptions.Stack |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 211 | } |
| 212 | |
David K. Bainbridge | 9189c63 | 2021-03-26 21:52:21 +0000 | [diff] [blame] | 213 | ApplyOptionOverrides(GlobalConfig.Current()) |
serkant.uluderya | d3daa90 | 2020-05-21 22:49:25 -0700 | [diff] [blame] | 214 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 215 | // If a k8s cert/key were not specified, then attempt to read it from |
| 216 | // any $HOME/.kube/config if it exists |
| 217 | if len(GlobalOptions.K8sConfig) == 0 { |
| 218 | home, err := os.UserHomeDir() |
| 219 | if err != nil { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 220 | Warn.Printf("Unable to discover the user's home directory: %s", err) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 221 | home = "~" |
| 222 | } |
| 223 | GlobalOptions.K8sConfig = filepath.Join(home, ".kube", "config") |
| 224 | } |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 225 | |
| 226 | if len(GlobalOptions.CommandOptions) == 0 { |
| 227 | home, err := os.UserHomeDir() |
| 228 | if err != nil { |
| 229 | Warn.Printf("Unable to discover the user's home directory: %s", err) |
| 230 | home = "~" |
| 231 | } |
| 232 | GlobalOptions.CommandOptions = filepath.Join(home, ".volt", "command_options") |
| 233 | } |
| 234 | |
| 235 | if info, err := os.Stat(GlobalOptions.CommandOptions); err == nil && !info.IsDir() { |
Akash Reddy Kankanala | c001463 | 2025-05-21 17:12:20 +0530 | [diff] [blame^] | 236 | optionsFile, err := os.ReadFile(GlobalOptions.CommandOptions) |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 237 | if err != nil { |
| 238 | Error.Fatalf("Unable to read command options configuration file '%s' : %s", |
| 239 | GlobalOptions.CommandOptions, err.Error()) |
| 240 | } |
| 241 | if err = yaml.Unmarshal(optionsFile, &GlobalCommandOptions); err != nil { |
| 242 | Error.Fatalf("Unable to parse the command line options configuration file '%s': %s", |
| 243 | GlobalOptions.CommandOptions, err.Error()) |
| 244 | } |
| 245 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 246 | } |
| 247 | |
David K. Bainbridge | 9189c63 | 2021-03-26 21:52:21 +0000 | [diff] [blame] | 248 | func ApplyOptionOverrides(stack *configv3.StackConfigSpec) { |
| 249 | |
| 250 | if stack == nil { |
| 251 | // nothing to do |
| 252 | return |
| 253 | } |
| 254 | // Override from command line |
| 255 | if GlobalOptions.Server != "" { |
| 256 | stack.Server = GlobalOptions.Server |
| 257 | } |
| 258 | |
| 259 | if GlobalOptions.UseTLS { |
| 260 | stack.Tls.UseTls = true |
| 261 | } |
| 262 | |
| 263 | if GlobalOptions.Verify { |
| 264 | stack.Tls.Verify = true |
| 265 | } |
| 266 | |
| 267 | if GlobalOptions.Kafka != "" { |
| 268 | stack.Kafka = GlobalOptions.Kafka |
| 269 | } |
| 270 | |
| 271 | if GlobalOptions.KvStore != "" { |
| 272 | stack.KvStore = GlobalOptions.KvStore |
| 273 | } |
| 274 | |
| 275 | if GlobalOptions.KvStoreTimeout != "" { |
| 276 | timeout, err := time.ParseDuration(GlobalOptions.KvStoreTimeout) |
| 277 | if err != nil { |
| 278 | Error.Fatalf("Unable to parse specified KV strore timeout duration '%s': %s", |
| 279 | GlobalOptions.KvStoreTimeout, err.Error()) |
| 280 | } |
| 281 | stack.KvStoreConfig.Timeout = timeout |
| 282 | } |
| 283 | |
| 284 | if GlobalOptions.Timeout != "" { |
| 285 | timeout, err := time.ParseDuration(GlobalOptions.Timeout) |
| 286 | if err != nil { |
| 287 | Error.Fatalf("Unable to parse specified timeout duration '%s': %s", |
| 288 | GlobalOptions.Timeout, err.Error()) |
| 289 | } |
| 290 | stack.Grpc.Timeout = timeout |
| 291 | } |
| 292 | |
| 293 | if GlobalOptions.MaxCallRecvMsgSize != "" { |
| 294 | stack.Grpc.MaxCallRecvMsgSize = GlobalOptions.MaxCallRecvMsgSize |
| 295 | } |
| 296 | } |
| 297 | |
David K. Bainbridge | 238e625 | 2021-06-28 09:49:16 -0700 | [diff] [blame] | 298 | func homeDir() (string, error) { |
| 299 | |
| 300 | // First attempt is using the current user information, if that |
| 301 | // fails we attempt to use the environment variables via a call |
| 302 | // to os.UserHomeDir() |
| 303 | cur, err := user.Current() |
| 304 | if err != nil { |
David K. Bainbridge | 9189c63 | 2021-03-26 21:52:21 +0000 | [diff] [blame] | 305 | home, err := os.UserHomeDir() |
| 306 | if err != nil { |
David K. Bainbridge | 238e625 | 2021-06-28 09:49:16 -0700 | [diff] [blame] | 307 | return "", errors.New("unable to get current user or determine home directory via environment") |
| 308 | } |
| 309 | return home, nil |
| 310 | } |
| 311 | if len(cur.HomeDir) == 0 { |
| 312 | home, err := os.UserHomeDir() |
| 313 | if err != nil { |
| 314 | return "", errors.New("unable to get determine home directory via user information or environment") |
| 315 | } |
| 316 | return home, nil |
| 317 | } |
| 318 | return cur.HomeDir, nil |
| 319 | } |
| 320 | |
| 321 | func ReadConfig() { |
| 322 | // assume that the config file was specified until it is |
| 323 | // determined it was not |
| 324 | if len(GlobalOptions.Config) == 0 { |
| 325 | home, err := homeDir() |
| 326 | if err != nil { |
| 327 | Error.Fatalf("Unable to create default configuration file path: %s", err.Error()) |
David K. Bainbridge | 9189c63 | 2021-03-26 21:52:21 +0000 | [diff] [blame] | 328 | } |
| 329 | GlobalOptions.Config = filepath.Join(home, ".volt", "config") |
| 330 | } |
| 331 | |
David K. Bainbridge | 238e625 | 2021-06-28 09:49:16 -0700 | [diff] [blame] | 332 | // If the config file value is `~` or begins with `~/` then |
| 333 | // attempt to expand that to the user's home directory |
| 334 | if GlobalOptions.Config == "~" { |
| 335 | home, err := homeDir() |
| 336 | if err != nil { |
| 337 | Error.Fatalf("Unable to expand config file path '%s': %s", |
| 338 | GlobalOptions.Config, err) |
| 339 | } |
| 340 | GlobalOptions.Config = home |
| 341 | } else if strings.HasPrefix(GlobalOptions.Config, "~/") { |
| 342 | home, err := homeDir() |
| 343 | if err != nil { |
| 344 | Error.Fatalf("Unable to expand config file path '%s': %s", |
| 345 | GlobalOptions.Config, err) |
| 346 | } |
| 347 | GlobalOptions.Config = filepath.Join(home, |
| 348 | GlobalOptions.Config[2:]) |
| 349 | } |
| 350 | |
David K. Bainbridge | 9189c63 | 2021-03-26 21:52:21 +0000 | [diff] [blame] | 351 | if info, err := os.Stat(GlobalOptions.Config); err == nil && !info.IsDir() { |
Akash Reddy Kankanala | c001463 | 2025-05-21 17:12:20 +0530 | [diff] [blame^] | 352 | configFile, err := os.ReadFile(GlobalOptions.Config) |
David K. Bainbridge | 9189c63 | 2021-03-26 21:52:21 +0000 | [diff] [blame] | 353 | if err != nil { |
| 354 | Error.Fatalf("Unable to read the configuration file '%s': %s", |
| 355 | GlobalOptions.Config, err.Error()) |
| 356 | } |
| 357 | // First try the latest version of the config api then work |
| 358 | // backwards |
| 359 | if err = yaml.Unmarshal(configFile, &GlobalConfig); err != nil { |
| 360 | GlobalConfigV2 := configv2.NewDefaultConfig() |
| 361 | if err = yaml.Unmarshal(configFile, &GlobalConfigV2); err != nil { |
| 362 | GlobalConfigV1 := configv1.NewDefaultConfig() |
| 363 | if err = yaml.Unmarshal(configFile, &GlobalConfigV1); err != nil { |
| 364 | Error.Fatalf("Unable to parse the configuration file '%s': %s", |
| 365 | GlobalOptions.Config, err.Error()) |
| 366 | } |
| 367 | GlobalConfig = configv3.FromConfigV1(GlobalConfigV1) |
| 368 | } else { |
| 369 | GlobalConfig = configv3.FromConfigV2(GlobalConfigV2) |
| 370 | } |
| 371 | } |
| 372 | } |
David K. Bainbridge | 9189c63 | 2021-03-26 21:52:21 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 375 | func NewConnection() (*grpc.ClientConn, error) { |
| 376 | ProcessGlobalOptions() |
serkant.uluderya | d3daa90 | 2020-05-21 22:49:25 -0700 | [diff] [blame] | 377 | |
| 378 | // convert grpc.msgSize into bytes |
David K. Bainbridge | 9189c63 | 2021-03-26 21:52:21 +0000 | [diff] [blame] | 379 | n, err := parseSize(GlobalConfig.Current().Grpc.MaxCallRecvMsgSize) |
serkant.uluderya | d3daa90 | 2020-05-21 22:49:25 -0700 | [diff] [blame] | 380 | if err != nil { |
David K. Bainbridge | 9189c63 | 2021-03-26 21:52:21 +0000 | [diff] [blame] | 381 | Error.Fatalf("Cannot convert msgSize %s to bytes", GlobalConfig.Current().Grpc.MaxCallRecvMsgSize) |
serkant.uluderya | d3daa90 | 2020-05-21 22:49:25 -0700 | [diff] [blame] | 382 | } |
| 383 | |
David K. Bainbridge | 1d94644 | 2021-03-19 16:45:52 +0000 | [diff] [blame] | 384 | var opts []grpc.DialOption |
| 385 | |
| 386 | opts = append(opts, |
| 387 | grpc.WithDisableRetry(), |
David K. Bainbridge | 1d94644 | 2021-03-19 16:45:52 +0000 | [diff] [blame] | 388 | grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(int(n)))) |
| 389 | |
David K. Bainbridge | 9189c63 | 2021-03-26 21:52:21 +0000 | [diff] [blame] | 390 | if GlobalConfig.Current().Tls.UseTls { |
David K. Bainbridge | 1d94644 | 2021-03-19 16:45:52 +0000 | [diff] [blame] | 391 | creds := credentials.NewTLS(&tls.Config{ |
David K. Bainbridge | 9189c63 | 2021-03-26 21:52:21 +0000 | [diff] [blame] | 392 | InsecureSkipVerify: !GlobalConfig.Current().Tls.Verify}) |
David K. Bainbridge | 1d94644 | 2021-03-19 16:45:52 +0000 | [diff] [blame] | 393 | opts = append(opts, grpc.WithTransportCredentials(creds)) |
| 394 | } else { |
| 395 | opts = append(opts, grpc.WithInsecure()) |
| 396 | } |
| 397 | ctx, cancel := context.WithTimeout(context.TODO(), |
David K. Bainbridge | 9189c63 | 2021-03-26 21:52:21 +0000 | [diff] [blame] | 398 | GlobalConfig.Current().Grpc.ConnectTimeout) |
David K. Bainbridge | 1d94644 | 2021-03-19 16:45:52 +0000 | [diff] [blame] | 399 | defer cancel() |
David K. Bainbridge | 9189c63 | 2021-03-26 21:52:21 +0000 | [diff] [blame] | 400 | return grpc.DialContext(ctx, GlobalConfig.Current().Server, opts...) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 403 | func ConvertJsonProtobufArray(data_in interface{}) (string, error) { |
| 404 | result := "" |
| 405 | |
| 406 | slice := reflect.ValueOf(data_in) |
| 407 | if slice.Kind() != reflect.Slice { |
| 408 | return "", errors.New("Not a slice") |
| 409 | } |
| 410 | |
| 411 | result = result + "[" |
| 412 | |
| 413 | marshaler := jsonpb.Marshaler{EmitDefaults: true} |
| 414 | for i := 0; i < slice.Len(); i++ { |
| 415 | item := slice.Index(i).Interface() |
| 416 | protoMessage, okay := item.(proto.Message) |
| 417 | if !okay { |
| 418 | return "", errors.New("Failed to convert item to a proto.Message") |
| 419 | } |
| 420 | asJson, err := marshaler.MarshalToString(protoMessage) |
| 421 | if err != nil { |
| 422 | return "", fmt.Errorf("Failed to marshal the json: %s", err) |
| 423 | } |
| 424 | |
| 425 | result = result + asJson |
| 426 | |
| 427 | if i < slice.Len()-1 { |
| 428 | result = result + "," |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | result = result + "]" |
| 433 | |
| 434 | return result, nil |
| 435 | } |
| 436 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 437 | func GenerateOutput(result *CommandResult) { |
| 438 | if result != nil && result.Data != nil { |
| 439 | data := result.Data |
| 440 | if result.Filter != "" { |
| 441 | f, err := filter.Parse(result.Filter) |
| 442 | if err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 443 | Error.Fatalf("Unable to parse specified output filter '%s': %s", result.Filter, err.Error()) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 444 | } |
| 445 | data, err = f.Process(data) |
| 446 | if err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 447 | Error.Fatalf("Unexpected error while filtering command results: %s", err.Error()) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | if result.OrderBy != "" { |
| 451 | s, err := order.Parse(result.OrderBy) |
| 452 | if err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 453 | Error.Fatalf("Unable to parse specified sort specification '%s': %s", result.OrderBy, err.Error()) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 454 | } |
| 455 | data, err = s.Process(data) |
| 456 | if err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 457 | Error.Fatalf("Unexpected error while sorting command result: %s", err.Error()) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | if result.OutputAs == OUTPUT_TABLE { |
| 461 | tableFormat := format.Format(result.Format) |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame] | 462 | if err := tableFormat.Execute(os.Stdout, true, result.NameLimit, data); err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 463 | Error.Fatalf("Unexpected error while attempting to format results as table : %s", err.Error()) |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame] | 464 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 465 | } else if result.OutputAs == OUTPUT_JSON { |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 466 | // first try to convert it as an array of protobufs |
| 467 | asJson, err := ConvertJsonProtobufArray(data) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 468 | if err != nil { |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 469 | // if that fails, then just do a standard json conversion |
| 470 | asJsonB, err := json.Marshal(&data) |
| 471 | if err != nil { |
| 472 | Error.Fatalf("Unexpected error while processing command results to JSON: %s", err.Error()) |
| 473 | } |
| 474 | asJson = string(asJsonB) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 475 | } |
| 476 | fmt.Printf("%s", asJson) |
| 477 | } else if result.OutputAs == OUTPUT_YAML { |
| 478 | asYaml, err := yaml.Marshal(&data) |
| 479 | if err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 480 | Error.Fatalf("Unexpected error while processing command results to YAML: %s", err.Error()) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 481 | } |
| 482 | fmt.Printf("%s", asYaml) |
| 483 | } |
| 484 | } |
| 485 | } |