/
list_users.go
63 lines (48 loc) · 1.31 KB
/
list_users.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
package command
import (
client "github.com/ds3lab/easeml/client/go/easemlclient"
"fmt"
"os"
"text/tabwriter"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var listUsersCmd = &cobra.Command{
Use: "users",
Short: "Lists users.",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
if apiKey == "" {
apiKey = viper.GetString("api-key")
}
serverAddress := viper.GetString("server-address")
context := client.Context{ServerAddress: serverAddress, UserCredentials: client.APIKeyCredentials{APIKey: apiKey}}
showAll, err := cmd.Flags().GetBool("all")
if err != nil {
panic(err)
}
status := ""
if showAll == false {
status = "active"
}
result, err := context.GetUsers(status)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Printf("Number of results: %d\n\n", len(result))
if len(result) > 0 {
w := tabwriter.NewWriter(os.Stdout, 0, 8, 3, ' ', 0)
fmt.Fprintln(w, "ID\tNAME\tSTATUS")
for _, r := range result {
fmt.Fprintf(w, "%s\t%s\t%s\n", r.ID, r.Name, r.Status)
}
w.Flush()
}
},
}
func init() {
listCmd.AddCommand(listUsersCmd)
//loginCmd.PersistentFlags().BoolVarP(&saveAPIKey, "save", "s", false, "Write the resulting API key to the config file.")
listUsersCmd.Flags().Bool("all", false, "Show both active and archived users.")
}