-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdiscussions.go
186 lines (160 loc) · 4.37 KB
/
discussions.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package cli
import (
"fmt"
"github.com/raystack/compass/core/namespace"
"os"
"github.com/raystack/compass/internal/client"
compassv1beta1 "github.com/raystack/compass/proto/raystack/compass/v1beta1"
"github.com/raystack/salt/printer"
"github.com/raystack/salt/term"
"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"
)
func discussionsCommand(cfg *Config) *cobra.Command {
cmd := &cobra.Command{
Use: "discussion",
Aliases: []string{"discussions"},
Short: "Manage discussions",
Annotations: map[string]string{
"group": "core",
},
Example: heredoc.Doc(`
$ compass discussion list
$ compass discussion view
$ compass discussion post
`),
}
cmd.AddCommand(
listAllDiscussionsCommand(cfg),
viewDiscussionByIDCommand(cfg),
postDiscussionCommand(cfg),
)
cmd.PersistentFlags().StringVarP(&namespaceID, "namespace", "n", namespace.DefaultNamespace.ID.String(), "namespace id or name")
return cmd
}
func listAllDiscussionsCommand(cfg *Config) *cobra.Command {
var json string
cmd := &cobra.Command{
Use: "list",
Short: "lists all discussions",
Example: heredoc.Doc(`
$ compass discussion list
`),
Annotations: map[string]string{
"action:core": "true",
},
RunE: func(cmd *cobra.Command, args []string) error {
spinner := printer.Spin("")
defer spinner.Stop()
clnt, cancel, err := client.Create(cmd.Context(), cfg.Client)
if err != nil {
return err
}
defer cancel()
ctx := client.SetMetadata(cmd.Context(), cfg.Client, namespaceID)
res, err := clnt.GetAllDiscussions(ctx, &compassv1beta1.GetAllDiscussionsRequest{})
if err != nil {
return err
}
if json != "json" {
report := [][]string{}
report = append(report, []string{"ID", "TITLE", "TYPE", "STATE"})
index := 1
for _, i := range res.GetData() {
report = append(report, []string{i.Id, i.Title, i.Type, i.State})
index++
}
printer.Table(os.Stdout, report)
fmt.Println(term.Cyanf("To view all the data in JSON format, use flag `-o json`"))
} else {
fmt.Println(term.Bluef(prettyPrint(res.GetData())))
}
return nil
},
}
cmd.Flags().StringVarP(&json, "out", "o", "table", "flag to control output viewing, for json `-o json`")
return cmd
}
func viewDiscussionByIDCommand(cfg *Config) *cobra.Command {
cmd := &cobra.Command{
Use: "view <id>",
Short: "view discussion for the given ID",
Example: heredoc.Doc(`
$ compass discussion view <id>
`),
Annotations: map[string]string{
"action:core": "true",
},
RunE: func(cmd *cobra.Command, args []string) error {
spinner := printer.Spin("")
defer spinner.Stop()
clnt, cancel, err := client.Create(cmd.Context(), cfg.Client)
if err != nil {
return err
}
defer cancel()
discussionID := args[0]
ctx := client.SetMetadata(cmd.Context(), cfg.Client, namespaceID)
res, err := clnt.GetDiscussion(ctx, &compassv1beta1.GetDiscussionRequest{
Id: discussionID,
})
if err != nil {
return err
}
spinner.Stop()
fmt.Println(term.Bluef(prettyPrint(res.GetData())))
return nil
},
}
return cmd
}
func postDiscussionCommand(cfg *Config) *cobra.Command {
var filePath string
cmd := &cobra.Command{
Use: "post",
Short: "post discussions, add ",
Example: heredoc.Doc(`
$ compass discussion post
`),
Annotations: map[string]string{
"action:core": "true",
},
RunE: func(cmd *cobra.Command, args []string) error {
spinner := printer.Spin("")
defer spinner.Stop()
var reqBody compassv1beta1.Discussion
if err := parseFile(filePath, &reqBody); err != nil {
return err
}
err := reqBody.ValidateAll()
if err != nil {
return err
}
clnt, cancel, err := client.Create(cmd.Context(), cfg.Client)
if err != nil {
return err
}
defer cancel()
ctx := client.SetMetadata(cmd.Context(), cfg.Client, namespaceID)
res, err := clnt.CreateDiscussion(ctx, &compassv1beta1.CreateDiscussionRequest{
Title: reqBody.Title,
Body: reqBody.Body,
Type: reqBody.Type,
State: reqBody.State,
Labels: reqBody.Labels,
Assets: reqBody.Assets,
})
if err != nil {
return err
}
spinner.Stop()
fmt.Println("ID: \t", term.Greenf(res.Id))
return nil
},
}
cmd.Flags().StringVarP(&filePath, "body", "b", "", "filepath to body that has to be upserted")
if err := cmd.MarkFlagRequired("body"); err != nil {
panic(err)
}
return cmd
}