-
Notifications
You must be signed in to change notification settings - Fork 4
/
list.go
81 lines (66 loc) · 1.97 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package project
import (
"fmt"
"os"
project "github.com/fuseml/fuseml-core/gen/project"
"github.com/fuseml/fuseml-core/pkg/cli/client"
"github.com/fuseml/fuseml-core/pkg/cli/common"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"
)
// ListOptions holds the options for 'project list' sub command
type ListOptions struct {
client.Clients
global *common.GlobalOptions
format *common.FormattingOptions
}
// custom formatting handler used to format project users
func formatUsers(object interface{}, column string, field interface{}) (formated string) {
if project, ok := object.(*project.Project); ok {
for _, user := range project.Users {
if formated != "" {
formated += "\n"
}
formated += fmt.Sprintf("- name: %s\n email: %s", user.Name, user.Email)
}
}
return
}
// NewListOptions initializes a ListOptions struct
func NewListOptions(o *common.GlobalOptions) (res *ListOptions) {
res = &ListOptions{global: o}
res.format = common.NewFormattingOptions(
[]string{"Name", "Description", "Users"},
[]table.SortBy{{Name: "Name", Mode: table.Asc}},
common.OutputFormatters{"Users": formatUsers},
)
return
}
// NewSubCmdProjectList creates and returns the cobra command for the `project list` CLI command
func NewSubCmdProjectList(gOpt *common.GlobalOptions) *cobra.Command {
o := NewListOptions(gOpt)
cmd := &cobra.Command{
Use: "list",
Short: "List all projects.",
Long: `Retrieve information about Projects registered in FuseML`,
Run: func(cmd *cobra.Command, args []string) {
common.CheckErr(o.InitializeClients(gOpt.URL, gOpt.Timeout, gOpt.Verbose))
common.CheckErr(o.validate())
common.CheckErr(o.run())
},
Args: cobra.ExactArgs(0),
}
o.format.AddMultiValueFormattingFlags(cmd)
return cmd
}
func (o *ListOptions) validate() error {
return nil
}
func (o *ListOptions) run() error {
projects, err := o.ProjectClient.List()
if err != nil {
return err
}
o.format.FormatValue(os.Stdout, projects)
return nil
}