Quellcode durchsuchen

Merge pull request #1730 from porter-dev/nafees/better-cli-config-set

`porter config set` commands should give a selector menu for better UX
abelanger5 vor 4 Jahren
Ursprung
Commit
556bf5255a
3 geänderte Dateien mit 188 neuen und 31 gelöschten Zeilen
  1. 185 31
      cli/cmd/config.go
  2. 1 0
      go.mod
  3. 2 0
      go.sum

+ 185 - 31
cli/cmd/config.go

@@ -1,14 +1,20 @@
 package cmd
 package cmd
 
 
 import (
 import (
+	"context"
 	"fmt"
 	"fmt"
 	"io/ioutil"
 	"io/ioutil"
 	"os"
 	"os"
 	"path/filepath"
 	"path/filepath"
 	"strconv"
 	"strconv"
 	"strings"
 	"strings"
+	"time"
 
 
+	"github.com/briandowns/spinner"
 	"github.com/fatih/color"
 	"github.com/fatih/color"
+	api "github.com/porter-dev/porter/api/client"
+	"github.com/porter-dev/porter/api/types"
+	"github.com/porter-dev/porter/cli/cmd/utils"
 	"github.com/spf13/cobra"
 	"github.com/spf13/cobra"
 	"github.com/spf13/viper"
 	"github.com/spf13/viper"
 
 
@@ -278,63 +284,87 @@ var configCmd = &cobra.Command{
 
 
 var configSetProjectCmd = &cobra.Command{
 var configSetProjectCmd = &cobra.Command{
 	Use:   "set-project [id]",
 	Use:   "set-project [id]",
-	Args:  cobra.ExactArgs(1),
+	Args:  cobra.MaximumNArgs(1),
 	Short: "Saves the project id in the default configuration",
 	Short: "Saves the project id in the default configuration",
 	Run: func(cmd *cobra.Command, args []string) {
 	Run: func(cmd *cobra.Command, args []string) {
-		projID, err := strconv.ParseUint(args[0], 10, 64)
+		if len(args) == 0 {
+			err := checkLoginAndRun(args, listAndSetProject)
 
 
-		if err != nil {
-			color.New(color.FgRed).Printf("An error occurred: %v\n", err)
-			os.Exit(1)
-		}
+			if err != nil {
+				os.Exit(1)
+			}
+		} else {
+			projID, err := strconv.ParseUint(args[0], 10, 64)
 
 
-		err = config.SetProject(uint(projID))
+			if err != nil {
+				color.New(color.FgRed).Printf("An error occurred: %v\n", err)
+				os.Exit(1)
+			}
 
 
-		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{
 var configSetClusterCmd = &cobra.Command{
 	Use:   "set-cluster [id]",
 	Use:   "set-cluster [id]",
-	Args:  cobra.ExactArgs(1),
+	Args:  cobra.MaximumNArgs(1),
 	Short: "Saves the cluster id in the default configuration",
 	Short: "Saves the cluster id in the default configuration",
 	Run: func(cmd *cobra.Command, args []string) {
 	Run: func(cmd *cobra.Command, args []string) {
-		clusterID, err := strconv.ParseUint(args[0], 10, 64)
+		if len(args) == 0 {
+			err := checkLoginAndRun(args, listAndSetCluster)
 
 
-		if err != nil {
-			color.New(color.FgRed).Printf("An error occurred: %v\n", err)
-			os.Exit(1)
-		}
+			if err != nil {
+				os.Exit(1)
+			}
+		} else {
+			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))
+			err = config.SetCluster(uint(clusterID))
 
 
-		if err != nil {
-			color.New(color.FgRed).Printf("An error occurred: %v\n", err)
-			os.Exit(1)
+			if err != nil {
+				color.New(color.FgRed).Printf("An error occurred: %v\n", err)
+				os.Exit(1)
+			}
 		}
 		}
 	},
 	},
 }
 }
 
 
 var configSetRegistryCmd = &cobra.Command{
 var configSetRegistryCmd = &cobra.Command{
 	Use:   "set-registry [id]",
 	Use:   "set-registry [id]",
-	Args:  cobra.ExactArgs(1),
+	Args:  cobra.MaximumNArgs(1),
 	Short: "Saves the registry id in the default configuration",
 	Short: "Saves the registry id in the default configuration",
 	Run: func(cmd *cobra.Command, args []string) {
 	Run: func(cmd *cobra.Command, args []string) {
-		registryID, err := strconv.ParseUint(args[0], 10, 64)
+		if len(args) == 0 {
+			err := checkLoginAndRun(args, listAndSetRegistry)
 
 
-		if err != nil {
-			color.New(color.FgRed).Printf("An error occurred: %v\n", err)
-			os.Exit(1)
-		}
+			if err != nil {
+				os.Exit(1)
+			}
+		} else {
+			registryID, err := strconv.ParseUint(args[0], 10, 64)
 
 
-		err = config.SetRegistry(uint(registryID))
+			if err != nil {
+				color.New(color.FgRed).Printf("An error occurred: %v\n", err)
+				os.Exit(1)
+			}
 
 
-		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)
+			}
 		}
 		}
 	},
 	},
 }
 }
@@ -391,7 +421,131 @@ func printConfig() error {
 		return err
 		return err
 	}
 	}
 
 
-	fmt.Printf(string(config))
+	fmt.Println(string(config))
+
+	return nil
+}
+
+func listAndSetProject(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
+	s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
+	s.Color("cyan")
+	s.Suffix = " Loading list of projects"
+	s.Start()
+
+	resp, err := client.ListUserProjects(context.Background())
+
+	s.Stop()
+
+	if err != nil {
+		return err
+	}
+
+	var projID uint64
+
+	if len(*resp) > 1 {
+		// only give the option to select when more than one option exists
+		projName, err := utils.PromptSelect("Select a project with ID", func() []string {
+			var names []string
+
+			for _, proj := range *resp {
+				names = append(names, fmt.Sprintf("%s - %d", proj.Name, proj.ID))
+			}
+
+			return names
+		}())
+
+		if err != nil {
+			return err
+		}
+
+		projID, _ = strconv.ParseUint(strings.Split(projName, " - ")[1], 10, 64)
+	} else {
+		projID = uint64((*resp)[0].ID)
+	}
+
+	config.SetProject(uint(projID))
+
+	return nil
+}
+
+func listAndSetCluster(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
+	s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
+	s.Color("cyan")
+	s.Suffix = " Loading list of clusters"
+	s.Start()
+
+	resp, err := client.ListProjectClusters(context.Background(), config.Project)
+
+	s.Stop()
+
+	if err != nil {
+		return err
+	}
+
+	var clusterID uint64
+
+	if len(*resp) > 1 {
+		clusterName, err := utils.PromptSelect("Select a cluster with ID", func() []string {
+			var names []string
+
+			for _, cluster := range *resp {
+				names = append(names, fmt.Sprintf("%s - %d", cluster.Name, cluster.ID))
+			}
+
+			return names
+		}())
+
+		if err != nil {
+			return err
+		}
+
+		clusterID, _ = strconv.ParseUint(strings.Split(clusterName, " - ")[1], 10, 64)
+	} else {
+		clusterID = uint64((*resp)[0].ID)
+	}
+
+	config.SetCluster(uint(clusterID))
+
+	return nil
+}
+
+func listAndSetRegistry(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
+	s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
+	s.Color("cyan")
+	s.Suffix = " Loading list of registries"
+	s.Start()
+
+	resp, err := client.ListRegistries(context.Background(), config.Project)
+
+	s.Stop()
+
+	if err != nil {
+		return err
+	}
+
+	var regID uint64
+
+	if len(*resp) > 1 {
+		regName, err := utils.PromptSelect("Select a registry with ID", func() []string {
+			var names []string
+
+			for _, cluster := range *resp {
+				names = append(names, fmt.Sprintf("%s - %d", cluster.Name, cluster.ID))
+			}
+
+			return names
+		}())
+
+		if err != nil {
+			return err
+		}
+
+		regID, _ = strconv.ParseUint(strings.Split(regName, " - ")[1], 10, 64)
+	} else {
+		regID = uint64((*resp)[0].ID)
+	}
+
+	config.SetRegistry(uint(regID))
 
 
 	return nil
 	return nil
 }
 }

