102 lines
2.9 KiB
Go
102 lines
2.9 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
|
|
Hostname string
|
|
Username string
|
|
Password string
|
|
ClientId string
|
|
)
|
|
|
|
// rootCmd represents the base command when called without any subcommands
|
|
var rootCmd = &cobra.Command{
|
|
Use: "tps",
|
|
Short: "Tenant Properties Service CLI",
|
|
Long: `Command line interface for accessing and setting Tenant Properties
|
|
For a full list of System Properties, please refer to the documentation:
|
|
https://em-docs.verint.com/15_3/em-knowledge-management/Content/SystemProperties/Tenant_Properties_Service.htm`,
|
|
// 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/.tps.yaml)")
|
|
addStringFlag(&Hostname, "hostname", "", "Environment hostname")
|
|
addStringFlag(&Username, "username", "apiclient", "API username")
|
|
addStringFlag(&Password, "password", "apiclient", "API password")
|
|
addStringFlag(&ClientId, "clientId", "default", "clientId or tenantId for API calls")
|
|
|
|
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())
|
|
}
|
|
}
|