Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 1 | /* |
Joey Armstrong | 3881b73 | 2022-12-27 07:55:37 -0500 | [diff] [blame] | 2 | * Portions Copyright 2019-2023 Open Networking Foundation (ONF) and the ONF Contributors |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 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 | |
| 18 | package config |
| 19 | |
| 20 | import ( |
Matteo Scandolo | 3830549 | 2019-10-11 11:36:00 -0700 | [diff] [blame] | 21 | "fmt" |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 22 | log "github.com/sirupsen/logrus" |
Matteo Scandolo | 3830549 | 2019-10-11 11:36:00 -0700 | [diff] [blame] | 23 | "gopkg.in/yaml.v2" |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 24 | "io/ioutil" |
| 25 | "net" |
| 26 | "os" |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 27 | "time" |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | var ( |
Matteo Scandolo | 3830549 | 2019-10-11 11:36:00 -0700 | [diff] [blame] | 31 | Version string |
| 32 | BuildTime string |
| 33 | CommitHash string |
| 34 | GitStatus string |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 35 | ) |
| 36 | |
| 37 | var GlobalOptions struct { |
Matteo Scandolo | 3830549 | 2019-10-11 11:36:00 -0700 | [diff] [blame] | 38 | Config string `short:"c" long:"config" env:"BBSIMCTL_CONFIG" value-name:"FILE" default:"" description:"Location of client config file"` |
| 39 | Server string `short:"s" long:"server" default:"" value-name:"SERVER:PORT" description:"IP/Host and port of XOS"` |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 40 | //Protoset string `long:"protoset" value-name:"FILENAME" description:"Load protobuf definitions from protoset instead of reflection api"` |
Matteo Scandolo | 3830549 | 2019-10-11 11:36:00 -0700 | [diff] [blame] | 41 | Debug bool `short:"d" long:"debug" description:"Enable debug mode"` |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | type GrpcConfigSpec struct { |
| 45 | Timeout time.Duration `yaml:"timeout"` |
| 46 | } |
| 47 | |
| 48 | type GlobalConfigSpec struct { |
Matteo Scandolo | 3830549 | 2019-10-11 11:36:00 -0700 | [diff] [blame] | 49 | Server string `yaml:"server"` |
| 50 | Grpc GrpcConfigSpec |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | var GlobalConfig = GlobalConfigSpec{ |
| 54 | Server: "localhost:50070", |
| 55 | Grpc: GrpcConfigSpec{ |
| 56 | Timeout: time.Second * 10, |
| 57 | }, |
| 58 | } |
| 59 | |
hkouser | 24361d4 | 2020-12-14 19:21:47 +0530 | [diff] [blame] | 60 | var DmiConfig = GlobalConfigSpec{ |
| 61 | Server: "localhost:50075", |
| 62 | Grpc: GrpcConfigSpec{ |
| 63 | Timeout: time.Second * 10, |
| 64 | }, |
| 65 | } |
| 66 | |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 67 | func ProcessGlobalOptions() { |
| 68 | if len(GlobalOptions.Config) == 0 { |
| 69 | home, err := os.UserHomeDir() |
| 70 | if err != nil { |
| 71 | log.Printf("Unable to discover the users home directory: %s\n", err) |
| 72 | } |
| 73 | GlobalOptions.Config = fmt.Sprintf("%s/.bbsim/config", home) |
| 74 | } |
| 75 | |
| 76 | info, err := os.Stat(GlobalOptions.Config) |
| 77 | if err == nil && !info.IsDir() { |
| 78 | configFile, err := ioutil.ReadFile(GlobalOptions.Config) |
| 79 | if err != nil { |
| 80 | log.Printf("configFile.Get err #%v ", err) |
| 81 | } |
| 82 | err = yaml.Unmarshal(configFile, &GlobalConfig) |
| 83 | if err != nil { |
| 84 | log.Fatalf("Unmarshal: %v", err) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // Override from environment |
| 89 | // in particualr, for passing env vars via `go test` |
| 90 | env_server, present := os.LookupEnv("BBSIMCTL_SERVER") |
| 91 | if present { |
| 92 | GlobalConfig.Server = env_server |
| 93 | } |
| 94 | |
| 95 | // Override from command line |
| 96 | if GlobalOptions.Server != "" { |
| 97 | GlobalConfig.Server = GlobalOptions.Server |
| 98 | } |
| 99 | |
Matteo Scandolo | 8df63df | 2019-09-12 10:34:32 -0700 | [diff] [blame] | 100 | // Generate error messages for required settings |
| 101 | if GlobalConfig.Server == "" { |
| 102 | log.Fatal("Server is not set. Please update config file or use the -s option") |
| 103 | } |
| 104 | |
| 105 | //Try to resolve hostname if provided for the server |
| 106 | if host, port, err := net.SplitHostPort(GlobalConfig.Server); err == nil { |
| 107 | if addrs, err := net.LookupHost(host); err == nil { |
| 108 | GlobalConfig.Server = net.JoinHostPort(addrs[0], port) |
| 109 | } |
| 110 | } |
Matteo Scandolo | 3830549 | 2019-10-11 11:36:00 -0700 | [diff] [blame] | 111 | } |