+ 1 - 0
go.mod

@@ -85,6 +85,7 @@ require (
 	github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535 // indirect
 	github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535 // indirect
 	github.com/beorn7/perks v1.0.1 // indirect
 	github.com/beorn7/perks v1.0.1 // indirect
 	github.com/bits-and-blooms/bitset v1.2.0 // indirect
 	github.com/bits-and-blooms/bitset v1.2.0 // indirect
+	github.com/briandowns/spinner v1.18.1 // indirect
 	github.com/buildpacks/imgutil v0.0.0-20210510154637-009f91f52918 // indirect
 	github.com/buildpacks/imgutil v0.0.0-20210510154637-009f91f52918 // indirect
 	github.com/buildpacks/lifecycle v0.11.3 // indirect
 	github.com/buildpacks/lifecycle v0.11.3 // indirect
 	github.com/census-instrumentation/opencensus-proto v0.3.0 // indirect
 	github.com/census-instrumentation/opencensus-proto v0.3.0 // indirect

+ 2 - 0
go.sum

@@ -217,6 +217,8 @@ github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dR
 github.com/bradleyfalzon/ghinstallation/v2 v2.0.3 h1:ywF/8q+GVpvlsEuvRb1SGSDQDUxntW1d4kFu/9q/YAE=
 github.com/bradleyfalzon/ghinstallation/v2 v2.0.3 h1:ywF/8q+GVpvlsEuvRb1SGSDQDUxntW1d4kFu/9q/YAE=
 github.com/bradleyfalzon/ghinstallation/v2 v2.0.3/go.mod h1:tlgi+JWCXnKFx/Y4WtnDbZEINo31N5bcvnCoqieefmk=
 github.com/bradleyfalzon/ghinstallation/v2 v2.0.3/go.mod h1:tlgi+JWCXnKFx/Y4WtnDbZEINo31N5bcvnCoqieefmk=
 github.com/briandowns/spinner v1.11.1/go.mod h1:QOuQk7x+EaDASo80FEXwlwiA+j/PPIcX3FScO+3/ZPQ=
 github.com/briandowns/spinner v1.11.1/go.mod h1:QOuQk7x+EaDASo80FEXwlwiA+j/PPIcX3FScO+3/ZPQ=
+github.com/briandowns/spinner v1.18.1 h1:yhQmQtM1zsqFsouh09Bk/jCjd50pC3EOGsh28gLVvwY=
+github.com/briandowns/spinner v1.18.1/go.mod h1:mQak9GHqbspjC/5iUx3qMlIho8xBS/ppAL/hX5SmPJU=
 github.com/bshuster-repo/logrus-logstash-hook v0.4.1/go.mod h1:zsTqEiSzDgAa/8GZR7E1qaXrhYNDKBYy5/dWPTIflbk=
 github.com/bshuster-repo/logrus-logstash-hook v0.4.1/go.mod h1:zsTqEiSzDgAa/8GZR7E1qaXrhYNDKBYy5/dWPTIflbk=
 github.com/bshuster-repo/logrus-logstash-hook v1.0.0 h1:e+C0SB5R1pu//O4MQ3f9cFuPGoOVeF2fE4Og9otCc70=
 github.com/bshuster-repo/logrus-logstash-hook v1.0.0 h1:e+C0SB5R1pu//O4MQ3f9cFuPGoOVeF2fE4Og9otCc70=
 github.com/bshuster-repo/logrus-logstash-hook v1.0.0/go.mod h1:zsTqEiSzDgAa/8GZR7E1qaXrhYNDKBYy5/dWPTIflbk=
 github.com/bshuster-repo/logrus-logstash-hook v1.0.0/go.mod h1:zsTqEiSzDgAa/8GZR7E1qaXrhYNDKBYy5/dWPTIflbk=