Sfoglia il codice sorgente

add config commands back

Alexander Belanger 4 anni fa
parent
commit
6126cb7791
1 ha cambiato i file con 133 aggiunte e 230 eliminazioni
  1. 133 230
      cli/cmd/config.go

+ 133 - 230
cli/cmd/config.go

@@ -1,11 +1,14 @@
 package cmd
 
 import (
+	"fmt"
 	"io/ioutil"
 	"os"
 	"path/filepath"
+	"strconv"
 
 	"github.com/fatih/color"
+	"github.com/spf13/cobra"
 	"github.com/spf13/viper"
 
 	flag "github.com/spf13/pflag"
@@ -258,233 +261,133 @@ func (c *CLIConfig) SetHelmRepo(helmRepoID uint) error {
 	return nil
 }
 
-// var configCmd = &cobra.Command{
-// 	Use:   "config",
-// 	Short: "Commands that control local configuration settings",
-// 	Run: func(cmd *cobra.Command, args []string) {
-// 		if err := printConfig(); err != nil {
-// 			color.New(color.FgRed).Printf("An error occurred: %v\n", err)
-// 			os.Exit(1)
-// 		}
-// 	},
-// }
-
-// var config.SetProjectCmd = &cobra.Command{
-// 	Use:   "set-project [id]",
-// 	Args:  cobra.ExactArgs(1),
-// 	Short: "Saves the project id in the default configuration",
-// 	Run: func(cmd *cobra.Command, args []string) {
-// 		projID, err := strconv.ParseUint(args[0], 10, 64)
-
-// 		if err != nil {
-// 			color.New(color.FgRed).Printf("An error occurred: %v\n", err)
-// 			os.Exit(1)
-// 		}
-
-// 		err = config.SetProject(uint(projID))
-
-// 		if err != nil {
-// 			color.New(color.FgRed).Printf("An error occurred: %v\n", err)
-// 			os.Exit(1)
-// 		}
-// 	},
-// }
-
-// var config.SetClusterCmd = &cobra.Command{
-// 	Use:   "set-cluster [id]",
-// 	Args:  cobra.ExactArgs(1),
-// 	Short: "Saves the cluster id in the default configuration",
-// 	Run: func(cmd *cobra.Command, args []string) {
-// 		clusterID, err := strconv.ParseUint(args[0], 10, 64)
-
-// 		if err != nil {
-// 			color.New(color.FgRed).Printf("An error occurred: %v\n", err)
-// 			os.Exit(1)
-// 		}
-
-// 		err = config.SetCluster(uint(clusterID))
-
-// 		if err != nil {
-// 			color.New(color.FgRed).Printf("An error occurred: %v\n", err)
-// 			os.Exit(1)
-// 		}
-// 	},
-// }
-
-// var setRegistryCmd = &cobra.Command{
-// 	Use:   "set-registry [id]",
-// 	Args:  cobra.ExactArgs(1),
-// 	Short: "Saves the registry id in the default configuration",
-// 	Run: func(cmd *cobra.Command, args []string) {
-// 		registryID, err := strconv.ParseUint(args[0], 10, 64)
-
-// 		if err != nil {
-// 			color.New(color.FgRed).Printf("An error occurred: %v\n", err)
-// 			os.Exit(1)
-// 		}
-
-// 		err = setRegistry(uint(registryID))
-
-// 		if err != nil {
-// 			color.New(color.FgRed).Printf("An error occurred: %v\n", err)
-// 			os.Exit(1)
-// 		}
-// 	},
-// }
-
-// var setHelmRepoCmd = &cobra.Command{
-// 	Use:   "set-helmrepo [id]",
-// 	Args:  cobra.ExactArgs(1),
-// 	Short: "Saves the helm repo id in the default configuration",
-// 	Run: func(cmd *cobra.Command, args []string) {
-// 		hrID, err := strconv.ParseUint(args[0], 10, 64)
-
-// 		if err != nil {
-// 			color.New(color.FgRed).Printf("An error occurred: %v\n", err)
-// 			os.Exit(1)
-// 		}
-
-// 		err = setHelmRepo(uint(hrID))
-
-// 		if err != nil {
-// 			color.New(color.FgRed).Printf("An error occurred: %v\n", err)
-// 			os.Exit(1)
-// 		}
-// 	},
-// }
-
-// var config.SetHostCmd = &cobra.Command{
-// 	Use:   "set-host [host]",
-// 	Args:  cobra.ExactArgs(1),
-// 	Short: "Saves the host in the default configuration",
-// 	Run: func(cmd *cobra.Command, args []string) {
-// 		err := config.SetHost(args[0])
-
-// 		if err != nil {
-// 			color.New(color.FgRed).Printf("An error occurred: %v\n", err)
-// 			os.Exit(1)
-// 		}
-// 	},
-// }
-
-// func init() {
-// 	rootCmd.AddCommand(configCmd)
-
-// 	configCmd.AddCommand(config.SetProjectCmd)
-// 	configCmd.AddCommand(config.SetClusterCmd)
-// 	configCmd.AddCommand(config.SetHostCmd)
-// 	configCmd.AddCommand(setRegistryCmd)
-// 	configCmd.AddCommand(setHelmRepoCmd)
-// }
-
-// func setDriver(driver string) error {
-// 	viper.Set("driver", driver)
-// 	err := viper.WriteConfig()
-// 	color.New(color.FgGreen).Printf("Set the current driver as %s\n", driver)
-// 	return err
-// }
-
-// func getDriver() string {
-// 	if driver != "" {
-// 		return driver
-// 	}
-
-// 	if opts.driver != "" {
-// 		return opts.driver
-// 	}
-
-// 	return viper.GetString("driver")
-// }
-
-// func printConfig() error {
-// 	config, err := ioutil.ReadFile(filepath.Join(home, ".porter", "porter.yaml"))
-
-// 	if err != nil {
-// 		return err
-// 	}
-
-// 	fmt.Printf(string(config))
-
-// 	return nil
-// }
-
-// func config.SetProject(id uint) error {
-// 	viper.Set("project", id)
-// 	color.New(color.FgGreen).Printf("Set the current project id as %d\n", id)
-// 	return viper.WriteConfig()
-// }
-
-// func config.SetCluster(id uint) error {
-// 	viper.Set("cluster", id)
-// 	color.New(color.FgGreen).Printf("Set the current cluster id as %d\n", id)
-// 	return viper.WriteConfig()
-// }
-
-// func setRegistry(id uint) error {
-// 	viper.Set("registry", id)
-// 	color.New(color.FgGreen).Printf("Set the current registry id as %d\n", id)
-// 	return viper.WriteConfig()
-// }
-
-// func setHelmRepo(id uint) error {
-// 	viper.Set("helm_repo", id)
-// 	color.New(color.FgGreen).Printf("Set the current helm repo id as %d\n", id)
-// 	return viper.WriteConfig()
-// }
-
-// func config.SetHost(host string) error {
-// 	viper.Set("host", host)
-// 	err := viper.WriteConfig()
-// 	color.New(color.FgGreen).Printf("Set the current host as %s\n", host)
-// 	return err
-// }
-
-// func config.SetToken(token string) error {
-// 	viper.Set("token", token)
-// 	err := viper.WriteConfig()
-// 	return err
-// }
-
-// func config.Host string {
-// 	if host != "" {
-// 		return host
-// 	}
-
-// 	return viper.GetString("host")
-// }
-
-// func config.Token string {
-// 	return viper.GetString("token")
-// }
-
-// func config.Cluster() uint {
-// 	if clusterID != 0 {
-// 		return clusterID
-// 	}
-
-// 	return viper.GetUint("cluster")
-// }
-
-// func config.Registry uint {
-// 	if registryID != 0 {
-// 		return registryID
-// 	}
-
-// 	return viper.GetUint("registry")
-// }
-
-// func config.HelmRepo() uint {
-// 	if helmRepoID != 0 {
-// 		return helmRepoID
-// 	}
-
-// 	return viper.GetUint("helm_repo")
-// }
-
-// func config.Project uint {
-// 	if projectID != 0 {
-// 		return projectID
-// 	}
-
-// 	return viper.GetUint("project")
-// }
+var configCmd = &cobra.Command{
+	Use:   "config",
+	Short: "Commands that control local configuration settings",
+	Run: func(cmd *cobra.Command, args []string) {
+		if err := printConfig(); err != nil {
+			color.New(color.FgRed).Printf("An error occurred: %v\n", err)
+			os.Exit(1)
+		}
+	},
+}
+
+var configSetProjectCmd = &cobra.Command{
+	Use:   "set-project [id]",
+	Args:  cobra.ExactArgs(1),
+	Short: "Saves the project id in the default configuration",
+	Run: func(cmd *cobra.Command, args []string) {
+		projID, err := strconv.ParseUint(args[0], 10, 64)
+
+		if err != nil {
+			color.New(color.FgRed).Printf("An error occurred: %v\n", err)
+			os.Exit(1)
+		}
+
+		err = config.SetProject(uint(projID))
+
+		if err != nil {
+			color.New(color.FgRed).Printf("An error occurred: %v\n", err)
+			os.Exit(1)
+		}
+	},
+}
+
+var configSetClusterCmd = &cobra.Command{
+	Use:   "set-cluster [id]",
+	Args:  cobra.ExactArgs(1),
+	Short: "Saves the cluster id in the default configuration",
+	Run: func(cmd *cobra.Command, args []string) {
+		clusterID, err := strconv.ParseUint(args[0], 10, 64)
+
+		if err != nil {
+			color.New(color.FgRed).Printf("An error occurred: %v\n", err)
+			os.Exit(1)
+		}
+
+		err = config.SetCluster(uint(clusterID))
+
+		if err != nil {
+			color.New(color.FgRed).Printf("An error occurred: %v\n", err)
+			os.Exit(1)
+		}
+	},
+}
+
+var configSetRegistryCmd = &cobra.Command{
+	Use:   "set-registry [id]",
+	Args:  cobra.ExactArgs(1),
+	Short: "Saves the registry id in the default configuration",
+	Run: func(cmd *cobra.Command, args []string) {
+		registryID, err := strconv.ParseUint(args[0], 10, 64)
+
+		if err != nil {
+			color.New(color.FgRed).Printf("An error occurred: %v\n", err)
+			os.Exit(1)
+		}
+
+		err = config.SetRegistry(uint(registryID))
+
+		if err != nil {
+			color.New(color.FgRed).Printf("An error occurred: %v\n", err)
+			os.Exit(1)
+		}
+	},
+}
+
+var configSetHelmRepoCmd = &cobra.Command{
+	Use:   "set-helmrepo [id]",
+	Args:  cobra.ExactArgs(1),
+	Short: "Saves the helm repo id in the default configuration",
+	Run: func(cmd *cobra.Command, args []string) {
+		hrID, err := strconv.ParseUint(args[0], 10, 64)
+
+		if err != nil {
+			color.New(color.FgRed).Printf("An error occurred: %v\n", err)
+			os.Exit(1)
+		}
+
+		err = config.SetHelmRepo(uint(hrID))
+
+		if err != nil {
+			color.New(color.FgRed).Printf("An error occurred: %v\n", err)
+			os.Exit(1)
+		}
+	},
+}
+
+var configSetHostCmd = &cobra.Command{
+	Use:   "set-host [host]",
+	Args:  cobra.ExactArgs(1),
+	Short: "Saves the host in the default configuration",
+	Run: func(cmd *cobra.Command, args []string) {
+		err := config.SetHost(args[0])
+
+		if err != nil {
+			color.New(color.FgRed).Printf("An error occurred: %v\n", err)
+			os.Exit(1)
+		}
+	},
+}
+
+func init() {
+	rootCmd.AddCommand(configCmd)
+
+	configCmd.AddCommand(configSetProjectCmd)
+	configCmd.AddCommand(configSetClusterCmd)
+	configCmd.AddCommand(configSetHostCmd)
+	configCmd.AddCommand(configSetRegistryCmd)
+	configCmd.AddCommand(configSetHelmRepoCmd)
+}
+
+func printConfig() error {
+	config, err := ioutil.ReadFile(filepath.Join(home, ".porter", "porter.yaml"))
+
+	if err != nil {
+		return err
+	}
+
+	fmt.Printf(string(config))
+
+	return nil
+}