-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroot.go
69 lines (56 loc) · 1.82 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
/*
SPDX-FileCopyrightText: 2024 Mercedes-Benz Tech Innovation GmbH
SPDX-License-Identifier: MIT
*/
package cmd
import (
"fmt"
"os"
"github.com/mercedes-benz/gitflow-cli/cmd/hotfix"
"github.com/mercedes-benz/gitflow-cli/cmd/release"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// Configuration file of the workflow automation command line tool.
var cfgFile string
// RootCmd represents the base command when called without any subcommands.
var rootCmd = &cobra.Command{
Args: cobra.NoArgs,
Use: "gitflow-cli",
}
// Execute adds all child commands to the root command and sets flags appropriately.
func Execute() {
if rootCmd.Execute() != nil {
os.Exit(1)
}
}
// Initialize Cobra flags and configuration settings.
func init() {
// sets the passed functions to be run when each command's Execute method is called
cobra.OnInitialize(initConfiguration)
// add subcommands to the root command
rootCmd.AddCommand(release.ReleaseCmd, hotfix.HotfixCmd)
// persistent flags, which, if defined here, will be global for the application
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.gitflow-cli.yaml)")
}
// Read in Viper config file and environment variables if set.
func initConfiguration() {
if cfgFile != "" {
// use config file from the flag
viper.SetConfigFile(cfgFile)
} else {
// find home directory
home, err := os.UserHomeDir()
cobra.CheckErr(err)
// search config in home directory with name ".gitflow-cli" (without extension)
viper.AddConfigPath(home)
viper.SetConfigType("yaml")
viper.SetConfigName(".gitflow-cli")
}
// read in environment variables that match
viper.AutomaticEnv()
// if a config file is found, read it in
if err := viper.ReadInConfig(); err == nil {
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
}
}