Skip to content

Commit 8ae3320

Browse files
author
R0n0066
committed
feature (cmd): Add root command
1 parent 7b2ab7e commit 8ae3320

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

cmd/root.go

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,24 @@
2121
package cmd
2222

2323
import (
24-
"fmt"
2524
"os"
2625

2726
homedir "github.com/mitchellh/go-homedir"
2827
"github.com/spf13/cobra"
2928
"github.com/spf13/viper"
3029
)
3130

31+
type option struct {
32+
Value string
33+
Desc string
34+
}
35+
type keyValue struct {
36+
Key string
37+
Value string
38+
}
39+
3240
var cfgFile string
41+
var variables []keyValue
3342

3443
// rootCmd represents the base command when called without any subcommands
3544
var rootCmd = &cobra.Command{
@@ -41,14 +50,17 @@ Generate a formated message for your repo using common notations for:
4150
- Fixes
4251
- Refactoring
4352
- Tests`,
44-
Run: func(cmd *cobra.Command, args []string) {},
53+
PreRun: func(cmd *cobra.Command, args []string) { promptList() },
54+
Run: func(cmd *cobra.Command, args []string) {
55+
p := parseTemplate(viper.GetString("template"))
56+
commit(p)
57+
},
4558
}
4659

4760
// Execute adds all child commands to the root command and sets flags appropriately.
4861
// This is called by main.main(). It only needs to happen once to the rootCmd.
4962
func Execute() {
5063
if err := rootCmd.Execute(); err != nil {
51-
fmt.Println(err)
5264
os.Exit(1)
5365
}
5466
}
@@ -58,33 +70,47 @@ func init() {
5870
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cfm.yaml)")
5971
}
6072

73+
func loadLocalConfigFile(name string) error {
74+
viper.AddConfigPath("./")
75+
viper.SetConfigType("yaml")
76+
viper.SetConfigName(name)
77+
return viper.ReadInConfig()
78+
}
79+
6180
// initConfig reads in config file and ENV variables if set.
6281
func initConfig() {
63-
projectDir, _ := os.Getwd()
82+
err := loadLocalConfigFile("default")
83+
checkErr(err)
84+
85+
projectDir, err := os.Getwd()
86+
checkErr(err)
6487
projectConfigFile := projectDir + "/.cfm.yaml"
88+
6589
if _, err := os.Stat(projectConfigFile); err == nil {
6690
cfgFile = projectConfigFile
6791
}
92+
6893
if cfgFile != "" {
6994
// Use config file from the flag.
7095
viper.SetConfigFile(cfgFile)
7196
} else {
7297
// Find home directory.
7398
home, err := homedir.Dir()
74-
if err != nil {
75-
fmt.Println(err)
76-
os.Exit(1)
77-
}
99+
checkErr(err)
78100

79101
// Search config in home directory with name ".cfm" (without extension).
80102
viper.AddConfigPath(home)
81103
viper.SetConfigName(".cfm")
82104
}
83105

84-
viper.AutomaticEnv() // read in environment variables that match
106+
viper.AutomaticEnv()
85107

86108
// If a config file is found, read it in.
87109
if err := viper.ReadInConfig(); err == nil {
88-
fmt.Println("Using config file:", viper.ConfigFileUsed())
110+
defaultFlow := viper.GetString("default")
111+
if defaultFlow != "" {
112+
err := loadLocalConfigFile(defaultFlow)
113+
checkErr(err)
114+
}
89115
}
90116
}

0 commit comments

Comments
 (0)