-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
51 lines (42 loc) · 1.23 KB
/
root.go
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
package cmd
import (
"github.com/pkg/errors"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var cfgFile string
//NewRoot is for creating commandline
func NewRoot() *cobra.Command {
var rootCmd = &cobra.Command{
Use: "app",
Short: "app is a birthday application",
Long: `
A Fast and Flexible Site
that helps you to remind remaining days
http://www.appholiday.com`,
Run: func(cmd *cobra.Command, args []string) {},
}
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.otsample.yaml)")
return rootCmd
}
func initConfig() {
// Don't forget to read config either from cfgFile or from home directory!
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
panic(errors.Wrap(err, "could not get HOME dir:"))
}
// Search config in home directory with name ".otsample" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName(".otsample")
}
if err := viper.ReadInConfig(); err != nil {
panic(errors.Wrap(err, "Can't read config:"))
}
}