generated from datumforge/go-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
group_update.go
85 lines (64 loc) · 2.05 KB
/
group_update.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
package datumgroup
import (
"context"
"encoding/json"
"github.com/spf13/cobra"
"github.com/spf13/viper"
datum "github.com/datumforge/datum/cmd/cli/cmd"
"github.com/datumforge/datum/pkg/datumclient"
)
var groupUpdateCmd = &cobra.Command{
Use: "update",
Short: "Update an existing datum group",
RunE: func(cmd *cobra.Command, args []string) error {
return updateGroup(cmd.Context())
},
}
func init() {
groupCmd.AddCommand(groupUpdateCmd)
groupUpdateCmd.Flags().StringP("id", "i", "", "group id to update")
datum.ViperBindFlag("group.update.id", groupUpdateCmd.Flags().Lookup("id"))
groupUpdateCmd.Flags().StringP("name", "n", "", "name of the group")
datum.ViperBindFlag("group.update.name", groupUpdateCmd.Flags().Lookup("name"))
groupUpdateCmd.Flags().StringP("short-name", "s", "", "display name of the group")
datum.ViperBindFlag("group.update.short-name", groupUpdateCmd.Flags().Lookup("short-name"))
groupUpdateCmd.Flags().StringP("description", "d", "", "description of the group")
datum.ViperBindFlag("group.update.description", groupUpdateCmd.Flags().Lookup("description"))
}
func updateGroup(ctx context.Context) error {
// setup datum http client
cli, err := datum.GetGraphClient(ctx)
if err != nil {
return err
}
// save session cookies on function exit
client, _ := cli.Client.(*datumclient.Client)
defer datum.StoreSessionCookies(client)
var s []byte
gID := viper.GetString("group.update.id")
if gID == "" {
return datum.NewRequiredFieldMissingError("group id")
}
name := viper.GetString("group.update.name")
displayName := viper.GetString("group.update.short-name")
description := viper.GetString("group.update.description")
input := datumclient.UpdateGroupInput{}
if name != "" {
input.Name = &name
}
if displayName != "" {
input.DisplayName = &displayName
}
if description != "" {
input.Description = &description
}
o, err := cli.Client.UpdateGroup(ctx, gID, input, cli.Interceptor)
if err != nil {
return err
}
s, err = json.Marshal(o)
if err != nil {
return err
}
return datum.JSONPrint(s)
}