apps-durpcli/cmd/root.go

90 lines
2.1 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"
"os/user"
2023-02-19 13:44:18 -06:00
"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
}
2024-06-23 09:49:14 -05:00
func Execute() error {
2023-02-19 13:44:18 -06:00
err := rootCmd.Execute()
if err != nil {
2024-06-23 09:49:14 -05:00
return err
2023-02-19 13:44:18 -06:00
}
2024-06-23 09:49:14 -05:00
return nil
2023-02-19 13:44:18 -06:00
}
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() {
//if cfgFile != "" {
// viper.SetConfigFile(cfgFile)
//} else {
// cobra.CheckErr(err)
//}
2023-02-19 18:20:25 -06:00
setDefaults()
initConfig()
loadConfig()
2023-02-19 18:53:48 -06:00
2024-06-23 09:49:14 -05:00
// styles := tui.DefaultStyles()
// styles.Title.BorderForeground(lipgloss.AdaptiveColor{Light: `#E3BD2D`, Dark: `#E3BD2D`})
// styles.Border.BorderForeground(lipgloss.AdaptiveColor{Light: `#E3BD2D`, Dark: `#E3BD2D`})
// styles.SelectedItem.Foreground(lipgloss.AdaptiveColor{Light: `#353C3B`, Dark: `#353C3B`}).
// Background(lipgloss.AdaptiveColor{Light: `#E3BD2D`, Dark: `#E3BD2D`})
//
// b := tui.New(tui.WithStyles(styles))
// rootCmd.SetUsageFunc(b.UsageFunc)
// rootCmd.SetHelpFunc(b.HelpFunc)
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)
2024-06-23 09:49:14 -05:00
rootCmd.AddCommand(initialize())
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() {
usr, err := user.Current()
if err != nil {
os.Exit(1)
2023-02-19 18:20:25 -06:00
}
viper.SetConfigType("yaml")
viper.SetConfigName(".durpcli")
viper.AddConfigPath(usr.HomeDir)
}
2023-02-19 18:20:25 -06:00
func loadConfig() {
2023-05-27 11:43:24 -05:00
viper.AutomaticEnv()
2024-06-23 09:49:14 -05:00
viper.AddConfigPath("~/.durpcli.yaml")
err := viper.ReadInConfig()
if err != nil {
fmt.Println("Config file not found. Creating a new one with defaults...")
viper.SafeWriteConfig()
}
2023-02-19 13:44:18 -06:00
}