/* Copyright © 2024 Brian Buller */ package cmd import ( "fmt" "os" "strings" "git.bullercodeworks.com/brian/nccli/util" gap "github.com/muesli/go-app-paths" "github.com/spf13/cobra" "github.com/spf13/viper" ) // rootCmd represents the base command when called without any subcommands var ( Version = "1.0" Build = "1" configFile string rootCmd = &cobra.Command{ Use: "nccli", Short: "A small utility for various Nextcloud functions", // 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() { rootCmd.Version = Version initConfig() } func initConfig() { var firstDir string // In case we need to make directories if configFile != "" { viper.SetConfigFile(configFile) } else { scope := gap.NewScope(gap.User, "nccli") dirs, err := scope.ConfigDirs() if err != nil { fmt.Println("Can't retrieve default config. Please manually pass a config file with '--config'") os.Exit(1) } firstDir = dirs[0] for _, v := range dirs { viper.AddConfigPath(v) } viper.SetConfigName("nccli") viper.SetConfigType("yaml") } var createConfig bool v2Path := fmt.Sprintf("%s%s%s", firstDir, string(os.PathSeparator), "nccli.yaml") if err := viper.ReadInConfig(); err != nil { createConfig = true if _, ok := err.(viper.ConfigFileNotFoundError); ok { // Config file not found; Check if we have a v1 config file fmt.Println("Config file not found.") } else { fmt.Println("Found config file, but another error occurred.") fmt.Println(err) } } if createConfig { _, err := os.Stat(firstDir) if os.IsNotExist(err) { err := os.Mkdir(firstDir, 0755) if err != nil { fmt.Println(err) os.Exit(1) } } if err = viper.WriteConfigAs(v2Path); err != nil { fmt.Println(err) os.Exit(1) } } ncuser := viper.GetString("ncuser") if ncuser == "" { ncuser = util.PromptUser("Nextcloud User Name") if ncuser == "" { fmt.Println("No Nextcloud User Name given") os.Exit(1) } viper.Set("ncuser", ncuser) viper.WriteConfig() } ncpw := viper.GetString("ncpw") if ncpw == "" { var err error ncpw = util.PromptUser("Enter Password") if ncpw != "" { ncpw, err = util.Encrypt(ncpw) if err != nil { fmt.Printf("Error encrypting password: %v", err) os.Exit(1) } } else { fmt.Println("No Nextcloud Password given") os.Exit(1) } viper.Set("ncpw", ncpw) viper.WriteConfig() } url := viper.GetString("ncurl") if url == "" { url = util.PromptUser("Nextcloud Base URL") if url == "" { fmt.Println("No Nextcloud Base URL given") os.Exit(1) } if !strings.HasSuffix(url, "/") { url = url + "/" } viper.Set("ncurl", url) viper.WriteConfig() } dir := viper.GetString("directory") if dir == "" { dir = util.PromptUser("Path to your local Nextcloud Root") if dir == "" { fmt.Println("No path to Nexctloud root given") os.Exit(1) } if !strings.HasSuffix(dir, "/") { dir = dir + "/" } viper.Set("directory", dir) viper.WriteConfig() } }