Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020-present Open Networking Foundation |
| 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 | |
| 17 | package utils |
| 18 | |
| 19 | import ( |
| 20 | "flag" |
| 21 | "fmt" |
| 22 | "github.com/opencord/voltha-lib-go/v4/pkg/log" |
| 23 | ) |
| 24 | |
| 25 | const ( |
| 26 | defaultLogLevel = "WARN" |
| 27 | defaultLogFormat = "json" // or "console" |
| 28 | defaultBBsimSadisPort = 50074 |
| 29 | ) |
| 30 | |
| 31 | type ConfigFlags struct { |
| 32 | LogLevel string |
| 33 | LogFormat string |
| 34 | Kubeconfig string |
| 35 | BBsimSadisPort int |
| 36 | } |
| 37 | |
| 38 | func NewConfigFlags() *ConfigFlags { |
| 39 | flags := &ConfigFlags{ |
| 40 | LogLevel: defaultLogLevel, |
| 41 | LogFormat: defaultLogFormat, |
| 42 | Kubeconfig: "", |
| 43 | BBsimSadisPort: defaultBBsimSadisPort, |
| 44 | } |
| 45 | return flags |
| 46 | } |
| 47 | |
| 48 | func (cf *ConfigFlags) ParseCommandArguments() { |
| 49 | help := fmt.Sprintf("Log level (debug, infor, warn, error)") |
| 50 | flag.StringVar(&(cf.LogLevel), "log_level", defaultLogLevel, help) |
| 51 | |
| 52 | help = fmt.Sprintf("Log format (json or console) ") |
| 53 | logFormat := flag.String("log_format", defaultLogFormat, help) |
| 54 | |
| 55 | flag.StringVar(&(cf.Kubeconfig), "kubeconfig", "", "Absolute path to the kubeconfig file") |
| 56 | flag.IntVar(&(cf.BBsimSadisPort), "bbsim_sadis_port", defaultBBsimSadisPort, "The port on which BBSim exposes the Sadis server") |
| 57 | |
| 58 | flag.Parse() |
| 59 | |
| 60 | if *logFormat != log.CONSOLE && *logFormat != log.JSON { |
| 61 | panic(fmt.Sprintf("log_format is invalid, allowed values are: %s, %s", log.JSON, log.CONSOLE)) |
| 62 | } |
| 63 | |
| 64 | cf.LogFormat = *logFormat |
| 65 | } |