53 lines
834 B
Go
53 lines
834 B
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/mitchellh/go-homedir"
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
var (
|
|
// Used for flags.
|
|
cfgFile string
|
|
userLicense string
|
|
|
|
rootCmd = &cobra.Command{
|
|
Use: "sn",
|
|
Short: "A shell utility program",
|
|
}
|
|
)
|
|
|
|
// Execute executes the root command.
|
|
func Execute() error {
|
|
return rootCmd.Execute()
|
|
}
|
|
|
|
func er(msg interface{}) {
|
|
fmt.Println("Error:", msg)
|
|
os.Exit(1)
|
|
}
|
|
|
|
func init() {
|
|
cobra.OnInitialize(initConfig)
|
|
}
|
|
|
|
func initConfig() {
|
|
home, err := homedir.Dir()
|
|
if err != nil {
|
|
er(err)
|
|
}
|
|
|
|
// Search config in home directory with name ".cobra" (without extension).
|
|
viper.AddConfigPath(home)
|
|
viper.SetConfigName(".sneak")
|
|
|
|
viper.AutomaticEnv()
|
|
|
|
if err := viper.ReadInConfig(); err == nil {
|
|
fmt.Println("Using config file:", viper.ConfigFileUsed())
|
|
}
|
|
}
|