Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Portions copyright 2019-present Open Networking Foundation |
| 3 | * Original copyright 2019-present Ciena Corporation |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | package commands |
| 18 | |
| 19 | import ( |
| 20 | "encoding/json" |
| 21 | "fmt" |
| 22 | "github.com/opencord/cordctl/format" |
| 23 | "google.golang.org/grpc" |
| 24 | "gopkg.in/yaml.v2" |
| 25 | "io/ioutil" |
| 26 | "log" |
| 27 | "os" |
| 28 | "strings" |
| 29 | "time" |
| 30 | ) |
| 31 | |
| 32 | type OutputType uint8 |
| 33 | |
| 34 | const ( |
| 35 | OUTPUT_TABLE OutputType = iota |
| 36 | OUTPUT_JSON |
| 37 | OUTPUT_YAML |
| 38 | ) |
| 39 | |
| 40 | var CharReplacer = strings.NewReplacer("\\t", "\t", "\\n", "\n") |
| 41 | |
| 42 | type GrpcConfigSpec struct { |
| 43 | Timeout time.Duration `yaml:"timeout"` |
| 44 | } |
| 45 | |
| 46 | type TlsConfigSpec struct { |
| 47 | UseTls bool `yaml:"useTls"` |
| 48 | CACert string `yaml:"caCert"` |
| 49 | Cert string `yaml:"cert"` |
| 50 | Key string `yaml:"key"` |
| 51 | Verify string `yaml:"verify"` |
| 52 | } |
| 53 | |
| 54 | type GlobalConfigSpec struct { |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 55 | Server string `yaml:"server"` |
| 56 | Username string `yaml:"username"` |
| 57 | Password string `yaml:"password"` |
| 58 | Tls TlsConfigSpec `yaml:"tls"` |
| 59 | Grpc GrpcConfigSpec |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | var GlobalConfig = GlobalConfigSpec{ |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 63 | Server: "localhost", |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 64 | Tls: TlsConfigSpec{ |
| 65 | UseTls: false, |
| 66 | }, |
| 67 | Grpc: GrpcConfigSpec{ |
| 68 | Timeout: time.Second * 10, |
| 69 | }, |
| 70 | } |
| 71 | |
| 72 | var GlobalOptions struct { |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 73 | Config string `short:"c" long:"config" env:"CORDCONFIG" value-name:"FILE" default:"" description:"Location of client config file"` |
| 74 | Server string `short:"s" long:"server" default:"" value-name:"SERVER:PORT" description:"IP/Host and port of XOS"` |
| 75 | Username string `short:"u" long:"username" value-name:"USERNAME" default:"" description:"Username to authenticate with XOS"` |
| 76 | Password string `short:"p" long:"password" value-name:"PASSWORD" default:"" description:"Password to authenticate with XOS"` |
| 77 | Debug bool `short:"d" long:"debug" description:"Enable debug mode"` |
| 78 | UseTLS bool `long:"tls" description:"Use TLS"` |
| 79 | CACert string `long:"tlscacert" value-name:"CA_CERT_FILE" description:"Trust certs signed only by this CA"` |
| 80 | Cert string `long:"tlscert" value-name:"CERT_FILE" description:"Path to TLS vertificate file"` |
| 81 | Key string `long:"tlskey" value-name:"KEY_FILE" description:"Path to TLS key file"` |
| 82 | Verify bool `long:"tlsverify" description:"Use TLS and verify the remote"` |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | type OutputOptions struct { |
| 86 | Format string `long:"format" value-name:"FORMAT" default:"" description:"Format to use to output structured data"` |
| 87 | Quiet bool `short:"q" long:"quiet" description:"Output only the IDs of the objects"` |
| 88 | OutputAs string `short:"o" long:"outputas" default:"table" choice:"table" choice:"json" choice:"yaml" description:"Type of output to generate"` |
| 89 | } |
| 90 | |
| 91 | func toOutputType(in string) OutputType { |
| 92 | switch in { |
| 93 | case "table": |
| 94 | fallthrough |
| 95 | default: |
| 96 | return OUTPUT_TABLE |
| 97 | case "json": |
| 98 | return OUTPUT_JSON |
| 99 | case "yaml": |
| 100 | return OUTPUT_YAML |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | type CommandResult struct { |
| 105 | Format format.Format |
| 106 | OutputAs OutputType |
| 107 | Data interface{} |
| 108 | } |
| 109 | |
| 110 | type config struct { |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 111 | Server string `yaml:"server"` |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Scott Baker | 63ce82e | 2019-05-15 09:01:42 -0700 | [diff] [blame^] | 114 | func ProcessGlobalOptions() { |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 115 | if len(GlobalOptions.Config) == 0 { |
Scott Baker | 63ce82e | 2019-05-15 09:01:42 -0700 | [diff] [blame^] | 116 | home, err := os.UserHomeDir() |
| 117 | if err != nil { |
| 118 | log.Printf("Unable to discover the users home directory: %s\n", err) |
| 119 | } |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 120 | GlobalOptions.Config = fmt.Sprintf("%s/.cord/config", home) |
| 121 | } |
| 122 | |
| 123 | info, err := os.Stat(GlobalOptions.Config) |
| 124 | if err == nil && !info.IsDir() { |
| 125 | configFile, err := ioutil.ReadFile(GlobalOptions.Config) |
| 126 | if err != nil { |
| 127 | log.Printf("configFile.Get err #%v ", err) |
| 128 | } |
| 129 | err = yaml.Unmarshal(configFile, &GlobalConfig) |
| 130 | if err != nil { |
| 131 | log.Fatalf("Unmarshal: %v", err) |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // Override from command line |
| 136 | if GlobalOptions.Server != "" { |
| 137 | GlobalConfig.Server = GlobalOptions.Server |
| 138 | } |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 139 | if GlobalOptions.Username != "" { |
| 140 | GlobalConfig.Username = GlobalOptions.Username |
| 141 | } |
| 142 | if GlobalOptions.Password != "" { |
| 143 | GlobalConfig.Password = GlobalOptions.Password |
| 144 | } |
| 145 | |
Scott Baker | 63ce82e | 2019-05-15 09:01:42 -0700 | [diff] [blame^] | 146 | if GlobalConfig.Server == "" { |
| 147 | log.Fatal("Server is not set. Please update config file or use the -s option") |
| 148 | } |
| 149 | if GlobalConfig.Username == "" { |
| 150 | log.Fatal("Username is not set. Please update config file or use the -u option") |
| 151 | } |
| 152 | if GlobalConfig.Password == "" { |
| 153 | log.Fatal("Password is not set. Please update config file or use the -p option") |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | func NewConnection() (*grpc.ClientConn, error) { |
| 158 | ProcessGlobalOptions() |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 159 | return grpc.Dial(GlobalConfig.Server, grpc.WithInsecure()) |
| 160 | } |
| 161 | |
| 162 | func GenerateOutput(result *CommandResult) { |
| 163 | if result != nil && result.Data != nil { |
| 164 | if result.OutputAs == OUTPUT_TABLE { |
| 165 | tableFormat := format.Format(result.Format) |
| 166 | tableFormat.Execute(os.Stdout, true, result.Data) |
| 167 | } else if result.OutputAs == OUTPUT_JSON { |
| 168 | asJson, err := json.Marshal(&result.Data) |
| 169 | if err != nil { |
| 170 | panic(err) |
| 171 | } |
| 172 | fmt.Printf("%s", asJson) |
| 173 | } else if result.OutputAs == OUTPUT_YAML { |
| 174 | asYaml, err := yaml.Marshal(&result.Data) |
| 175 | if err != nil { |
| 176 | panic(err) |
| 177 | } |
| 178 | fmt.Printf("%s", asYaml) |
| 179 | } |
| 180 | } |
| 181 | } |