| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- package cmd
- import (
- "fmt"
- "io/ioutil"
- "os"
- "path/filepath"
- "strconv"
- "github.com/fatih/color"
- "github.com/spf13/cobra"
- "github.com/spf13/viper"
- )
- // a set of shared flags
- var (
- driver string
- host string
- projectID uint
- registryID uint
- clusterID uint
- helmRepoID uint
- )
- 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 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 = setProject(uint(projID))
- if err != nil {
- color.New(color.FgRed).Printf("An error occurred: %v\n", err)
- os.Exit(1)
- }
- },
- }
- var 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 = 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 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 := 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(setProjectCmd)
- configCmd.AddCommand(setClusterCmd)
- configCmd.AddCommand(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 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 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 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 setToken(token string) error {
- viper.Set("token", token)
- err := viper.WriteConfig()
- return err
- }
- func getHost() string {
- if host != "" {
- return host
- }
- return viper.GetString("host")
- }
- func getToken() string {
- return viper.GetString("token")
- }
- func getClusterID() uint {
- if clusterID != 0 {
- return clusterID
- }
- return viper.GetUint("cluster")
- }
- func getRegistryID() uint {
- if registryID != 0 {
- return registryID
- }
- return viper.GetUint("registry")
- }
- func getHelmRepoID() uint {
- if helmRepoID != 0 {
- return helmRepoID
- }
- return viper.GetUint("helm_repo")
- }
- func getProjectID() uint {
- if projectID != 0 {
- return projectID
- }
- return viper.GetUint("project")
- }
|