-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minder CLI - New commands for auth invite - list, accept <code> and d…
…ecline <code> (#3551) * Implement minder auth invite CLI commands Signed-off-by: Radoslav Dimitrov <radoslav@stacklok.com> * Add expiration to the list output cli command Signed-off-by: Radoslav Dimitrov <radoslav@stacklok.com> * Hide the auth invite commands until it's fully implemented in the server Signed-off-by: Radoslav Dimitrov <radoslav@stacklok.com> * Update cmd/cli/app/auth/invite/invite.go Co-authored-by: Evan Anderson <evan@stacklok.com> * Fix missing comma Signed-off-by: Radoslav Dimitrov <radoslav@stacklok.com> --------- Signed-off-by: Radoslav Dimitrov <radoslav@stacklok.com> Co-authored-by: Evan Anderson <evan@stacklok.com>
- Loading branch information
1 parent
52a5da6
commit 74b17cf
Showing
5 changed files
with
250 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// Copyright 2024 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 ( | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/stacklok/minder/cmd/cli/app/auth" | ||
) | ||
|
||
// inviteCmd represents the offline-token set of sub-commands | ||
var inviteCmd = &cobra.Command{ | ||
Hidden: true, // TODO: This hides the command, remove it once it's implemented | ||
Use: "invite", | ||
Short: "Manage user invitations", | ||
Long: `The minder auth invite command lets you manage (accept/decline/list) your invitations.`, | ||
RunE: func(cmd *cobra.Command, _ []string) error { | ||
return cmd.Usage() | ||
}, | ||
} | ||
|
||
func init() { | ||
auth.AuthCmd.AddCommand(inviteCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// | ||
// 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" | ||
|
||
"github.com/spf13/cobra" | ||
"google.golang.org/grpc" | ||
|
||
"github.com/stacklok/minder/internal/util/cli" | ||
minderv1 "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1" | ||
) | ||
|
||
// inviteAcceptCmd represents the accept command | ||
var inviteAcceptCmd = &cobra.Command{ | ||
Hidden: true, // TODO: This hides the command, remove it once it's implemented | ||
Use: "accept", | ||
Short: "Accept a pending invitation", | ||
Long: `Accept a pending invitation for the current minder user`, | ||
RunE: cli.GRPCClientWrapRunE(inviteAcceptCommand), | ||
Args: cobra.ExactArgs(1), | ||
} | ||
|
||
// inviteAcceptCommand is the whoami subcommand | ||
func inviteAcceptCommand(ctx context.Context, cmd *cobra.Command, args []string, conn *grpc.ClientConn) error { | ||
client := minderv1.NewUserServiceClient(conn) | ||
code := args[0] | ||
// 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 | ||
|
||
res, err := client.ResolveInvitation(ctx, &minderv1.ResolveInvitationRequest{ | ||
Accept: true, | ||
Code: code, | ||
}) | ||
if err != nil { | ||
return cli.MessageAndError("Error resolving invitation", err) | ||
} | ||
cmd.Printf("Invitation %s for %s to become %s of project %s was accepted!\n", code, res.Email, res.Role, res.Project) | ||
return nil | ||
} | ||
|
||
func init() { | ||
inviteCmd.AddCommand(inviteAcceptCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// | ||
// 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" | ||
|
||
"github.com/spf13/cobra" | ||
"google.golang.org/grpc" | ||
|
||
"github.com/stacklok/minder/internal/util/cli" | ||
minderv1 "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1" | ||
) | ||
|
||
// inviteDeclineCmd represents the decline command | ||
var inviteDeclineCmd = &cobra.Command{ | ||
Hidden: true, // TODO: This hides the command, remove it once it's implemented | ||
Use: "decline", | ||
Short: "Declines a pending invitation", | ||
Long: `Declines a pending invitation for the current minder user`, | ||
RunE: cli.GRPCClientWrapRunE(inviteDeclineCommand), | ||
Args: cobra.ExactArgs(1), | ||
} | ||
|
||
// inviteAcceptCommand is the whoami subcommand | ||
func inviteDeclineCommand(ctx context.Context, cmd *cobra.Command, args []string, conn *grpc.ClientConn) error { | ||
client := minderv1.NewUserServiceClient(conn) | ||
code := args[0] | ||
// 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 | ||
|
||
res, err := client.ResolveInvitation(ctx, &minderv1.ResolveInvitationRequest{ | ||
Accept: false, | ||
Code: code, | ||
}) | ||
if err != nil { | ||
return cli.MessageAndError("Error resolving invitation", err) | ||
} | ||
cmd.Printf("Invitation %s for %s to become %s of project %s was declined!\n", code, res.Email, res.Role, res.Project) | ||
return nil | ||
} | ||
|
||
func init() { | ||
inviteCmd.AddCommand(inviteDeclineCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// | ||
// 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" | ||
) | ||
|
||
// inviteListCmd represents the list command | ||
var inviteListCmd = &cobra.Command{ | ||
Hidden: true, // TODO: This hides the command, remove it once it's implemented | ||
Use: "list", | ||
Short: "List pending invitations", | ||
Long: `List shows all pending invitations for the current minder user`, | ||
RunE: cli.GRPCClientWrapRunE(inviteListCommand), | ||
} | ||
|
||
// inviteListCommand is the whoami subcommand | ||
func inviteListCommand(ctx context.Context, cmd *cobra.Command, _ []string, conn *grpc.ClientConn) error { | ||
client := minderv1.NewUserServiceClient(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{}) | ||
if err != nil { | ||
return cli.MessageAndError("Error listing invitations", 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", "Role", "Expires", "Code"}) | ||
for _, v := range res.Invitations { | ||
t.AddRow(v.SponsorDisplay, v.Project, v.Role, v.ExpiresAt.AsTime().Format(time.RFC3339), v.Code) | ||
} | ||
t.Render() | ||
default: | ||
return fmt.Errorf("unsupported output format: %s", format) | ||
} | ||
return nil | ||
} | ||
|
||
func init() { | ||
inviteCmd.AddCommand(inviteListCmd) | ||
|
||
inviteListCmd.Flags().StringP("output", "o", app.Table, | ||
fmt.Sprintf("Output format (one of %s)", strings.Join(app.SupportedOutputFormats(), ","))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters