Skip to content

Commit

Permalink
Update the auth invite CLI commands (#3674)
Browse files Browse the repository at this point in the history
Signed-off-by: Radoslav Dimitrov <[email protected]>
  • Loading branch information
rdimitrov authored Jun 19, 2024
1 parent cfeaed8 commit 247627b
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cmd/cli/app/auth/invite/invite_accept.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var inviteAcceptCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
}

// inviteAcceptCommand is the whoami subcommand
// inviteAcceptCommand is the "invite accept" subcommand
func inviteAcceptCommand(ctx context.Context, cmd *cobra.Command, args []string, conn *grpc.ClientConn) error {
client := minderv1.NewUserServiceClient(conn)
code := args[0]
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/app/auth/invite/invite_decline.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var inviteDeclineCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
}

// inviteAcceptCommand is the whoami subcommand
// inviteDeclineCommand is the "invite decline" subcommand
func inviteDeclineCommand(ctx context.Context, cmd *cobra.Command, args []string, conn *grpc.ClientConn) error {
client := minderv1.NewUserServiceClient(conn)
code := args[0]
Expand Down
90 changes: 90 additions & 0 deletions cmd/cli/app/auth/invite/invite_get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//
// 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 invite provides the auth invite command for the minder CLI.
package invite

import (
"context"
"fmt"
"strings"
"time"

"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"
"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"
)

// inviteGetCmd represents the list command
var inviteGetCmd = &cobra.Command{
Hidden: true, // TODO: This hides the command, remove it once it's implemented
Use: "get",
Short: "Get info about pending invitations",
Long: `Get shows additional information about a pending invitation`,
RunE: cli.GRPCClientWrapRunE(inviteGetCommand),
Args: cobra.ExactArgs(1),
}

// inviteGetCommand is the invite get subcommand
func inviteGetCommand(ctx context.Context, cmd *cobra.Command, args []string, conn *grpc.ClientConn) error {
client := minderv1.NewInviteServiceClient(conn)

// No longer print usage on returned error, since we've parsed our inputs
// See https://github.com/spf13/cobra/issues/340#issuecomment-374617413
cmd.SilenceUsage = true
format := viper.GetString("output")

res, err := client.GetInviteDetails(ctx, &minderv1.GetInviteDetailsRequest{
Code: args[0],
})
if err != nil {
return cli.MessageAndError("Error getting info for invitation", err)
}

switch format {
case app.JSON:
out, err := util.GetJsonFromProto(res)
if err != nil {
return cli.MessageAndError("Error getting json from proto", err)
}
cmd.Println(out)
case app.YAML:
out, err := util.GetYamlFromProto(res)
if err != nil {
return cli.MessageAndError("Error getting yaml from proto", err)
}
cmd.Println(out)
case app.Table:
t := table.New(table.Simple, layouts.Default, []string{"Sponsor", "Project", "Expires"})
t.AddRow(res.SponsorDisplay, res.ProjectDisplay, res.ExpiresAt.AsTime().Format(time.RFC3339))
t.Render()
default:
return fmt.Errorf("unsupported output format: %s", format)
}
return nil
}

func init() {
inviteCmd.AddCommand(inviteGetCmd)
inviteGetCmd.Flags().StringP("output", "o", app.Table,
fmt.Sprintf("Output format (one of %s)", strings.Join(app.SupportedOutputFormats(), ",")))
}
6 changes: 4 additions & 2 deletions cmd/cli/app/auth/invite/invite_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ func inviteListCommand(ctx context.Context, cmd *cobra.Command, _ []string, conn
// No longer print usage on returned error, since we've parsed our inputs
// See https://github.com/spf13/cobra/issues/340#issuecomment-374617413
cmd.SilenceUsage = true

format := viper.GetString("output")

res, err := client.ListInvitations(ctx, &minderv1.ListInvitationsRequest{})
Expand All @@ -72,6 +71,10 @@ func inviteListCommand(ctx context.Context, cmd *cobra.Command, _ []string, conn
}
cmd.Println(out)
case app.Table:
if len(res.Invitations) == 0 {
cmd.Println("No pending invitations")
return nil
}
t := table.New(table.Simple, layouts.Default, []string{"Sponsor", "Project", "Role", "Expires", "Code"})
for _, v := range res.Invitations {
t.AddRow(v.SponsorDisplay, v.Project, v.Role, v.ExpiresAt.AsTime().Format(time.RFC3339), v.Code)
Expand All @@ -85,7 +88,6 @@ func inviteListCommand(ctx context.Context, cmd *cobra.Command, _ []string, conn

func init() {
inviteCmd.AddCommand(inviteListCmd)

inviteListCmd.Flags().StringP("output", "o", app.Table,
fmt.Sprintf("Output format (one of %s)", strings.Join(app.SupportedOutputFormats(), ",")))
}

0 comments on commit 247627b

Please sign in to comment.