apps-durpcli/cmd/root.go

71 lines
1.4 KiB
Go
Raw Normal View History

2023-02-19 13:44:18 -06:00
package cmd
import (
2023-02-19 18:20:25 -06:00
"fmt"
2023-02-19 13:44:18 -06:00
"os"
"github.com/spf13/cobra"
2023-02-19 18:20:25 -06:00
"github.com/spf13/viper"
2023-05-27 11:43:24 -05:00
"gitlab.com/DeveloperDurp/DurpCLI/cmd/auth"
"gitlab.com/DeveloperDurp/DurpCLI/cmd/cfg"
"gitlab.com/DeveloperDurp/DurpCLI/cmd/net"
2023-02-19 13:44:18 -06:00
)
2023-02-19 18:20:25 -06:00
var cfgFile string
2023-02-19 13:44:18 -06:00
var rootCmd = &cobra.Command{
2023-05-27 11:43:24 -05:00
Use: "DurpCLI",
Short: "CLI Tool made for Durp",
Long: ``,
2023-02-19 13:44:18 -06:00
}
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
2023-02-19 18:20:25 -06:00
func setDefaults() {
2023-05-27 11:43:24 -05:00
viper.SetDefault("auth.url", "https://authentik.durp.info/application/o/token/")
viper.SetDefault("auth.grantType", "client_credentials")
viper.SetDefault("auth.clientID", "")
viper.SetDefault("auth.username", "")
viper.SetDefault("auth.password", "")
2023-02-19 13:44:18 -06:00
}
func init() {
2023-02-19 18:20:25 -06:00
cobra.OnInitialize(initConfig)
setDefaults()
2023-05-27 11:43:24 -05:00
err := viper.WriteConfigAs(".DurpCLI.yaml")
2023-02-19 18:53:48 -06:00
if err != nil {
fmt.Println(err)
}
2023-02-19 18:20:25 -06:00
rootCmd.AddCommand(net.NetCmd)
2023-05-27 11:43:24 -05:00
rootCmd.AddCommand(auth.AuthCmd)
rootCmd.AddCommand(cfg.Cfgcmd)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.DurpCLI.yaml)")
2023-02-19 13:44:18 -06:00
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
2023-02-19 18:20:25 -06:00
}
func initConfig() {
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
} else {
home, err := os.UserHomeDir()
cobra.CheckErr(err)
viper.AddConfigPath(home)
viper.SetConfigType("yaml")
2023-05-27 11:43:24 -05:00
viper.SetConfigName(".DurpCLI")
2023-02-19 18:20:25 -06:00
}
2023-05-27 11:43:24 -05:00
viper.AutomaticEnv()
viper.ReadInConfig()
2023-02-19 18:20:25 -06:00
2023-02-19 13:44:18 -06:00
}