Scott Baker | ce11c49 | 2019-07-30 15:53:34 -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 config |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "gopkg.in/yaml.v2" |
| 22 | "io/ioutil" |
| 23 | "log" |
| 24 | "net" |
| 25 | "os" |
| 26 | "strings" |
| 27 | "time" |
| 28 | ) |
| 29 | |
| 30 | type OutputType uint8 |
| 31 | |
| 32 | const ( |
| 33 | OUTPUT_TABLE OutputType = iota |
| 34 | OUTPUT_JSON |
| 35 | OUTPUT_YAML |
| 36 | |
| 37 | CORE_VERSION_CONSTRAINT = ">= 3, < 4" // Support XOS major version 3 |
| 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 { |
| 55 | Server string `yaml:"server"` |
| 56 | Username string `yaml:"username"` |
| 57 | Password string `yaml:"password"` |
| 58 | Protoset string `yaml:"protoset"` |
| 59 | Tls TlsConfigSpec `yaml:"tls"` |
| 60 | Grpc GrpcConfigSpec |
| 61 | } |
| 62 | |
| 63 | var GlobalConfig = GlobalConfigSpec{ |
| 64 | Server: "localhost", |
| 65 | Tls: TlsConfigSpec{ |
| 66 | UseTls: false, |
| 67 | }, |
| 68 | Grpc: GrpcConfigSpec{ |
| 69 | Timeout: time.Second * 10, |
| 70 | }, |
| 71 | } |
| 72 | |
| 73 | var GlobalOptions struct { |
| 74 | Config string `short:"c" long:"config" env:"CORDCONFIG" value-name:"FILE" default:"" description:"Location of client config file"` |
| 75 | Server string `short:"s" long:"server" default:"" value-name:"SERVER:PORT" description:"IP/Host and port of XOS"` |
| 76 | Username string `short:"u" long:"username" value-name:"USERNAME" default:"" description:"Username to authenticate with XOS"` |
| 77 | Password string `short:"p" long:"password" value-name:"PASSWORD" default:"" description:"Password to authenticate with XOS"` |
| 78 | Protoset string `long:"protoset" value-name:"FILENAME" description:"Load protobuf definitions from protoset instead of reflection api"` |
| 79 | Debug bool `short:"d" long:"debug" description:"Enable debug mode"` |
| 80 | UseTLS bool `long:"tls" description:"Use TLS"` |
| 81 | CACert string `long:"tlscacert" value-name:"CA_CERT_FILE" description:"Trust certs signed only by this CA"` |
| 82 | Cert string `long:"tlscert" value-name:"CERT_FILE" description:"Path to TLS vertificate file"` |
| 83 | Key string `long:"tlskey" value-name:"KEY_FILE" description:"Path to TLS key file"` |
| 84 | Verify bool `long:"tlsverify" description:"Use TLS and verify the remote"` |
| 85 | Yes bool `short:"y" long:"yes" description:"answer yes to any confirmation prompts"` |
| 86 | } |
| 87 | |
| 88 | func ProcessGlobalOptions() { |
| 89 | if len(GlobalOptions.Config) == 0 { |
| 90 | home, err := os.UserHomeDir() |
| 91 | if err != nil { |
| 92 | log.Printf("Unable to discover the users home directory: %s\n", err) |
| 93 | } |
| 94 | GlobalOptions.Config = fmt.Sprintf("%s/.cord/config", home) |
| 95 | } |
| 96 | |
| 97 | info, err := os.Stat(GlobalOptions.Config) |
| 98 | if err == nil && !info.IsDir() { |
| 99 | configFile, err := ioutil.ReadFile(GlobalOptions.Config) |
| 100 | if err != nil { |
| 101 | log.Printf("configFile.Get err #%v ", err) |
| 102 | } |
| 103 | err = yaml.Unmarshal(configFile, &GlobalConfig) |
| 104 | if err != nil { |
| 105 | log.Fatalf("Unmarshal: %v", err) |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // Override from environment |
| 110 | // in particualr, for passing env vars via `go test` |
| 111 | env_server, present := os.LookupEnv("CORDCTL_SERVER") |
| 112 | if present { |
| 113 | GlobalConfig.Server = env_server |
| 114 | } |
| 115 | env_username, present := os.LookupEnv("CORDCTL_USERNAME") |
| 116 | if present { |
| 117 | GlobalConfig.Username = env_username |
| 118 | } |
| 119 | env_password, present := os.LookupEnv("CORDCTL_PASSWORD") |
| 120 | if present { |
| 121 | GlobalConfig.Password = env_password |
| 122 | } |
| 123 | env_protoset, present := os.LookupEnv("CORDCTL_PROTOSET") |
| 124 | if present { |
| 125 | GlobalConfig.Protoset = env_protoset |
| 126 | } |
| 127 | |
| 128 | // Override from command line |
| 129 | if GlobalOptions.Server != "" { |
| 130 | GlobalConfig.Server = GlobalOptions.Server |
| 131 | } |
| 132 | if GlobalOptions.Username != "" { |
| 133 | GlobalConfig.Username = GlobalOptions.Username |
| 134 | } |
| 135 | if GlobalOptions.Password != "" { |
| 136 | GlobalConfig.Password = GlobalOptions.Password |
| 137 | } |
| 138 | if GlobalOptions.Protoset != "" { |
| 139 | GlobalConfig.Protoset = GlobalOptions.Protoset |
| 140 | } |
| 141 | |
| 142 | // Generate error messages for required settings |
| 143 | if GlobalConfig.Server == "" { |
| 144 | log.Fatal("Server is not set. Please update config file or use the -s option") |
| 145 | } |
| 146 | if GlobalConfig.Username == "" { |
| 147 | log.Fatal("Username is not set. Please update config file or use the -u option") |
| 148 | } |
| 149 | if GlobalConfig.Password == "" { |
| 150 | log.Fatal("Password is not set. Please update config file or use the -p option") |
| 151 | } |
| 152 | //Try to resolve hostname if provided for the server |
| 153 | if host, port, err := net.SplitHostPort(GlobalConfig.Server); err == nil { |
| 154 | if addrs, err := net.LookupHost(host); err == nil { |
| 155 | GlobalConfig.Server = net.JoinHostPort(addrs[0], port) |
| 156 | } |
| 157 | } |
| 158 | } |