60 lines
1 KiB
Go
60 lines
1 KiB
Go
|
package cmd
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"log"
|
||
|
"os/signal"
|
||
|
"strings"
|
||
|
"syscall"
|
||
|
|
||
|
"github.com/pterm/pterm"
|
||
|
"github.com/spf13/cobra"
|
||
|
|
||
|
"github.com/spf13/viper"
|
||
|
)
|
||
|
|
||
|
var cfgFile string
|
||
|
|
||
|
var rootCmd = &cobra.Command{
|
||
|
Use: "scim",
|
||
|
Short: "A tool to quickly setup SCIM",
|
||
|
}
|
||
|
|
||
|
func Execute() {
|
||
|
ctx := context.Background()
|
||
|
ctx, cancel := context.WithCancel(ctx)
|
||
|
defer cancel()
|
||
|
ctx, stop := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM)
|
||
|
defer stop()
|
||
|
cobra.CheckErr(rootCmd.ExecuteContext(ctx))
|
||
|
}
|
||
|
|
||
|
func init() {
|
||
|
cobra.OnInitialize(initConfig)
|
||
|
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file")
|
||
|
}
|
||
|
|
||
|
func initConfig() {
|
||
|
if cfgFile != "" {
|
||
|
viper.SetConfigFile(cfgFile)
|
||
|
}
|
||
|
viper.SetEnvPrefix("scim")
|
||
|
viper.SetEnvKeyReplacer(strings.NewReplacer(`.`, `_`))
|
||
|
viper.AutomaticEnv()
|
||
|
viper.ReadInConfig()
|
||
|
// viper.BindPFlags(rootCmd.Flags())
|
||
|
}
|
||
|
|
||
|
func CheckErr(err error) {
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func CheckErrSpinner(err error, spinner *pterm.SpinnerPrinter) {
|
||
|
if err != nil {
|
||
|
spinner.Fail(err)
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
}
|