blob: c57c182d0a9b6e3af1731bc7c0824fc3007b05be [file] [log] [blame]
Scott Bakerbdb962b2020-04-03 10:53:36 -07001/*
2 * Copyright 2018-present Open Networking Foundation
Dinesh Belwalkar6c0bc752020-04-24 23:47:53 +00003 * Copyright 2018-present Edgecore Networks Corporation
Scott Bakerbdb962b2020-04-03 10:53:36 -07004 *
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// Implements global configuration for redfish importer
19package main
20
21import (
22 "fmt"
23 flags "github.com/jessevdk/go-flags"
24 "gopkg.in/yaml.v2"
25 "io/ioutil"
26 "log"
27 "os"
28 "path"
29 "path/filepath"
30 "strings"
31)
32
33type OutputType uint8
34
35type GlobalConfigSpec struct {
36 Kafka string `yaml:"kafka"`
37 Local string `yaml:"local"`
38 LocalGrpc string `yaml:"localgrpc"`
39}
40
41var (
42 CharReplacer = strings.NewReplacer("\\t", "\t", "\\n", "\n")
43
44 GlobalConfig = GlobalConfigSpec{
45 Kafka: "cord-kafka.default.svc.cluster.local:9092",
46 Local: "0.0.0.0:8080",
47 LocalGrpc: "0.0.0.0:50051",
48 }
49
50 GlobalCommandOptions = make(map[string]map[string]string)
51
52 GlobalOptions struct {
53 Config string `short:"c" long:"config" env:"PROXYCONFIG" value-name:"FILE" default:"" description:"Location of proxy config file"`
54 Kafka string `short:"k" long:"kafka" default:"" value-name:"SERVER:PORT" description:"IP/Host and port of Kafka"`
55 Local string `short:"l" long:"local" default:"" value-name:"SERVER:PORT" description:"IP/Host and port to listen on for http"`
56 LocalGrpc string `short:"g" long:"localgrpc" default:"" value-name:"SERVER:PORT" description:"IP/Host and port to listen on for grpc"`
57 }
58
59 Debug = log.New(os.Stdout, "DEBUG: ", 0)
60 Info = log.New(os.Stdout, "INFO: ", 0)
61 Warn = log.New(os.Stderr, "WARN: ", 0)
62 Error = log.New(os.Stderr, "ERROR: ", 0)
63)
64
65func ParseCommandLine() {
66 parser := flags.NewNamedParser(path.Base(os.Args[0]),
67 flags.HelpFlag|flags.PassDoubleDash|flags.PassAfterNonOption)
68 _, err := parser.AddGroup("Global Options", "", &GlobalOptions)
69 if err != nil {
70 panic(err)
71 }
72
73 _, err = parser.ParseArgs(os.Args[1:])
74 if err != nil {
75 _, ok := err.(*flags.Error)
76 if ok {
77 real := err.(*flags.Error)
78 if real.Type == flags.ErrHelp {
79 os.Stdout.WriteString(err.Error() + "\n")
80 os.Exit(0)
81 }
82 }
83
84 fmt.Fprintf(os.Stderr, "%s: %s\n", os.Args[0], err.Error())
85
86 os.Exit(1)
87 }
88}
89
90func ProcessGlobalOptions() {
91 if len(GlobalOptions.Config) == 0 {
92 home, err := os.UserHomeDir()
93 if err != nil {
94 Warn.Printf("Unable to discover the user's home directory: %s", err)
95 home = "~"
96 }
97 GlobalOptions.Config = filepath.Join(home, ".redfish-importer", "config")
98 }
99
100 if info, err := os.Stat(GlobalOptions.Config); err == nil && !info.IsDir() {
101 configFile, err := ioutil.ReadFile(GlobalOptions.Config)
102 if err != nil {
103 Error.Fatalf("Unable to read the configuration file '%s': %s",
104 GlobalOptions.Config, err.Error())
105 }
106 if err = yaml.Unmarshal(configFile, &GlobalConfig); err != nil {
107 Error.Fatalf("Unable to parse the configuration file '%s': %s",
108 GlobalOptions.Config, err.Error())
109 }
110 }
111
112 if GlobalOptions.Kafka != "" {
113 GlobalConfig.Kafka = GlobalOptions.Kafka
114 }
115 if GlobalOptions.Local != "" {
116 GlobalConfig.Local = GlobalOptions.Local
117 }
118 if GlobalOptions.LocalGrpc != "" {
119 GlobalConfig.LocalGrpc = GlobalOptions.LocalGrpc
120 }
121}
122
123func ShowGlobalOptions() {
124 log.Printf("Configuration:")
125 log.Printf(" Kafka: %v", GlobalConfig.Kafka)
126 log.Printf(" Listen Address: %v", GlobalConfig.Local)
127 log.Printf(" Grpc Listen Address: %v", GlobalConfig.LocalGrpc)
128}