Skip to content
This repository was archived by the owner on Mar 24, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions cmd/rig/cmd/completions/completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"connectrpc.com/connect"
"github.com/rigdev/rig-go-api/api/v1/capsule"
"github.com/rigdev/rig-go-api/api/v1/environment"
"github.com/rigdev/rig-go-api/api/v1/group"
"github.com/rigdev/rig-go-api/api/v1/project"
"github.com/rigdev/rig-go-sdk"
"github.com/rigdev/rig/cmd/rig/cmd/cmdconfig"
Expand Down Expand Up @@ -41,6 +42,10 @@ func formatEnvironment(e *environment.Environment) string {
return fmt.Sprintf("%v\t (Operator Version: %v)", e.GetEnvironmentId(), operatorVersion)
}

func formatGroup(g *group.Group) string {
return fmt.Sprintf("%s\t (#Members: %v)", g.GetGroupId(), g.GetNumMembers())
}

func Contexts(
toComplete string,
config *cmdconfig.Config,
Expand All @@ -64,14 +69,9 @@ func Environments(
ctx context.Context,
rig rig.Client,
toComplete string,
scope scope.Scope,
) ([]string, cobra.ShellCompDirective) {
var environmentIDs []string

if scope.GetCurrentContext() == nil || scope.GetCurrentContext().GetAuth() == nil {
return nil, cobra.ShellCompDirectiveError
}

resp, err := rig.Environment().List(ctx, &connect.Request[environment.ListRequest]{
Msg: &environment.ListRequest{},
})
Expand All @@ -96,14 +96,9 @@ func Projects(
ctx context.Context,
rig rig.Client,
toComplete string,
scope scope.Scope,
) ([]string, cobra.ShellCompDirective) {
var projectIDs []string

if scope.GetCurrentContext() == nil || scope.GetCurrentContext().GetAuth() == nil {
return nil, cobra.ShellCompDirectiveError
}

resp, err := rig.Project().List(ctx, &connect.Request[project.ListRequest]{
Msg: &project.ListRequest{},
})
Expand Down Expand Up @@ -154,6 +149,27 @@ func Capsules(
return capsuleIDs, cobra.ShellCompDirectiveDefault
}

func Groups(ctx context.Context,
rig rig.Client,
toComplete string,
) ([]string, cobra.ShellCompDirective) {
completions := []string{}
resp, err := rig.Group().List(ctx, &connect.Request[group.ListRequest]{
Msg: &group.ListRequest{},
})
if err != nil {
return nil, cobra.ShellCompDirectiveError
}

for _, g := range resp.Msg.GetGroups() {
if strings.HasPrefix(g.GetGroupId(), toComplete) {
completions = append(completions, formatGroup(g))
}
}

return completions, cobra.ShellCompDirectiveNoFileComp
}

func OutputType(_ *cobra.Command,
_ []string,
toComplete string) ([]string, cobra.ShellCompDirective) {
Expand Down
22 changes: 11 additions & 11 deletions cmd/rig/cmd/config/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ import (
"go.uber.org/fx"
)

var minify bool

var (
field string
value string
contextName string
)

type CmdWScope struct {
fx.In

Expand All @@ -26,14 +34,6 @@ type CmdWScope struct {
Prompter common.Prompter
}

var minify bool

var (
field string
value string
contextName string
)

var cmdWScope CmdWScope

func initCmdWScope(c CmdWScope) {
Expand Down Expand Up @@ -216,7 +216,7 @@ func (c *CmdNoScope) completions(
s *cli.SetupContext,
) ([]string, cobra.ShellCompDirective) {

if err := s.ExecuteInvokes(cmd, args, initCmdWScope); err != nil {
if err := s.ExecuteInvokes(cmd, args, initCmdNoScope); err != nil {
return nil, cobra.ShellCompDirectiveError
}

Expand All @@ -234,7 +234,7 @@ func (c *CmdWScope) useProjectCompletion(
return nil, cobra.ShellCompDirectiveError
}

return completions.Projects(ctx, c.Rig, toComplete, c.Scope)
return completions.Projects(ctx, c.Rig, toComplete)
}

func (c *CmdWScope) useEnvironmentCompletion(
Expand All @@ -248,5 +248,5 @@ func (c *CmdWScope) useEnvironmentCompletion(
return nil, cobra.ShellCompDirectiveError
}

return completions.Environments(ctx, c.Rig, toComplete, c.Scope)
return completions.Environments(ctx, c.Rig, toComplete)
}
2 changes: 1 addition & 1 deletion cmd/rig/cmd/environment/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,5 @@ func (c *Cmd) completeEnvironment(
return nil, cobra.ShellCompDirectiveError
}

return completions.Environments(ctx, c.Rig, toComplete, c.Scope)
return completions.Environments(ctx, c.Rig, toComplete)
}
2 changes: 1 addition & 1 deletion cmd/rig/cmd/group/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ func (c *Cmd) create(ctx context.Context, cmd *cobra.Command, args []string) err
return err
}

cmd.Println("Created group:", groupID, res.Msg.GetGroup().GetGroupId())
cmd.Println("Created group", res.Msg.GetGroup().GetGroupId())
return nil
}
2 changes: 1 addition & 1 deletion cmd/rig/cmd/group/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/spf13/cobra"
)

func (c *Cmd) get(ctx context.Context, cmd *cobra.Command, args []string) error {
func (c *Cmd) list(ctx context.Context, cmd *cobra.Command, args []string) error {
if len(args) > 0 {
identifier := ""
if len(args) > 0 {
Expand Down
42 changes: 13 additions & 29 deletions cmd/rig/cmd/group/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (
"strings"

"connectrpc.com/connect"
"github.com/rigdev/rig-go-api/api/v1/group"
"github.com/rigdev/rig-go-api/api/v1/service_account"
"github.com/rigdev/rig-go-api/api/v1/user"
"github.com/rigdev/rig-go-api/model"
"github.com/rigdev/rig-go-sdk"
"github.com/rigdev/rig/cmd/common"
"github.com/rigdev/rig/cmd/rig/cmd/completions"
"github.com/rigdev/rig/cmd/rig/services/auth"
"github.com/rigdev/rig/pkg/cli"
"github.com/rigdev/rig/pkg/cli/scope"
"github.com/spf13/cobra"
"go.uber.org/fx"
)
Expand All @@ -28,18 +29,23 @@ type Cmd struct {

Rig rig.Client
Prompter common.Prompter
Scope scope.Scope
}

var cmd Cmd

func initCmd(c Cmd) {
cmd = c
cmd.Rig = c.Rig
cmd.Scope = c.Scope
cmd.Prompter = c.Prompter
}

func Setup(parent *cobra.Command, s *cli.SetupContext) {
group := &cobra.Command{
Use: "group",
Short: "Manage groups",
Use: "group",
Short: "Manage role groups",
Long: "Groups are a way to organize users and service accounts into groups with certain roles, where " +
"the roles assigned to a group are inherited by all members of the group.",
PersistentPreRunE: s.MakeInvokePreRunE(initCmd),
Annotations: map[string]string{
auth.OmitProject: "",
Expand All @@ -52,7 +58,7 @@ func Setup(parent *cobra.Command, s *cli.SetupContext) {
Use: "create [group-id]",
Short: "Create a new group",
RunE: cli.CtxWrap(cmd.create),
Args: cobra.NoArgs,
Args: cobra.MaximumNArgs(1),
}
group.AddCommand(create)

Expand Down Expand Up @@ -84,7 +90,7 @@ func Setup(parent *cobra.Command, s *cli.SetupContext) {
Use: "list",
Short: "list groups",
Aliases: []string{"ls"},
RunE: cli.CtxWrap(cmd.get),
RunE: cli.CtxWrap(cmd.list),
}
list.Flags().IntVarP(&limit, "limit", "l", 10, "limit the number of groups to return")
list.Flags().IntVar(&offset, "offset", 0, "offset the number of groups to return")
Expand Down Expand Up @@ -158,29 +164,7 @@ func (c *Cmd) completions(
return nil, cobra.ShellCompDirectiveError
}

completions := []string{}
resp, err := c.Rig.Group().List(ctx, &connect.Request[group.ListRequest]{
Msg: &group.ListRequest{},
})
if err != nil {
return nil, cobra.ShellCompDirectiveError
}

for _, g := range resp.Msg.GetGroups() {
if strings.HasPrefix(g.GetGroupId(), toComplete) {
completions = append(completions, formatGroup(g))
}
}

if len(completions) == 0 {
return nil, cobra.ShellCompDirectiveError
}

return completions, cobra.ShellCompDirectiveNoFileComp
}

func formatGroup(g *group.Group) string {
return fmt.Sprintf("%s\t (#Members: %v)", g.GetGroupId(), g.GetNumMembers())
return completions.Groups(ctx, c.Rig, toComplete)
}

func (c *Cmd) memberCompletions(
Expand Down
33 changes: 31 additions & 2 deletions cmd/rig/cmd/noop/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,21 @@ func initCmd(c Cmd) {
cc = c
}

type CmdNoRig struct {
fx.In

Prompter common.Prompter
}

var ccNoRig CmdNoRig

func initCmdNoRig(c CmdNoRig) {
ccNoRig = c
}

func Setup(parent *cobra.Command, s *cli.SetupContext) {
cmd := &cobra.Command{
Use: "noop",
PersistentPreRunE: s.MakeInvokePreRunE(initCmd),
Use: "noop",
}
cmd1 := &cobra.Command{
Use: "cmd1",
Expand All @@ -49,10 +60,28 @@ func Setup(parent *cobra.Command, s *cli.SetupContext) {
}
cmd.AddCommand(cmd2)

cmd3 := &cobra.Command{
Use: "cmd3",
PersistentPreRunE: s.MakeInvokePreRunE(initCmdNoRig),
Annotations: map[string]string{
auth.OmitUser: "",
auth.OmitEnvironment: "",
auth.OmitCapsule: "",
auth.OmitProject: "",
},
RunE: ccNoRig.noop,
}
cmd.AddCommand(cmd3)

parent.AddCommand(cmd)
}

func (c *Cmd) noop(_ *cobra.Command, _ []string) error {
fmt.Println("noop")
return nil
}

func (c *CmdNoRig) noop(_ *cobra.Command, _ []string) error {
fmt.Println("noop")
return nil
}
69 changes: 69 additions & 0 deletions cmd/rig/cmd/role/assign.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package role

import (
"context"

"connectrpc.com/connect"
"github.com/rigdev/rig-go-api/api/v1/group"
"github.com/rigdev/rig-go-api/api/v1/role"
"github.com/spf13/cobra"
)

func (c *Cmd) assign(ctx context.Context, cmd *cobra.Command, args []string) error {
var roleID string
var groupID string
// Get role ID
if len(args) == 0 {
resp, err := c.Rig.Role().List(ctx, connect.NewRequest(&role.ListRequest{}))
if err != nil {
return err
}

var roleIDs []string
for _, r := range resp.Msg.GetRoles() {
roleIDs = append(roleIDs, r.GetRoleId())
}

_, roleID, err = c.Prompter.Select("Select role:", roleIDs)
if err != nil {
return err
}
} else {
roleID = args[0]
}

// Get group ID
if len(args) < 2 {
resp, err := c.Rig.Group().List(ctx, connect.NewRequest(&group.ListRequest{}))
if err != nil {
return err
}

var groupIDs []string
for _, g := range resp.Msg.GetGroups() {
groupIDs = append(groupIDs, g.GetGroupId())
}

_, groupID, err = c.Prompter.Select("Select group:", groupIDs)
if err != nil {
return err
}
} else {
groupID = args[1]
}

if _, err := c.Rig.Role().Assign(ctx, connect.NewRequest(&role.AssignRequest{
RoleId: roleID,
EntityId: &role.EntityID{
Kind: &role.EntityID_GroupId{
GroupId: groupID,
},
},
})); err != nil {
return err
}

cmd.Println("Assigned role:", roleID, "to group:", groupID)

return nil
}
Loading