/
root.go
71 lines (60 loc) · 1.89 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package cmd
import (
goflag "flag"
"fmt"
"os"
"github.com/alok87/github-cli/pkg/ghub"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var gitSha string
var version string
var buildDate string
var gc = &ghub.Gclient{Name: "Github Client"}
var configFile string
// RootCmd is the github-cli root command.
var RootCmd = &cobra.Command{
Use: "github-cli",
Short: "Use github-cli to perform github tasks.",
Long: "Use github-cli to perform github tasks.",
}
func init() {
cobra.OnInitialize(initConfig)
RootCmd.PersistentFlags().AddGoFlagSet(goflag.CommandLine)
RootCmd.PersistentFlags().StringVar(&configFile, "config", "", "config file (default is $HOME/.github-cli.yaml)")
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if configFile != "" {
// enable ability to specify config file via flag
viper.SetConfigFile(configFile)
}
viper.SetConfigName(".github-cli") // name of config file (without extension)
viper.AddConfigPath("$HOME") // adding home directory as first search path
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
// Print config file name based on `show_vuper_config_file`.
if viper.IsSet("show_viper_config_file") {
if viper.GetBool("show_viper_config_file") {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
} else {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}
}
// Execute runs the root command.
func Execute() {
goflag.Set("logtostderr", "true")
goflag.CommandLine.Parse([]string{})
if err := RootCmd.Execute(); err != nil {
exitWithError(err)
}
}
// exitWithError will terminate execution with an error result
// It prints the error to stderr and exits with a non-zero exit code
func exitWithError(err error) {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}