Explorar o código

cli list clusters and projects

Alexander Belanger %!s(int64=5) %!d(string=hai) anos
pai
achega
d16038926e
Modificáronse 3 ficheiros con 78 adicións e 5 borrados
  1. 17 0
      cli/cmd/auth.go
  2. 60 4
      cli/cmd/project.go
  3. 1 1
      go.mod

+ 17 - 0
cli/cmd/auth.go

@@ -4,6 +4,9 @@ import (
 	"context"
 	"fmt"
 	"os"
+	"strings"
+
+	"github.com/fatih/color"
 
 	"github.com/porter-dev/porter/cli/cmd/api"
 	"github.com/porter-dev/porter/cli/cmd/utils"
@@ -103,6 +106,20 @@ func login() error {
 	return nil
 }
 
+func check(client *api.Client) (*api.AuthCheckResponse, error) {
+	user, err := client.AuthCheck(context.Background())
+
+	if err != nil {
+		if strings.Contains(err.Error(), "403") {
+			color.Red("You are not logged in. Log in using \"porter auth login\"")
+		}
+
+		return nil, err
+	}
+
+	return user, nil
+}
+
 func register() error {
 	host := getHost()
 

+ 60 - 4
cli/cmd/project.go

@@ -4,7 +4,9 @@ import (
 	"context"
 	"fmt"
 	"os"
+	"text/tabwriter"
 
+	"github.com/fatih/color"
 	"github.com/porter-dev/porter/cli/cmd/api"
 	"github.com/spf13/cobra"
 )
@@ -24,7 +26,6 @@ var createProjectCmd = &cobra.Command{
 		err := createProject(getHost(), args[0])
 
 		if err != nil {
-			fmt.Printf("An error occurred: %v\n", err)
 			os.Exit(1)
 		}
 	},
@@ -37,7 +38,18 @@ var listProjectCmd = &cobra.Command{
 		err := listProjects(getHost())
 
 		if err != nil {
-			fmt.Printf("An error occurred: %v\n", err)
+			os.Exit(1)
+		}
+	},
+}
+
+var listProjectClustersCmd = &cobra.Command{
+	Use:   "clusters list",
+	Short: "Lists the linked clusters for a project",
+	Run: func(cmd *cobra.Command, args []string) {
+		err := listProjectClusters(getHost(), getProjectID())
+
+		if err != nil {
 			os.Exit(1)
 		}
 	},
@@ -56,6 +68,8 @@ func init() {
 	)
 
 	projectCmd.AddCommand(listProjectCmd)
+
+	projectCmd.AddCommand(listProjectClustersCmd)
 }
 
 func createProject(host string, name string) error {
@@ -77,7 +91,7 @@ func createProject(host string, name string) error {
 func listProjects(host string) error {
 	client := api.NewClient(host+"/api", "cookie.json")
 
-	user, err := client.AuthCheck(context.Background())
+	user, err := check(client)
 
 	if err != nil {
 		return err
@@ -89,9 +103,51 @@ func listProjects(host string) error {
 		return err
 	}
 
+	w := new(tabwriter.Writer)
+	w.Init(os.Stdout, 3, 8, 0, '\t', tabwriter.AlignRight)
+
+	fmt.Fprintf(w, "%s\t%s\n", "ID", "NAME")
+
+	currProjectID := getProjectID()
+
 	for _, project := range projects {
-		fmt.Println(project.Name, project.ID)
+		if currProjectID == project.ID {
+			color.New(color.FgGreen).Fprintf(w, "%d\t%s (current project)\n", project.ID, project.Name)
+		} else {
+			fmt.Fprintf(w, "%d\t%s\n", project.ID, project.Name)
+		}
+	}
+
+	w.Flush()
+
+	return nil
+}
+
+func listProjectClusters(host string, projectID uint) error {
+	client := api.NewClient(host+"/api", "cookie.json")
+
+	_, err := check(client)
+
+	if err != nil {
+		return err
 	}
 
+	clusters, err := client.ListProjectClusters(context.Background(), projectID)
+
+	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\t%s\n", "ID", "NAME", "SERVER")
+
+	for _, cluster := range clusters {
+		fmt.Fprintf(w, "%d\t%s\t%s\n", cluster.ID, cluster.Name, cluster.Server)
+	}
+
+	w.Flush()
+
 	return nil
 }

+ 1 - 1
go.mod

@@ -16,7 +16,7 @@ require (
 	github.com/docker/go-connections v0.4.0
 	github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 // indirect
 	github.com/evanphx/json-patch v4.9.0+incompatible // indirect
-	github.com/fatih/color v1.9.0 // indirect
+	github.com/fatih/color v1.9.0
 	github.com/go-chi/chi v4.1.2+incompatible
 	github.com/go-chi/cors v1.1.1
 	github.com/go-playground/locales v0.13.0