focused_opps/cmd/root.go
2024-07-17 18:33:08 -05:00

95 lines
2.6 KiB
Go

/*
Copyright © 2023 Peter Morton <Peter.Morton@verint.com>
*/
package cmd
import (
"fmt"
"os"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
cfgFile string
Token string
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "focusedopps",
Short: "Focused Opps CLI",
Long: `Command line interface for accessing DFE Focused Opps list
https://verint-my.sharepoint.com/personal/peter_morton_verint_com/Lists/DFE%20Focus%20Opportunities/AllItems.aspx?env=WebViewList`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
cobra.OnInitialize(initConfig)
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.focusedopps.yaml)")
addStringFlag(&Token, "token", "", "Auth Token")
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose logging")
err := viper.BindPFlag("verbose", rootCmd.PersistentFlags().Lookup("verbose"))
if err != nil {
log.Error(err)
os.Exit(1)
}
// Cobra also supports local flags, which will only run
// when this action is called directly.
// rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
func addStringFlag(p *string, key string, value string, usage string) {
rootCmd.PersistentFlags().StringVar(p, key, value, usage)
err := viper.BindPFlag(key, rootCmd.PersistentFlags().Lookup(key))
if err != nil {
log.Error(err)
os.Exit(1)
}
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
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 ".tps" (without extension).
viper.AddConfigPath(home)
viper.SetConfigType("yaml")
viper.SetConfigName(".tps")
}
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
}
}