-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathroot.go
More file actions
127 lines (109 loc) · 4.46 KB
/
root.go
File metadata and controls
127 lines (109 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* === This file is part of ALICE O² ===
*
* Copyright 2018 CERN and copyright holders of ALICE O².
* Author: Teo Mrnjavac <teo.mrnjavac@cern.ch>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* In applying this license CERN does not waive the privileges and
* immunities granted to it by virtue of its status as an
* Intergovernmental Organization or submit itself to any jurisdiction.
*/
// Package cmd contains all the entry points for command line
// subcommands, following library convention.
package cmd
import (
"fmt"
"os"
"github.com/AliceO2Group/Control/coconut/app"
"github.com/AliceO2Group/Control/common/product"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"path"
"github.com/AliceO2Group/Control/common/logger"
"github.com/sirupsen/logrus"
)
var log = logger.New(logrus.StandardLogger(), app.NAME)
var cfgFile string
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: app.NAME,
Short: app.PRETTY_FULLNAME,
Long: fmt.Sprintf(`The %s is a command line program for interacting with the %s.`, app.PRETTY_FULLNAME, product.PRETTY_FULLNAME),
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
log.WithField("error", err).Fatal("cannot run command")
os.Exit(1)
}
}
func init() {
cobra.OnInitialize(initConfig)
viper.Set("version", product.VERSION)
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", fmt.Sprintf("configuration file (default $HOME/.config/%s/settings.yaml)", app.NAME))
rootCmd.PersistentFlags().String("endpoint", "127.0.0.1:47102", product.PRETTY_SHORTNAME + " endpoint as HOST:PORT")
rootCmd.PersistentFlags().String("config_endpoint", "consul://127.0.0.1:8500", "O² Configuration endpoint as PROTO://HOST:PORT")
rootCmd.PersistentFlags().BoolP("verbose", "v", false, "show verbose output for debug purposes")
viper.BindPFlag("endpoint", rootCmd.PersistentFlags().Lookup("endpoint"))
viper.BindPFlag("config_endpoint", rootCmd.PersistentFlags().Lookup("config_endpoint"))
viper.BindPFlag("verbose", rootCmd.PersistentFlags().Lookup("verbose"))
// Cobra also supports local flags, which will only run
// when this action is called directly.
//rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
viper.SetDefault("log.level", "info")
viper.SetDefault("endpoint", "127.0.0.1:47102")
viper.SetDefault("config_endpoint", "127.0.0.1:8500")
viper.SetDefault("verbose", false)
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
log.WithField("error", err).Error("cannot find configuration file")
os.Exit(1)
}
// Search config in .config/coconut directory with name "settings.yaml"
viper.AddConfigPath(path.Join(home, ".config/" + app.NAME))
viper.SetConfigName("settings")
}
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
logLevel, err := logrus.ParseLevel(viper.GetString("log.level"))
if err == nil {
logrus.SetLevel(logLevel)
}
log.WithField("file", viper.ConfigFileUsed()).
Debug("configuration loaded")
}
if viper.GetBool("verbose") {
viper.Set("log.level", "debug")
logrus.SetLevel(logrus.DebugLevel)
}
}