Skip to content

Commit

Permalink
Add json/yaml output to whoami (#2313)
Browse files Browse the repository at this point in the history
This will be useful in scripts to read the project ID associated with
the username, e.g.:
```
minder auth whoami -ojson | jq '.projects[] | select(.name == "jhrozek") | .projectId'
```
  • Loading branch information
jhrozek authored Feb 9, 2024
1 parent ddc5296 commit 44dba51
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
10 changes: 8 additions & 2 deletions cmd/cli/app/auth/auth_whoami.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ package auth

import (
"context"
"fmt"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/grpc"

"github.com/stacklok/minder/cmd/cli/app"
"github.com/stacklok/minder/internal/util/cli"
minderv1 "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
)
Expand All @@ -46,11 +50,13 @@ func whoamiCommand(ctx context.Context, cmd *cobra.Command, conn *grpc.ClientCon
return cli.MessageAndError("Error getting information for user", err)
}

cmd.Println(cli.Header.Render("Here are your details:"))
renderUserInfoWhoami(conn.Target(), userInfo)
renderUserInfoWhoami(conn.Target(), cmd.OutOrStderr(), viper.GetString("output"), userInfo)
return nil
}

func init() {
AuthCmd.AddCommand(whoamiCmd)

whoamiCmd.Flags().StringP("output", "o", app.Table,
fmt.Sprintf("Output format (one of %s)", strings.Join(app.SupportedOutputFormats(), ",")))
}
38 changes: 29 additions & 9 deletions cmd/cli/app/auth/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ package auth
import (
"context"
"fmt"
"io"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/stacklok/minder/cmd/cli/app"
"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 @@ -58,16 +62,32 @@ func renderUserInfo(conn string, user *minderv1.GetUserResponse) {
t.Render()
}

func renderUserInfoWhoami(conn string, user *minderv1.GetUserResponse) {
t := table.New(table.Simple, layouts.KeyValue, nil)
t.AddRow("Subject", user.GetUser().GetIdentitySubject())
t.AddRow("Created At", user.GetUser().GetCreatedAt().AsTime().String())
t.AddRow("Updated At", user.GetUser().GetUpdatedAt().AsTime().String())
t.AddRow("Minder Server", conn)
for _, project := range getProjectTableRows(user.Projects) {
t.AddRow(project...)
func renderUserInfoWhoami(conn string, outWriter io.Writer, format string, user *minderv1.GetUserResponse) {
switch format {
case app.Table:
fmt.Fprintln(outWriter, cli.Header.Render("Here are your details:"))
t := table.New(table.Simple, layouts.KeyValue, nil)
t.AddRow("Subject", user.GetUser().GetIdentitySubject())
t.AddRow("Created At", user.GetUser().GetCreatedAt().AsTime().String())
t.AddRow("Updated At", user.GetUser().GetUpdatedAt().AsTime().String())
t.AddRow("Minder Server", conn)
for _, project := range getProjectTableRows(user.Projects) {
t.AddRow(project...)
}
t.Render()
case app.JSON:
out, err := util.GetJsonFromProto(user)
if err != nil {
fmt.Fprintf(outWriter, "Error converting to JSON: %v\n", err)
}
fmt.Fprintln(outWriter, out)
case app.YAML:
out, err := util.GetYamlFromProto(user)
if err != nil {
fmt.Fprintf(outWriter, "Error converting to YAML: %v\n", err)
}
fmt.Fprintln(outWriter, out)
}
t.Render()
}

func getProjectTableRows(projects []*minderv1.Project) [][]string {
Expand Down

0 comments on commit 44dba51

Please sign in to comment.