-
Notifications
You must be signed in to change notification settings - Fork 6
/
list.go
60 lines (51 loc) · 1.36 KB
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package account
import (
"fmt"
"strings"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
"github.com/jedib0t/go-pretty/v6/text"
)
// ListCommand creates the 'account list' command
func ListCommand() commands.Command {
return &listCommand{
BaseCommand: commands.New("list", "List sub-accounts", "upctl account list"),
}
}
type listCommand struct {
*commands.BaseCommand
}
// ExecuteWithoutArguments implements commands.NoArgumentCommand
func (l *listCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Output, error) {
svc := exec.Account()
accounts, err := svc.GetAccountList(exec.Context())
if err != nil {
return nil, err
}
rows := []output.TableRow{}
for _, a := range accounts {
rows = append(rows, output.TableRow{
a.Username,
a.Type,
a.Roles.Role,
})
}
return output.MarshaledWithHumanOutput{
Value: accounts,
Output: output.Table{
Columns: []output.TableColumn{
{Key: "username", Header: "Username"},
{Key: "type", Header: "Type"},
{Key: "roles", Header: "Roles", Format: formatRoles},
},
Rows: rows,
},
}, nil
}
func formatRoles(val interface{}) (text.Colors, string, error) {
roles, ok := val.([]string)
if !ok {
return nil, "", fmt.Errorf("cannot parse %T, expected []string", val)
}
return nil, strings.Join(roles, ", "), nil
}