2022-03-27 04:09:48 -04:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-04-14 01:41:03 -04:00
|
|
|
"os"
|
|
|
|
|
2022-03-27 04:09:48 -04:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
var cfgFile string
|
|
|
|
|
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
Use: "paisa",
|
|
|
|
Short: "A command line tool to manager personal finance",
|
|
|
|
}
|
|
|
|
|
|
|
|
func Execute() {
|
|
|
|
err := rootCmd.Execute()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cobra.OnInitialize(initConfig)
|
|
|
|
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is ./paisa.yaml)")
|
|
|
|
}
|
|
|
|
|
|
|
|
func initConfig() {
|
|
|
|
log.SetFormatter(&log.TextFormatter{DisableTimestamp: true})
|
|
|
|
|
2022-04-14 01:41:03 -04:00
|
|
|
currentCommand, _, _ := rootCmd.Find(os.Args[1:])
|
|
|
|
if currentCommand.Name() == "init" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-27 04:09:48 -04:00
|
|
|
if cfgFile != "" {
|
|
|
|
viper.SetConfigFile(cfgFile)
|
|
|
|
} else {
|
|
|
|
|
|
|
|
viper.SetConfigName("paisa.yaml")
|
|
|
|
viper.SetConfigType("yaml")
|
|
|
|
viper.AddConfigPath(".")
|
|
|
|
}
|
|
|
|
|
|
|
|
err := viper.ReadInConfig()
|
|
|
|
if err == nil {
|
|
|
|
log.Info("Using config file: ", viper.ConfigFileUsed())
|
|
|
|
} else {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|