Skip to content

Commit

Permalink
Replace table layout strings with type (#2007)
Browse files Browse the repository at this point in the history
This makes it easier to verify and better ensures we use the right type.
  • Loading branch information
JAORMX authored Dec 21, 2023
1 parent 5d598bf commit 08e8fbf
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 28 deletions.
7 changes: 5 additions & 2 deletions cmd/cli/app/artifact/artifact_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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,
Expand All @@ -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),
Expand Down
4 changes: 3 additions & 1 deletion cmd/cli/app/artifact/artifact_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions cmd/cli/app/auth/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -39,15 +40,15 @@ 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})
t.Render()
}

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)
Expand All @@ -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()})
Expand Down
9 changes: 5 additions & 4 deletions cmd/cli/app/profile/table_render.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion cmd/cli/app/repo/repo_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion cmd/cli/app/repo/repo_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion cmd/cli/app/ruletype/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
38 changes: 38 additions & 0 deletions internal/util/cli/table/layouts/constants.go
Original file line number Diff line number Diff line change
@@ -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 = ""
)
21 changes: 13 additions & 8 deletions internal/util/cli/table/simple/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
10 changes: 3 additions & 7 deletions internal/util/cli/table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
}

0 comments on commit 08e8fbf

Please sign in to comment.