|
| 1 | +package permissions |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/UpCloudLtd/upcloud-cli/v3/internal/commands" |
| 7 | + "github.com/UpCloudLtd/upcloud-cli/v3/internal/completion" |
| 8 | + "github.com/UpCloudLtd/upcloud-cli/v3/internal/config" |
| 9 | + "github.com/UpCloudLtd/upcloud-cli/v3/internal/format" |
| 10 | + "github.com/UpCloudLtd/upcloud-cli/v3/internal/namedargs" |
| 11 | + "github.com/UpCloudLtd/upcloud-cli/v3/internal/output" |
| 12 | + "github.com/UpCloudLtd/upcloud-cli/v3/internal/ui" |
| 13 | + "github.com/UpCloudLtd/upcloud-go-api/v6/upcloud" |
| 14 | + "github.com/UpCloudLtd/upcloud-go-api/v6/upcloud/request" |
| 15 | + "github.com/jedib0t/go-pretty/v6/text" |
| 16 | + "github.com/spf13/pflag" |
| 17 | +) |
| 18 | + |
| 19 | +// ListCommand creates the 'permissions list' command |
| 20 | +func ListCommand() commands.Command { |
| 21 | + return &listCommand{ |
| 22 | + BaseCommand: commands.New("list", "List permissions", "upctl account show"), |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +type listCommand struct { |
| 27 | + *commands.BaseCommand |
| 28 | + username string |
| 29 | +} |
| 30 | + |
| 31 | +// InitCommand implements Command.InitCommand |
| 32 | +func (l *listCommand) InitCommand() { |
| 33 | + flagSet := &pflag.FlagSet{} |
| 34 | + flagSet.StringVar(&l.username, "username", "", "Filter permissions by username.") |
| 35 | + |
| 36 | + l.AddFlags(flagSet) |
| 37 | +} |
| 38 | + |
| 39 | +func (l *listCommand) InitCommandWithConfig(cfg *config.Config) { |
| 40 | + _ = l.Cobra().RegisterFlagCompletionFunc("username", namedargs.CompletionFunc(completion.Username{}, cfg)) |
| 41 | +} |
| 42 | + |
| 43 | +func (l *listCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Output, error) { |
| 44 | + svc := exec.All() |
| 45 | + permissions, err := svc.GetPermissions(exec.Context(), &request.GetPermissionsRequest{}) |
| 46 | + if err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + |
| 50 | + filtered := make([]upcloud.Permission, 0) |
| 51 | + rows := []output.TableRow{} |
| 52 | + for _, permission := range permissions { |
| 53 | + if permission.User == l.username || l.username == "" { |
| 54 | + filtered = append(filtered, permission) |
| 55 | + rows = append(rows, output.TableRow{ |
| 56 | + permission.User, |
| 57 | + permission.TargetType, |
| 58 | + permission.TargetIdentifier, |
| 59 | + permission.Options, |
| 60 | + }) |
| 61 | + } |
| 62 | + } |
| 63 | + return output.MarshaledWithHumanOutput{ |
| 64 | + Value: filtered, |
| 65 | + Output: output.Table{ |
| 66 | + Columns: []output.TableColumn{ |
| 67 | + {Key: "username", Header: "Username"}, |
| 68 | + {Key: "target_type", Header: "Target type"}, |
| 69 | + {Key: "target_identifier", Header: "Target identifier", Colour: ui.DefaultUUUIDColours}, |
| 70 | + {Key: "options", Header: "Options", Format: formatOptions}, |
| 71 | + }, |
| 72 | + Rows: rows, |
| 73 | + }, |
| 74 | + }, nil |
| 75 | +} |
| 76 | + |
| 77 | +func formatOptions(val interface{}) (text.Colors, string, error) { |
| 78 | + options, ok := val.(*upcloud.PermissionOptions) |
| 79 | + if !ok { |
| 80 | + return nil, "", fmt.Errorf("cannot parse %T, expected *upcloud.PermissionOptions", val) |
| 81 | + } |
| 82 | + |
| 83 | + if options == nil { |
| 84 | + return nil, "", nil |
| 85 | + } |
| 86 | + |
| 87 | + colors, value, _ := format.Boolean(options.Storage) |
| 88 | + return nil, fmt.Sprintf("Storage: %s", colors.Sprint(value)), nil |
| 89 | +} |
0 commit comments