Skip to content

Commit

Permalink
Print multiple projects in auth login subcommand (#1649)
Browse files Browse the repository at this point in the history
Similar to PR #1648 this prints the projects into
multiple rows, now for the `auth login` subcommand. This created a function that is
re-used.
  • Loading branch information
JAORMX authored Nov 14, 2023
1 parent 6274d1c commit e7f570d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 22 deletions.
16 changes: 2 additions & 14 deletions cmd/cli/app/auth/auth_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"fmt"
"net/http"
"net/url"
"strings"
"time"

"github.com/charmbracelet/bubbles/table"
Expand Down Expand Up @@ -222,26 +221,15 @@ func renderNewUser(cmd *cobra.Command, conn *grpc.ClientConn, newUser *pb.Create
}

func renderUserInfo(cmd *cobra.Command, conn *grpc.ClientConn, user *pb.GetUserResponse) {
projects := []string{}
for _, project := range user.Projects {
projects = append(projects, project.GetName())
}

minderSrvKey := "Minder Server"
projectKey := "Project Name"
if len(projects) > 1 {
projectKey += "s"
}

rows := []table.Row{
{
projectKey, strings.Join(projects, ", "),
},
{
minderSrvKey, conn.Target(),
},
}

rows = append(rows, getProjectTableRows(user.Projects)...)

renderUserToTable(cmd, rows)
}

Expand Down
9 changes: 1 addition & 8 deletions cmd/cli/app/auth/auth_whoami.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,7 @@ func renderUserInfoWhoami(cmd *cobra.Command, conn *grpc.ClientConn, user *pb.Ge
},
}

projectKey := "Project"
for idx, project := range user.Projects {
if len(user.Projects) > 1 {
projectKey = fmt.Sprintf("Project #%d", idx+1)
}
projectVal := fmt.Sprintf("%s / %s", project.GetName(), project.GetProjectId())
rows = append(rows, table.Row{projectKey, projectVal})
}
rows = append(rows, getProjectTableRows(user.Projects)...)

renderUserToTable(cmd, rows)
}
43 changes: 43 additions & 0 deletions cmd/cli/app/auth/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// 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.

// NOTE: This file is for stubbing out client code for proof of concept
// purposes. It will / should be removed in the future.
// Until then, it is not covered by unit tests and should not be used
// It does make a good example of how to use the generated client code
// for others to use as a reference.

package auth

import (
"fmt"

"github.com/charmbracelet/bubbles/table"

minderv1 "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
)

func getProjectTableRows(projects []*minderv1.Project) []table.Row {
rows := []table.Row{}
projectKey := "Project"
for idx, project := range projects {
if len(projects) > 1 {
projectKey = fmt.Sprintf("Project #%d", idx+1)
}
projectVal := fmt.Sprintf("%s / %s", project.GetName(), project.GetProjectId())
rows = append(rows, table.Row{projectKey, projectVal})
}
return rows
}

0 comments on commit e7f570d

Please sign in to comment.