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 ( |
| 19 | "encoding/json" |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 20 | "errors" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 21 | "fmt" |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 22 | "github.com/golang/protobuf/jsonpb" |
| 23 | "github.com/golang/protobuf/proto" |
Scott Baker | 2b0ad65 | 2019-08-21 14:57:07 -0700 | [diff] [blame] | 24 | "github.com/opencord/voltctl/pkg/filter" |
| 25 | "github.com/opencord/voltctl/pkg/format" |
| 26 | "github.com/opencord/voltctl/pkg/order" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 27 | "google.golang.org/grpc" |
| 28 | "gopkg.in/yaml.v2" |
| 29 | "io/ioutil" |
| 30 | "log" |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame] | 31 | "net" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 32 | "os" |
| 33 | "path/filepath" |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 34 | "reflect" |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame] | 35 | "strconv" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 36 | "strings" |
| 37 | "time" |
| 38 | ) |
| 39 | |
| 40 | type OutputType uint8 |
| 41 | |
| 42 | const ( |
| 43 | OUTPUT_TABLE OutputType = iota |
| 44 | OUTPUT_JSON |
| 45 | OUTPUT_YAML |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame] | 46 | |
| 47 | defaultApiHost = "localhost" |
| 48 | defaultApiPort = 55555 |
| 49 | |
| 50 | defaultKafkaHost = "localhost" |
| 51 | defaultKafkaPort = 9092 |
| 52 | |
| 53 | supportedKvStoreType = "etcd" |
| 54 | defaultKvHost = "localhost" |
| 55 | defaultKvPort = 2379 |
| 56 | defaultKvTimeout = time.Second * 5 |
| 57 | |
| 58 | defaultGrpcTimeout = time.Minute * 5 |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 59 | ) |
| 60 | |
| 61 | type GrpcConfigSpec struct { |
| 62 | Timeout time.Duration `yaml:"timeout"` |
| 63 | } |
| 64 | |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame] | 65 | type KvStoreConfigSpec struct { |
| 66 | Timeout time.Duration `yaml:"timeout"` |
| 67 | } |
| 68 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 69 | type TlsConfigSpec struct { |
| 70 | UseTls bool `yaml:"useTls"` |
| 71 | CACert string `yaml:"caCert"` |
| 72 | Cert string `yaml:"cert"` |
| 73 | Key string `yaml:"key"` |
| 74 | Verify string `yaml:"verify"` |
| 75 | } |
| 76 | |
| 77 | type GlobalConfigSpec struct { |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame] | 78 | Server string `yaml:"server"` |
| 79 | Kafka string `yaml:"kafka"` |
| 80 | KvStore string `yaml:"kvstore"` |
| 81 | Tls TlsConfigSpec `yaml:"tls"` |
| 82 | Grpc GrpcConfigSpec `yaml:"grpc"` |
| 83 | KvStoreConfig KvStoreConfigSpec `yaml:"kvstoreconfig"` |
| 84 | K8sConfig string `yaml:"-"` |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | var ( |
| 88 | ParamNames = map[string]map[string]string{ |
| 89 | "v1": { |
| 90 | "ID": "voltha.ID", |
| 91 | }, |
| 92 | "v2": { |
| 93 | "ID": "common.ID", |
| 94 | }, |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 95 | "v3": { |
Dinesh Belwalkar | c9aa6d8 | 2020-03-04 15:22:17 -0800 | [diff] [blame] | 96 | "ID": "common.ID", |
| 97 | "port": "voltha.Port", |
| 98 | "ValueSpecifier": "common.ValueSpecifier", |
kesavand | 12cd8eb | 2020-01-20 22:25:22 -0500 | [diff] [blame] | 99 | }, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | CharReplacer = strings.NewReplacer("\\t", "\t", "\\n", "\n") |
| 103 | |
| 104 | GlobalConfig = GlobalConfigSpec{ |
Scott Baker | 80126ab | 2020-06-04 11:49:07 -0700 | [diff] [blame] | 105 | Server: "localhost:55555", |
| 106 | Kafka: "", |
| 107 | KvStore: "localhost:2379", |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 108 | Tls: TlsConfigSpec{ |
| 109 | UseTls: false, |
| 110 | }, |
| 111 | Grpc: GrpcConfigSpec{ |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame] | 112 | Timeout: defaultGrpcTimeout, |
| 113 | }, |
| 114 | KvStoreConfig: KvStoreConfigSpec{ |
| 115 | Timeout: defaultKvTimeout, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 116 | }, |
| 117 | } |
| 118 | |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 119 | GlobalCommandOptions = make(map[string]map[string]string) |
| 120 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 121 | GlobalOptions struct { |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame] | 122 | Config string `short:"c" long:"config" env:"VOLTCONFIG" value-name:"FILE" default:"" description:"Location of client config file"` |
| 123 | Server string `short:"s" long:"server" default:"" value-name:"SERVER:PORT" description:"IP/Host and port of VOLTHA"` |
| 124 | Kafka string `short:"k" long:"kafka" default:"" value-name:"SERVER:PORT" description:"IP/Host and port of Kafka"` |
| 125 | KvStore string `short:"e" long:"kvstore" env:"KVSTORE" value-name:"SERVER:PORT" description:"IP/Host and port of KV store (etcd)"` |
| 126 | |
David K. Bainbridge | 2b62761 | 2020-02-18 14:50:13 -0800 | [diff] [blame] | 127 | // nolint: staticcheck |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 128 | Debug bool `short:"d" long:"debug" description:"Enable debug mode"` |
David K. Bainbridge | 402f848 | 2020-02-26 17:14:46 -0800 | [diff] [blame] | 129 | Timeout string `short:"t" long:"timeout" description:"API call timeout duration" value-name:"DURATION" default:""` |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 130 | UseTLS bool `long:"tls" description:"Use TLS"` |
| 131 | CACert string `long:"tlscacert" value-name:"CA_CERT_FILE" description:"Trust certs signed only by this CA"` |
| 132 | Cert string `long:"tlscert" value-name:"CERT_FILE" description:"Path to TLS vertificate file"` |
| 133 | Key string `long:"tlskey" value-name:"KEY_FILE" description:"Path to TLS key file"` |
| 134 | Verify bool `long:"tlsverify" description:"Use TLS and verify the remote"` |
| 135 | K8sConfig string `short:"8" long:"k8sconfig" env:"KUBECONFIG" value-name:"FILE" default:"" description:"Location of Kubernetes config file"` |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame] | 136 | KvStoreTimeout string `long:"kvstoretimeout" env:"KVSTORE_TIMEOUT" value-name:"DURATION" default:"" description:"timeout for calls to KV store"` |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 137 | CommandOptions string `short:"o" long:"command-options" env:"VOLTCTL_COMMAND_OPTIONS" value-name:"FILE" default:"" description:"Location of command options default configuration file"` |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 138 | } |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 139 | |
| 140 | Debug = log.New(os.Stdout, "DEBUG: ", 0) |
| 141 | Info = log.New(os.Stdout, "INFO: ", 0) |
| 142 | Warn = log.New(os.Stderr, "WARN: ", 0) |
| 143 | Error = log.New(os.Stderr, "ERROR: ", 0) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 144 | ) |
| 145 | |
| 146 | type OutputOptions struct { |
David K. Bainbridge | 2b62761 | 2020-02-18 14:50:13 -0800 | [diff] [blame] | 147 | Format string `long:"format" value-name:"FORMAT" default:"" description:"Format to use to output structured data"` |
| 148 | Quiet bool `short:"q" long:"quiet" description:"Output only the IDs of the objects"` |
| 149 | // nolint: staticcheck |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 150 | OutputAs string `short:"o" long:"outputas" default:"table" choice:"table" choice:"json" choice:"yaml" description:"Type of output to generate"` |
| 151 | NameLimit int `short:"l" long:"namelimit" default:"-1" value-name:"LIMIT" description:"Limit the depth (length) in the table column name"` |
| 152 | } |
| 153 | |
| 154 | type ListOutputOptions struct { |
| 155 | OutputOptions |
| 156 | Filter string `short:"f" long:"filter" default:"" value-name:"FILTER" description:"Only display results that match filter"` |
| 157 | OrderBy string `short:"r" long:"orderby" default:"" value-name:"ORDER" description:"Specify the sort order of the results"` |
| 158 | } |
| 159 | |
| 160 | type OutputOptionsJson struct { |
David K. Bainbridge | 2b62761 | 2020-02-18 14:50:13 -0800 | [diff] [blame] | 161 | Format string `long:"format" value-name:"FORMAT" default:"" description:"Format to use to output structured data"` |
| 162 | Quiet bool `short:"q" long:"quiet" description:"Output only the IDs of the objects"` |
| 163 | // nolint: staticcheck |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 164 | OutputAs string `short:"o" long:"outputas" default:"json" choice:"table" choice:"json" choice:"yaml" description:"Type of output to generate"` |
| 165 | NameLimit int `short:"l" long:"namelimit" default:"-1" value-name:"LIMIT" description:"Limit the depth (length) in the table column name"` |
| 166 | } |
| 167 | |
| 168 | type ListOutputOptionsJson struct { |
| 169 | OutputOptionsJson |
| 170 | Filter string `short:"f" long:"filter" default:"" value-name:"FILTER" description:"Only display results that match filter"` |
| 171 | OrderBy string `short:"r" long:"orderby" default:"" value-name:"ORDER" description:"Specify the sort order of the results"` |
| 172 | } |
| 173 | |
| 174 | func toOutputType(in string) OutputType { |
| 175 | switch in { |
| 176 | case "table": |
| 177 | fallthrough |
| 178 | default: |
| 179 | return OUTPUT_TABLE |
| 180 | case "json": |
| 181 | return OUTPUT_JSON |
| 182 | case "yaml": |
| 183 | return OUTPUT_YAML |
| 184 | } |
| 185 | } |
| 186 | |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame] | 187 | func splitEndpoint(ep, defaultHost string, defaultPort int) (string, int, error) { |
| 188 | port := defaultPort |
| 189 | host, sPort, err := net.SplitHostPort(ep) |
| 190 | if err != nil { |
| 191 | if addrErr, ok := err.(*net.AddrError); ok { |
| 192 | if addrErr.Err != "missing port in address" { |
| 193 | return "", 0, err |
| 194 | } |
| 195 | host = ep |
| 196 | } else { |
| 197 | return "", 0, err |
| 198 | } |
| 199 | } else if len(strings.TrimSpace(sPort)) > 0 { |
| 200 | val, err := strconv.Atoi(sPort) |
| 201 | if err != nil { |
| 202 | return "", 0, err |
| 203 | } |
| 204 | port = val |
| 205 | } |
| 206 | if len(strings.TrimSpace(host)) == 0 { |
| 207 | host = defaultHost |
| 208 | } |
| 209 | return strings.Trim(host, "]["), port, nil |
| 210 | } |
| 211 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 212 | type CommandResult struct { |
| 213 | Format format.Format |
| 214 | Filter string |
| 215 | OrderBy string |
| 216 | OutputAs OutputType |
| 217 | NameLimit int |
| 218 | Data interface{} |
| 219 | } |
| 220 | |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 221 | func GetCommandOptionWithDefault(name, option, defaultValue string) string { |
| 222 | if cmd, ok := GlobalCommandOptions[name]; ok { |
| 223 | if val, ok := cmd[option]; ok { |
| 224 | return CharReplacer.Replace(val) |
| 225 | } |
| 226 | } |
| 227 | return defaultValue |
| 228 | } |
| 229 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 230 | func ProcessGlobalOptions() { |
| 231 | if len(GlobalOptions.Config) == 0 { |
| 232 | home, err := os.UserHomeDir() |
| 233 | if err != nil { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 234 | Warn.Printf("Unable to discover the user's home directory: %s", err) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 235 | home = "~" |
| 236 | } |
| 237 | GlobalOptions.Config = filepath.Join(home, ".volt", "config") |
| 238 | } |
| 239 | |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 240 | if info, err := os.Stat(GlobalOptions.Config); err == nil && !info.IsDir() { |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 241 | configFile, err := ioutil.ReadFile(GlobalOptions.Config) |
| 242 | if err != nil { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 243 | Error.Fatalf("Unable to read the configuration file '%s': %s", |
| 244 | GlobalOptions.Config, err.Error()) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 245 | } |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 246 | if err = yaml.Unmarshal(configFile, &GlobalConfig); err != nil { |
| 247 | Error.Fatalf("Unable to parse the configuration file '%s': %s", |
| 248 | GlobalOptions.Config, err.Error()) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
| 252 | // Override from command line |
| 253 | if GlobalOptions.Server != "" { |
| 254 | GlobalConfig.Server = GlobalOptions.Server |
| 255 | } |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame] | 256 | host, port, err := splitEndpoint(GlobalConfig.Server, defaultApiHost, defaultApiPort) |
| 257 | if err != nil { |
| 258 | Error.Fatalf("voltha API endport incorrectly specified '%s':%s", |
| 259 | GlobalConfig.Server, err) |
| 260 | } |
| 261 | GlobalConfig.Server = net.JoinHostPort(host, strconv.Itoa(port)) |
| 262 | |
Scott Baker | ed4efab | 2020-01-13 19:12:25 -0800 | [diff] [blame] | 263 | if GlobalOptions.Kafka != "" { |
| 264 | GlobalConfig.Kafka = GlobalOptions.Kafka |
| 265 | } |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame] | 266 | host, port, err = splitEndpoint(GlobalConfig.Kafka, defaultKafkaHost, defaultKafkaPort) |
| 267 | if err != nil { |
| 268 | Error.Fatalf("Kafka endport incorrectly specified '%s':%s", |
| 269 | GlobalConfig.Kafka, err) |
| 270 | } |
| 271 | GlobalConfig.Kafka = net.JoinHostPort(host, strconv.Itoa(port)) |
| 272 | |
| 273 | if GlobalOptions.KvStore != "" { |
| 274 | GlobalConfig.KvStore = GlobalOptions.KvStore |
| 275 | } |
| 276 | host, port, err = splitEndpoint(GlobalConfig.KvStore, defaultKvHost, defaultKvPort) |
| 277 | if err != nil { |
| 278 | Error.Fatalf("KV store endport incorrectly specified '%s':%s", |
| 279 | GlobalConfig.KvStore, err) |
| 280 | } |
| 281 | GlobalConfig.KvStore = net.JoinHostPort(host, strconv.Itoa(port)) |
| 282 | |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame] | 283 | if GlobalOptions.KvStoreTimeout != "" { |
| 284 | timeout, err := time.ParseDuration(GlobalOptions.KvStoreTimeout) |
| 285 | if err != nil { |
| 286 | Error.Fatalf("Unable to parse specified KV strore timeout duration '%s': %s", |
| 287 | GlobalOptions.KvStoreTimeout, err.Error()) |
| 288 | } |
| 289 | GlobalConfig.KvStoreConfig.Timeout = timeout |
| 290 | } |
| 291 | |
David K. Bainbridge | 402f848 | 2020-02-26 17:14:46 -0800 | [diff] [blame] | 292 | if GlobalOptions.Timeout != "" { |
| 293 | timeout, err := time.ParseDuration(GlobalOptions.Timeout) |
| 294 | if err != nil { |
| 295 | Error.Fatalf("Unable to parse specified timeout duration '%s': %s", |
| 296 | GlobalOptions.Timeout, err.Error()) |
| 297 | } |
| 298 | GlobalConfig.Grpc.Timeout = timeout |
| 299 | } |
| 300 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 301 | // If a k8s cert/key were not specified, then attempt to read it from |
| 302 | // any $HOME/.kube/config if it exists |
| 303 | if len(GlobalOptions.K8sConfig) == 0 { |
| 304 | home, err := os.UserHomeDir() |
| 305 | if err != nil { |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 306 | Warn.Printf("Unable to discover the user's home directory: %s", err) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 307 | home = "~" |
| 308 | } |
| 309 | GlobalOptions.K8sConfig = filepath.Join(home, ".kube", "config") |
| 310 | } |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 311 | |
| 312 | if len(GlobalOptions.CommandOptions) == 0 { |
| 313 | home, err := os.UserHomeDir() |
| 314 | if err != nil { |
| 315 | Warn.Printf("Unable to discover the user's home directory: %s", err) |
| 316 | home = "~" |
| 317 | } |
| 318 | GlobalOptions.CommandOptions = filepath.Join(home, ".volt", "command_options") |
| 319 | } |
| 320 | |
| 321 | if info, err := os.Stat(GlobalOptions.CommandOptions); err == nil && !info.IsDir() { |
| 322 | optionsFile, err := ioutil.ReadFile(GlobalOptions.CommandOptions) |
| 323 | if err != nil { |
| 324 | Error.Fatalf("Unable to read command options configuration file '%s' : %s", |
| 325 | GlobalOptions.CommandOptions, err.Error()) |
| 326 | } |
| 327 | if err = yaml.Unmarshal(optionsFile, &GlobalCommandOptions); err != nil { |
| 328 | Error.Fatalf("Unable to parse the command line options configuration file '%s': %s", |
| 329 | GlobalOptions.CommandOptions, err.Error()) |
| 330 | } |
| 331 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | func NewConnection() (*grpc.ClientConn, error) { |
| 335 | ProcessGlobalOptions() |
| 336 | return grpc.Dial(GlobalConfig.Server, grpc.WithInsecure()) |
| 337 | } |
| 338 | |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 339 | func ConvertJsonProtobufArray(data_in interface{}) (string, error) { |
| 340 | result := "" |
| 341 | |
| 342 | slice := reflect.ValueOf(data_in) |
| 343 | if slice.Kind() != reflect.Slice { |
| 344 | return "", errors.New("Not a slice") |
| 345 | } |
| 346 | |
| 347 | result = result + "[" |
| 348 | |
| 349 | marshaler := jsonpb.Marshaler{EmitDefaults: true} |
| 350 | for i := 0; i < slice.Len(); i++ { |
| 351 | item := slice.Index(i).Interface() |
| 352 | protoMessage, okay := item.(proto.Message) |
| 353 | if !okay { |
| 354 | return "", errors.New("Failed to convert item to a proto.Message") |
| 355 | } |
| 356 | asJson, err := marshaler.MarshalToString(protoMessage) |
| 357 | if err != nil { |
| 358 | return "", fmt.Errorf("Failed to marshal the json: %s", err) |
| 359 | } |
| 360 | |
| 361 | result = result + asJson |
| 362 | |
| 363 | if i < slice.Len()-1 { |
| 364 | result = result + "," |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | result = result + "]" |
| 369 | |
| 370 | return result, nil |
| 371 | } |
| 372 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 373 | func GenerateOutput(result *CommandResult) { |
| 374 | if result != nil && result.Data != nil { |
| 375 | data := result.Data |
| 376 | if result.Filter != "" { |
| 377 | f, err := filter.Parse(result.Filter) |
| 378 | if err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 379 | 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] | 380 | } |
| 381 | data, err = f.Process(data) |
| 382 | if err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 383 | Error.Fatalf("Unexpected error while filtering command results: %s", err.Error()) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | if result.OrderBy != "" { |
| 387 | s, err := order.Parse(result.OrderBy) |
| 388 | if err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 389 | 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] | 390 | } |
| 391 | data, err = s.Process(data) |
| 392 | if err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 393 | Error.Fatalf("Unexpected error while sorting command result: %s", err.Error()) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | if result.OutputAs == OUTPUT_TABLE { |
| 397 | tableFormat := format.Format(result.Format) |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame] | 398 | if err := tableFormat.Execute(os.Stdout, true, result.NameLimit, data); err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 399 | 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] | 400 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 401 | } else if result.OutputAs == OUTPUT_JSON { |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 402 | // first try to convert it as an array of protobufs |
| 403 | asJson, err := ConvertJsonProtobufArray(data) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 404 | if err != nil { |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 405 | // if that fails, then just do a standard json conversion |
| 406 | asJsonB, err := json.Marshal(&data) |
| 407 | if err != nil { |
| 408 | Error.Fatalf("Unexpected error while processing command results to JSON: %s", err.Error()) |
| 409 | } |
| 410 | asJson = string(asJsonB) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 411 | } |
| 412 | fmt.Printf("%s", asJson) |
| 413 | } else if result.OutputAs == OUTPUT_YAML { |
| 414 | asYaml, err := yaml.Marshal(&data) |
| 415 | if err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 416 | 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] | 417 | } |
| 418 | fmt.Printf("%s", asYaml) |
| 419 | } |
| 420 | } |
| 421 | } |