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