-
Notifications
You must be signed in to change notification settings - Fork 8
/
user_profile_command.go
139 lines (113 loc) · 3.94 KB
/
user_profile_command.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"fmt"
"github.com/landoop/bite"
"github.com/spf13/cobra"
)
func init() {
app.AddCommand(newUserGroupCommand())
}
func newUserGroupCommand() *cobra.Command {
root := &cobra.Command{
Use: "user",
Short: "List information about the authenticated logged user such as the given roles given by the lenses administrator or work with the user's profile",
Example: "user",
TraverseChildren: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
if user := client.User; user.Name != "" {
// if logged in using the user password, then we have those info,
// let's print it as well.
return bite.PrintObject(cmd, user)
}
return nil
},
}
bite.CanPrintJSON(root)
root.AddCommand(newUserProfileGroupCommand())
return root
}
func newUserProfileGroupCommand() *cobra.Command {
rootSub := &cobra.Command{
Use: "profile",
Short: "List the user-specific favourites, if any",
Example: "user profile",
TraverseChildren: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
profile, err := client.GetUserProfile()
if err != nil {
return err
}
if bite.ExpectsFeedback(cmd) && len(profile.Topics)+len(profile.Schemas)+len(profile.Transformers) == 0 {
// do not throw error, it's not an error.
return bite.PrintInfo(cmd, "Currently, there is no user profile available.")
}
return bite.PrintObject(cmd, profile)
},
}
bite.CanBeSilent(rootSub)
bite.CanPrintJSON(rootSub)
rootSub.AddCommand(newCreateUserProfilePropertyValueCommand())
rootSub.AddCommand(newDeleteUserProfilePropertyValueCommand())
return rootSub
}
func walkPropertyValueFromArgs(args []string, actionFunc func(property, value string) error) error {
if len(args) < 2 {
return fmt.Errorf("at least two arguments are required, the first is the property name and the second is the actual property's value")
}
for i, n := 0, len(args); i < n; i++ {
property := args[i]
i++
if i >= n {
break
}
value := args[i]
if err := actionFunc(property, value); err != nil {
return err
}
}
return nil
}
func newCreateUserProfilePropertyValueCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "set",
Aliases: []string{"add", "insert", "create", "update"},
Short: `Add a "value" to the user profile "property" entries`,
Example: "user profile set newProperty newValueToTheProperty",
TraverseChildren: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
return walkPropertyValueFromArgs(args, func(property, value string) error {
if err := client.CreateUserProfilePropertyValue(property, value); err != nil {
bite.FriendlyError(cmd, errResourceInternal, "unable to add a user profile value '%s' for property '%s'", value, property)
return err
}
return bite.PrintInfo(cmd, "User profile value: '%s' for property: '%s' added", value, property)
})
},
}
bite.CanBeSilent(cmd)
return cmd
}
func newDeleteUserProfilePropertyValueCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Aliases: []string{"remove"},
Short: `Remove the "value" from the user profile "property" entries`,
Example: "user profile delete existingProperty existingValueFromProperty",
TraverseChildren: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
return walkPropertyValueFromArgs(args, func(property, value string) error {
if err := client.DeleteUserProfilePropertyValue(property, value); err != nil {
bite.FriendlyError(cmd, errResourceInternal, "unable to remove the user profile value '%s' from property '%s'", value, property)
return err
}
return bite.PrintInfo(cmd, "User profile value: '%s' from property: '%s' removed", value, property)
})
},
}
bite.CanBeSilent(cmd)
return cmd
}