From 08e8fbf01f0647e57b33e6b80664d412ff18dc40 Mon Sep 17 00:00:00 2001 From: Juan Antonio Osorio Date: Thu, 21 Dec 2023 15:32:10 +0200 Subject: [PATCH] Replace table layout strings with type (#2007) This makes it easier to verify and better ensures we use the right type. --- cmd/cli/app/artifact/artifact_get.go | 7 ++-- cmd/cli/app/artifact/artifact_list.go | 4 ++- cmd/cli/app/auth/common.go | 7 ++-- cmd/cli/app/profile/table_render.go | 9 ++--- cmd/cli/app/repo/repo_list.go | 3 +- cmd/cli/app/repo/repo_register.go | 3 +- cmd/cli/app/ruletype/common.go | 3 +- internal/util/cli/table/layouts/constants.go | 38 ++++++++++++++++++++ internal/util/cli/table/simple/simple.go | 21 ++++++----- internal/util/cli/table/table.go | 10 ++---- 10 files changed, 77 insertions(+), 28 deletions(-) create mode 100644 internal/util/cli/table/layouts/constants.go diff --git a/cmd/cli/app/artifact/artifact_get.go b/cmd/cli/app/artifact/artifact_get.go index 23ecf89a5c..38902c5a9d 100644 --- a/cmd/cli/app/artifact/artifact_get.go +++ b/cmd/cli/app/artifact/artifact_get.go @@ -31,6 +31,7 @@ import ( "github.com/stacklok/minder/internal/util" "github.com/stacklok/minder/internal/util/cli" "github.com/stacklok/minder/internal/util/cli/table" + "github.com/stacklok/minder/internal/util/cli/table/layouts" minderv1 "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1" ) @@ -70,7 +71,8 @@ func getCommand(ctx context.Context, cmd *cobra.Command, conn *grpc.ClientConn) switch format { case app.Table: - ta := table.New(table.Simple, "", []string{"ID", "Type", "Owner", "Name", "Repository", "Visibility", "Creation date"}) + ta := table.New(table.Simple, layouts.Default, + []string{"ID", "Type", "Owner", "Name", "Repository", "Visibility", "Creation date"}) ta.AddRow([]string{ art.ArtifactPk, art.Type, @@ -82,7 +84,8 @@ func getCommand(ctx context.Context, cmd *cobra.Command, conn *grpc.ClientConn) }) ta.Render() - tv := table.New(table.Simple, "", []string{"ID", "Tags", "Signature", "Identity", "Creation date"}) + tv := table.New(table.Simple, layouts.Default, + []string{"ID", "Tags", "Signature", "Identity", "Creation date"}) for _, version := range versions { tv.AddRow([]string{ fmt.Sprintf("%d", version.VersionId), diff --git a/cmd/cli/app/artifact/artifact_list.go b/cmd/cli/app/artifact/artifact_list.go index 9058de629f..4d4eafd139 100644 --- a/cmd/cli/app/artifact/artifact_list.go +++ b/cmd/cli/app/artifact/artifact_list.go @@ -29,6 +29,7 @@ import ( "github.com/stacklok/minder/internal/util" "github.com/stacklok/minder/internal/util/cli" "github.com/stacklok/minder/internal/util/cli/table" + "github.com/stacklok/minder/internal/util/cli/table/layouts" minderv1 "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1" ) @@ -73,7 +74,8 @@ func listCommand(ctx context.Context, cmd *cobra.Command, conn *grpc.ClientConn) switch format { case app.Table: - t := table.New(table.Simple, "", []string{"ID", "Type", "Owner", "Name", "Repository", "Visibility", "Creation date"}) + t := table.New(table.Simple, layouts.Default, + []string{"ID", "Type", "Owner", "Name", "Repository", "Visibility", "Creation date"}) for _, artifact := range artifactList.Results { t.AddRow([]string{ artifact.ArtifactPk, diff --git a/cmd/cli/app/auth/common.go b/cmd/cli/app/auth/common.go index a4fd9557e5..657bc38a8b 100644 --- a/cmd/cli/app/auth/common.go +++ b/cmd/cli/app/auth/common.go @@ -22,6 +22,7 @@ import ( "google.golang.org/grpc/status" "github.com/stacklok/minder/internal/util/cli/table" + "github.com/stacklok/minder/internal/util/cli/table/layouts" minderv1 "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1" ) @@ -39,7 +40,7 @@ func userRegistered(ctx context.Context, client minderv1.UserServiceClient) (boo } func renderNewUser(conn string, newUser *minderv1.CreateUserResponse) { - t := table.New(table.Simple, "keyvalue", nil) + t := table.New(table.Simple, layouts.KeyValue, nil) t.AddRow([]string{"Project ID", newUser.ProjectId}) t.AddRow([]string{"Project Name", newUser.ProjectName}) t.AddRow([]string{"Minder Server", conn}) @@ -47,7 +48,7 @@ func renderNewUser(conn string, newUser *minderv1.CreateUserResponse) { } func renderUserInfo(conn string, user *minderv1.GetUserResponse) { - t := table.New(table.Simple, "keyvalue", nil) + t := table.New(table.Simple, layouts.KeyValue, nil) t.AddRow([]string{"Minder Server", conn}) for _, project := range getProjectTableRows(user.Projects) { t.AddRow(project) @@ -56,7 +57,7 @@ func renderUserInfo(conn string, user *minderv1.GetUserResponse) { } func renderUserInfoWhoami(conn string, user *minderv1.GetUserResponse) { - t := table.New(table.Simple, "keyvalue", nil) + t := table.New(table.Simple, layouts.KeyValue, nil) t.AddRow([]string{"Subject", user.GetUser().GetIdentitySubject()}) t.AddRow([]string{"Created At", user.GetUser().GetCreatedAt().AsTime().String()}) t.AddRow([]string{"Updated At", user.GetUser().GetUpdatedAt().AsTime().String()}) diff --git a/cmd/cli/app/profile/table_render.go b/cmd/cli/app/profile/table_render.go index c93034411f..2f59146db1 100644 --- a/cmd/cli/app/profile/table_render.go +++ b/cmd/cli/app/profile/table_render.go @@ -24,6 +24,7 @@ import ( "gopkg.in/yaml.v2" "github.com/stacklok/minder/internal/util/cli/table" + "github.com/stacklok/minder/internal/util/cli/table/layouts" minderv1 "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1" ) @@ -54,7 +55,7 @@ const ( // NewProfileSettingsTable creates a new table for rendering profile settings func NewProfileSettingsTable() table.Table { - return table.New(table.Simple, "profile_settings", nil) + return table.New(table.Simple, layouts.ProfileSettings, nil) } // RenderProfileSettingsTable renders the profile settings table @@ -68,7 +69,7 @@ func RenderProfileSettingsTable(p *minderv1.Profile, t table.Table) { // NewProfileTable creates a new table for rendering profiles func NewProfileTable() table.Table { - return table.New(table.Simple, "profile", nil) + return table.New(table.Simple, layouts.Profile, nil) } // RenderProfileTable renders the profile table @@ -109,7 +110,7 @@ func renderRuleTable(entType minderv1.EntityType, rule *minderv1.Profile_Rule, t // NewProfileStatusTable creates a new table for rendering profile status func NewProfileStatusTable() table.Table { - return table.New(table.Simple, "profile_status", nil) + return table.New(table.Simple, layouts.ProfileStatus, nil) } // RenderProfileStatusTable renders the profile status table @@ -146,7 +147,7 @@ func getEvalStatusColor(status string) string { // NewRuleEvaluationsTable creates a new table for rendering rule evaluations func NewRuleEvaluationsTable() table.Table { - return table.New(table.Simple, "rule_evaluations", nil) + return table.New(table.Simple, layouts.RuleEvaluations, nil) } // RenderRuleEvaluationStatusTable renders the rule evaluations table diff --git a/cmd/cli/app/repo/repo_list.go b/cmd/cli/app/repo/repo_list.go index 7c0641febc..e23a8b298f 100644 --- a/cmd/cli/app/repo/repo_list.go +++ b/cmd/cli/app/repo/repo_list.go @@ -28,6 +28,7 @@ import ( "github.com/stacklok/minder/internal/util" "github.com/stacklok/minder/internal/util/cli" "github.com/stacklok/minder/internal/util/cli/table" + "github.com/stacklok/minder/internal/util/cli/table/layouts" minderv1 "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1" ) @@ -68,7 +69,7 @@ func listCommand(ctx context.Context, cmd *cobra.Command, conn *grpc.ClientConn) switch format { case app.Table: - t := table.New(table.Simple, "repolist", nil) + t := table.New(table.Simple, layouts.RepoList, nil) for _, v := range resp.Results { t.AddRow([]string{ *v.Id, diff --git a/cmd/cli/app/repo/repo_register.go b/cmd/cli/app/repo/repo_register.go index c33f7da8f6..95abf6762f 100644 --- a/cmd/cli/app/repo/repo_register.go +++ b/cmd/cli/app/repo/repo_register.go @@ -36,6 +36,7 @@ import ( "github.com/stacklok/minder/cmd/cli/app" "github.com/stacklok/minder/internal/util/cli" "github.com/stacklok/minder/internal/util/cli/table" + "github.com/stacklok/minder/internal/util/cli/table/layouts" minderv1 "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1" ) @@ -134,7 +135,7 @@ func RegisterCmd(ctx context.Context, cmd *cobra.Command, conn *grpc.ClientConn) // Register the repos // The result gives a list of repositories with the registration status // Let's parse the results and print the status - t := table.New(table.Simple, "", []string{"Repository", "Status", "Message"}) + t := table.New(table.Simple, layouts.Default, []string{"Repository", "Status", "Message"}) for _, result := range results { row := []string{fmt.Sprintf("%s/%s", result.Repository.Owner, result.Repository.Name)} if result.Status.Success { diff --git a/cmd/cli/app/ruletype/common.go b/cmd/cli/app/ruletype/common.go index 4e58aab435..ccfc1f2bb8 100644 --- a/cmd/cli/app/ruletype/common.go +++ b/cmd/cli/app/ruletype/common.go @@ -28,6 +28,7 @@ import ( "github.com/stacklok/minder/internal/util" "github.com/stacklok/minder/internal/util/cli" "github.com/stacklok/minder/internal/util/cli/table" + "github.com/stacklok/minder/internal/util/cli/table/layouts" minderv1 "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1" ) @@ -100,5 +101,5 @@ func shouldSkipFile(f string) bool { // initializeTable initializes the table for the rule type func initializeTable() table.Table { - return table.New(table.Simple, "ruletype", nil) + return table.New(table.Simple, layouts.RuleType, nil) } diff --git a/internal/util/cli/table/layouts/constants.go b/internal/util/cli/table/layouts/constants.go new file mode 100644 index 0000000000..9329ddd373 --- /dev/null +++ b/internal/util/cli/table/layouts/constants.go @@ -0,0 +1,38 @@ +// Copyright 2023 Stacklok, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Package layouts defines the available table layouts +package layouts + +// TableLayout is the type for table layouts +type TableLayout string + +const ( + // KeyValue is the key value table layout + KeyValue TableLayout = "keyvalue" + // RuleType is the rule type table layout + RuleType TableLayout = "ruletype" + // ProfileSettings is the profile settings table layout + ProfileSettings TableLayout = "profile_settings" + // Profile is the profile table layout + Profile TableLayout = "profile" + // RepoList is the repo list table layout + RepoList TableLayout = "repolist" + // ProfileStatus is the profile status table layout + ProfileStatus TableLayout = "profile_status" + // RuleEvaluations is the rule evaluations table layout + RuleEvaluations TableLayout = "rule_evaluations" + // Default is the default table layout + Default TableLayout = "" +) diff --git a/internal/util/cli/table/simple/simple.go b/internal/util/cli/table/simple/simple.go index b34dd3c25b..7343e3e948 100644 --- a/internal/util/cli/table/simple/simple.go +++ b/internal/util/cli/table/simple/simple.go @@ -19,6 +19,8 @@ import ( "os" "github.com/olekukonko/tablewriter" + + "github.com/stacklok/minder/internal/util/cli/table/layouts" ) // Table is a wrapper around tablewriter.Table @@ -27,23 +29,26 @@ type Table struct { } // New creates a new table with the given header -func New(layout string, header []string) *Table { +func New(layout layouts.TableLayout, header []string) *Table { table := tablewriter.NewWriter(os.Stdout) switch layout { - case "keyvalue": + case layouts.KeyValue: keyValueLayout(table) - case "ruletype": + case layouts.RuleType: ruleTypeLayout(table) - case "profile_settings": + case layouts.ProfileSettings: profileSettingsLayout(table) - case "profile": + case layouts.Profile: profileLayout(table) - case "repolist": + case layouts.RepoList: repoListLayout(table) - case "profile_status": + case layouts.ProfileStatus: profileStatusLayout(table) - case "rule_evaluations": + case layouts.RuleEvaluations: ruleEvaluationsLayout(table) + case layouts.Default: + table.SetHeader(header) + defaultLayout(table) default: table.SetHeader(header) defaultLayout(table) diff --git a/internal/util/cli/table/table.go b/internal/util/cli/table/table.go index 5d37ffe548..fbd0b8f6cf 100644 --- a/internal/util/cli/table/table.go +++ b/internal/util/cli/table/table.go @@ -16,6 +16,7 @@ package table import ( + "github.com/stacklok/minder/internal/util/cli/table/layouts" "github.com/stacklok/minder/internal/util/cli/table/simple" ) @@ -40,11 +41,6 @@ type Table interface { } // New creates a new table -func New(tableType, layout string, header []string) Table { - switch tableType { - case Simple: - return simple.New(layout, header) - default: - return simple.New(layout, header) - } +func New(_ string, layout layouts.TableLayout, header []string) Table { + return simple.New(layout, header) }