Alexander Belanger пре 5 година
родитељ
комит
ae2dcf40a3
3 измењених фајлова са 113 додато и 19 уклоњено
  1. 41 4
      cli/cmd/config.go
  2. 0 15
      cli/cmd/connect/ecr.go
  3. 72 0
      cli/cmd/registry.go

+ 41 - 4
cli/cmd/config.go

@@ -11,10 +11,11 @@ import (
 
 // a set of shared flags
 var (
-	driver    string
-	host      string
-	projectID uint
-	clusterID uint
+	driver     string
+	host       string
+	projectID  uint
+	registryID uint
+	clusterID  uint
 )
 
 var configCmd = &cobra.Command{
@@ -64,6 +65,27 @@ var setClusterCmd = &cobra.Command{
 	},
 }
 
+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 setHostCmd = &cobra.Command{
 	Use:   "set-host [host]",
 	Args:  cobra.ExactArgs(1),
@@ -84,6 +106,7 @@ func init() {
 	configCmd.AddCommand(setProjectCmd)
 	configCmd.AddCommand(setClusterCmd)
 	configCmd.AddCommand(setHostCmd)
+	configCmd.AddCommand(setRegistryCmd)
 }
 
 func setDriver(driver string) error {
@@ -117,6 +140,12 @@ func setCluster(id uint) error {
 	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 setHost(host string) error {
 	viper.Set("host", host)
 	err := viper.WriteConfig()
@@ -140,6 +169,14 @@ func getClusterID() uint {
 	return viper.GetUint("cluster")
 }
 
+func getRegistryID() uint {
+	if registryID != 0 {
+		return registryID
+	}
+
+	return viper.GetUint("registry")
+}
+
 func getProjectID() uint {
 	if projectID != 0 {
 		return projectID

+ 0 - 15
cli/cmd/connect/ecr.go

@@ -80,20 +80,5 @@ func ECR(
 
 	color.New(color.FgGreen).Printf("created registry with id %d and name %s\n", reg.ID, reg.Name)
 
-	// test by listing repositories
-	repos, err := client.ListRegistryRepositories(
-		context.Background(),
-		projectID,
-		reg.ID,
-	)
-
-	if err != nil {
-		return err
-	}
-
-	for _, repo := range repos {
-		fmt.Println("REPO IS", repo.Name)
-	}
-
 	return nil
 }

+ 72 - 0
cli/cmd/registry.go

@@ -0,0 +1,72 @@
+package cmd
+
+import (
+	"context"
+	"fmt"
+	"os"
+	"text/tabwriter"
+
+	"github.com/porter-dev/porter/cli/cmd/api"
+	"github.com/spf13/cobra"
+)
+
+// registryCmd represents the "porter registry" base command when called
+// without any subcommands
+var registryCmd = &cobra.Command{
+	Use:   "registry",
+	Short: "Commands that read from a connected registry",
+}
+
+var registryCmdListRepos = &cobra.Command{
+	Use:   "repos list",
+	Short: "Lists the repositories in a registry",
+	Run: func(cmd *cobra.Command, args []string) {
+		err := checkLoginAndRun(args, listRepos)
+
+		if err != nil {
+			os.Exit(1)
+		}
+	},
+}
+
+func init() {
+	rootCmd.AddCommand(registryCmd)
+
+	registryCmd.PersistentFlags().UintVar(
+		&registryID,
+		"registry-id",
+		0,
+		"id of the registry",
+	)
+
+	registryCmd.AddCommand(registryCmdListRepos)
+}
+
+func listRepos(user *api.AuthCheckResponse, client *api.Client, args []string) error {
+	pID := getProjectID()
+	rID := getRegistryID()
+
+	// get the list of namespaces
+	repos, err := client.ListRegistryRepositories(
+		context.Background(),
+		pID,
+		rID,
+	)
+
+	if err != nil {
+		return err
+	}
+
+	w := new(tabwriter.Writer)
+	w.Init(os.Stdout, 3, 8, 0, '\t', tabwriter.AlignRight)
+
+	fmt.Fprintf(w, "%s\t%s\n", "NAME", "CREATED_AT")
+
+	for _, repo := range repos {
+		fmt.Fprintf(w, "%s\t%s\n", repo.Name, repo.CreatedAt.String())
+	}
+
+	w.Flush()
+
+	return nil
+}