Skip to content

Commit 391de6e

Browse files
committed
chore: Updated and refactored groups
1 parent b399690 commit 391de6e

3 files changed

Lines changed: 106 additions & 49 deletions

File tree

cmd/main.go

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,19 @@ func main() {
8080
Usage: "Create a new group",
8181
ArgsUsage: "[name of group]",
8282
Flags: []cli.Flag{
83-
&cli.BoolFlag{
84-
Name: "public",
85-
Aliases: []string{"p"},
86-
Value: false,
87-
Usage: "Determine if the group should be public",
83+
&cli.StringFlag{
84+
Name: "network",
85+
Aliases: []string{"net"},
86+
Usage: "Specify the network (public or private). Uses default if not specified",
8887
},
8988
},
9089
Action: func(ctx *cli.Context) error {
9190
name := ctx.Args().First()
92-
public := ctx.Bool("public")
91+
network := ctx.String("network")
9392
if name == "" {
9493
return errors.New("Group name required")
9594
}
96-
_, err := groups.CreateGroup(name, public)
95+
_, err := groups.CreateGroup(name, network)
9796
return err
9897
},
9998
},
@@ -102,11 +101,6 @@ func main() {
102101
Aliases: []string{"l"},
103102
Usage: "List groups on your account",
104103
Flags: []cli.Flag{
105-
&cli.BoolFlag{
106-
Name: "public",
107-
Aliases: []string{"p"},
108-
Usage: "List only public groups",
109-
},
110104
&cli.StringFlag{
111105
Name: "amount",
112106
Aliases: []string{"a"},
@@ -123,13 +117,18 @@ func main() {
123117
Aliases: []string{"t"},
124118
Usage: "Paginate through results using the pageToken",
125119
},
120+
&cli.StringFlag{
121+
Name: "network",
122+
Aliases: []string{"net"},
123+
Usage: "Specify the network (public or private). Uses default if not specified",
124+
},
126125
},
127126
Action: func(ctx *cli.Context) error {
128-
public := ctx.Bool("public")
129127
amount := ctx.String("amount")
130128
name := ctx.String("name")
131129
token := ctx.String("token")
132-
_, err := groups.ListGroups(amount, public, name, token)
130+
network := ctx.String("network")
131+
_, err := groups.ListGroups(amount, name, token, network)
133132
return err
134133
},
135134
},
@@ -139,26 +138,25 @@ func main() {
139138
Usage: "Update a group",
140139
ArgsUsage: "[ID of group]",
141140
Flags: []cli.Flag{
142-
&cli.BoolFlag{
143-
Name: "public",
144-
Aliases: []string{"p"},
145-
Value: false,
146-
Usage: "Determine if the group should be public",
147-
},
148141
&cli.StringFlag{
149142
Name: "name",
150143
Aliases: []string{"n"},
151144
Usage: "Update the name of a group",
152145
},
146+
&cli.StringFlag{
147+
Name: "network",
148+
Aliases: []string{"net"},
149+
Usage: "Specify the network (public or private). Uses default if not specified",
150+
},
153151
},
154152
Action: func(ctx *cli.Context) error {
155153
groupId := ctx.Args().First()
156154
name := ctx.String("name")
157-
public := ctx.Bool("public")
155+
network := ctx.String("network")
158156
if groupId == "" {
159157
return errors.New("no ID provided")
160158
}
161-
_, err := groups.UpdateGroup(groupId, name, public)
159+
_, err := groups.UpdateGroup(groupId, name, network)
162160
return err
163161
},
164162
},
@@ -167,12 +165,20 @@ func main() {
167165
Aliases: []string{"d"},
168166
Usage: "Delete a group by ID",
169167
ArgsUsage: "[ID of group]",
168+
Flags: []cli.Flag{
169+
&cli.StringFlag{
170+
Name: "network",
171+
Aliases: []string{"net"},
172+
Usage: "Specify the network (public or private). Uses default if not specified",
173+
},
174+
},
170175
Action: func(ctx *cli.Context) error {
171176
groupId := ctx.Args().First()
177+
network := ctx.String("network")
172178
if groupId == "" {
173179
return errors.New("no ID provided")
174180
}
175-
err := groups.DeleteGroup(groupId)
181+
err := groups.DeleteGroup(groupId, network)
176182
return err
177183
},
178184
},
@@ -181,12 +187,20 @@ func main() {
181187
Aliases: []string{"g"},
182188
Usage: "Get group info by ID",
183189
ArgsUsage: "[ID of group]",
190+
Flags: []cli.Flag{
191+
&cli.StringFlag{
192+
Name: "network",
193+
Aliases: []string{"net"},
194+
Usage: "Specify the network (public or private). Uses default if not specified",
195+
},
196+
},
184197
Action: func(ctx *cli.Context) error {
185198
groupId := ctx.Args().First()
199+
network := ctx.String("network")
186200
if groupId == "" {
187201
return errors.New("no ID provided")
188202
}
189-
_, err := groups.GetGroup(groupId)
203+
_, err := groups.GetGroup(groupId, network)
190204
return err
191205
},
192206
},
@@ -195,16 +209,24 @@ func main() {
195209
Aliases: []string{"a"},
196210
Usage: "Add a file to a group",
197211
ArgsUsage: "[group id] [file id]",
212+
Flags: []cli.Flag{
213+
&cli.StringFlag{
214+
Name: "network",
215+
Aliases: []string{"net"},
216+
Usage: "Specify the network (public or private). Uses default if not specified",
217+
},
218+
},
198219
Action: func(ctx *cli.Context) error {
199220
groupId := ctx.Args().First()
200221
fileId := ctx.Args().Get(1)
222+
network := ctx.String("network")
201223
if groupId == "" {
202224
return errors.New("no group id provided")
203225
}
204226
if fileId == "" {
205227
return errors.New("no file id provided")
206228
}
207-
err := groups.AddFile(groupId, fileId)
229+
err := groups.AddFile(groupId, fileId, network)
208230
return err
209231
},
210232
},
@@ -213,16 +235,24 @@ func main() {
213235
Aliases: []string{"r"},
214236
Usage: "Remove a file from a group",
215237
ArgsUsage: "[group id] [file id]",
238+
Flags: []cli.Flag{
239+
&cli.StringFlag{
240+
Name: "network",
241+
Aliases: []string{"net"},
242+
Usage: "Specify the network (public or private). Uses default if not specified",
243+
},
244+
},
216245
Action: func(ctx *cli.Context) error {
217246
groupId := ctx.Args().First()
218247
fileId := ctx.Args().Get(1)
248+
network := ctx.String("network")
219249
if groupId == "" {
220250
return errors.New("no group id provided")
221251
}
222252
if fileId == "" {
223253
return errors.New("no file id provided")
224254
}
225-
err := groups.RemoveFile(groupId, fileId)
255+
err := groups.RemoveFile(groupId, fileId, network)
226256
return err
227257
},
228258
},

internal/groups/groups.go

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,17 @@ import (
1212
"strings"
1313
)
1414

15-
func GetGroup(id string) (types.GroupCreateResponse, error) {
15+
func GetGroup(id string, network string) (types.GroupCreateResponse, error) {
1616
jwt, err := common.FindToken()
1717
if err != nil {
1818
return types.GroupCreateResponse{}, err
1919
}
20-
url := fmt.Sprintf("https://%s/v3/files/groups/%s", config.GetAPIHost(), id)
20+
networkParam, err := config.GetNetworkParam(network)
21+
if err != nil {
22+
return types.GroupCreateResponse{}, err
23+
}
24+
25+
url := fmt.Sprintf("https://%s/v3/groups/%s/%s", config.GetAPIHost(), networkParam, id)
2126

2227
req, err := http.NewRequest("GET", url, nil)
2328
if err != nil {
@@ -53,23 +58,24 @@ func GetGroup(id string) (types.GroupCreateResponse, error) {
5358

5459
}
5560

56-
func ListGroups(amount string, isPublic bool, name string, token string) (types.GroupListResponse, error) {
61+
func ListGroups(amount string, name string, token string, network string) (types.GroupListResponse, error) {
5762
jwt, err := common.FindToken()
5863
if err != nil {
5964
return types.GroupListResponse{}, err
6065
}
61-
url := fmt.Sprintf("https://%s/v3/files/groups?", config.GetAPIHost())
66+
networkParam, err := config.GetNetworkParam(network)
67+
if err != nil {
68+
return types.GroupListResponse{}, err
69+
}
70+
71+
url := fmt.Sprintf("https://%s/v3/groups/%s?", config.GetAPIHost(), networkParam)
6272

6373
params := []string{}
6474

6575
if amount != "" {
6676
params = append(params, "limit="+amount)
6777
}
6878

69-
if isPublic {
70-
params = append(params, "isPublic=true")
71-
}
72-
7379
if name != "" {
7480
params = append(params, "name="+name)
7581
}
@@ -117,15 +123,14 @@ func ListGroups(amount string, isPublic bool, name string, token string) (types.
117123

118124
}
119125

120-
func CreateGroup(name string, isPublic bool) (types.GroupCreateResponse, error) {
126+
func CreateGroup(name string, network string) (types.GroupCreateResponse, error) {
121127
jwt, err := common.FindToken()
122128
if err != nil {
123129
return types.GroupCreateResponse{}, err
124130
}
125131

126132
payload := types.GroupCreateBody{
127-
Name: name,
128-
IsPublic: isPublic,
133+
Name: name,
129134
}
130135

131136
jsonPayload, err := json.Marshal(payload)
@@ -134,7 +139,12 @@ func CreateGroup(name string, isPublic bool) (types.GroupCreateResponse, error)
134139
return types.GroupCreateResponse{}, errors.Join(err, errors.New("Failed to marshal paylod"))
135140
}
136141

137-
url := fmt.Sprintf("https://%s/v3/files/groups", config.GetAPIHost())
142+
networkParam, err := config.GetNetworkParam(network)
143+
if err != nil {
144+
return types.GroupCreateResponse{}, err
145+
}
146+
147+
url := fmt.Sprintf("https://%s/v3/groups/%s", config.GetAPIHost(), networkParam)
138148
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonPayload))
139149
if err != nil {
140150
return types.GroupCreateResponse{}, errors.Join(err, errors.New("failed to create the request"))
@@ -170,24 +180,27 @@ func CreateGroup(name string, isPublic bool) (types.GroupCreateResponse, error)
170180

171181
}
172182

173-
func UpdateGroup(id string, name string, isPublic bool) (types.GroupCreateResponse, error) {
183+
func UpdateGroup(id string, name string, network string) (types.GroupCreateResponse, error) {
174184
jwt, err := common.FindToken()
175185
if err != nil {
176186
return types.GroupCreateResponse{}, err
177187
}
178188

179189
payload := types.GroupCreateBody{
180-
Name: name,
181-
IsPublic: isPublic,
190+
Name: name,
182191
}
183192

184193
jsonPayload, err := json.Marshal(payload)
185194

186195
if err != nil {
187196
return types.GroupCreateResponse{}, errors.Join(err, errors.New("Failed to marshal paylod"))
188197
}
198+
networkParam, err := config.GetNetworkParam(network)
199+
if err != nil {
200+
return types.GroupCreateResponse{}, err
201+
}
189202

190-
url := fmt.Sprintf("https://%s/v3/files/groups/%s", config.GetAPIHost(), id)
203+
url := fmt.Sprintf("https://%s/v3/groups/%s/%s", config.GetAPIHost(), networkParam, id)
191204
req, err := http.NewRequest("PUT", url, bytes.NewBuffer(jsonPayload))
192205
if err != nil {
193206
return types.GroupCreateResponse{}, errors.Join(err, errors.New("failed to create the request"))
@@ -223,12 +236,17 @@ func UpdateGroup(id string, name string, isPublic bool) (types.GroupCreateRespon
223236

224237
}
225238

226-
func DeleteGroup(id string) error {
239+
func DeleteGroup(id string, network string) error {
227240
jwt, err := common.FindToken()
228241
if err != nil {
229242
return err
230243
}
231-
url := fmt.Sprintf("https://%s/v3/files/groups/%s", config.GetAPIHost(), id)
244+
networkParam, err := config.GetNetworkParam(network)
245+
if err != nil {
246+
return err
247+
}
248+
249+
url := fmt.Sprintf("https://%s/v3/groups/%s/%s", config.GetAPIHost(), networkParam, id)
232250

233251
req, err := http.NewRequest("DELETE", url, nil)
234252
if err != nil {
@@ -254,13 +272,18 @@ func DeleteGroup(id string) error {
254272

255273
}
256274

257-
func AddFile(groupId string, fileId string) error {
275+
func AddFile(groupId string, fileId string, network string) error {
258276

259277
jwt, err := common.FindToken()
260278
if err != nil {
261279
return err
262280
}
263-
url := fmt.Sprintf("https://%s/v3/files/groups/%s/ids/%s", config.GetAPIHost(), groupId, fileId)
281+
networkParam, err := config.GetNetworkParam(network)
282+
if err != nil {
283+
return err
284+
}
285+
286+
url := fmt.Sprintf("https://%s/v3/groups/%s/%s/ids/%s", config.GetAPIHost(), networkParam, groupId, fileId)
264287

265288
req, err := http.NewRequest("PUT", url, nil)
266289
if err != nil {
@@ -285,13 +308,18 @@ func AddFile(groupId string, fileId string) error {
285308
return nil
286309
}
287310

288-
func RemoveFile(groupId string, fileId string) error {
311+
func RemoveFile(groupId string, fileId string, network string) error {
289312

290313
jwt, err := common.FindToken()
291314
if err != nil {
292315
return err
293316
}
294-
url := fmt.Sprintf("https://%s/v3/files/groups/%s/ids/%s", config.GetAPIHost(), groupId, fileId)
317+
networkParam, err := config.GetNetworkParam(network)
318+
if err != nil {
319+
return err
320+
}
321+
322+
url := fmt.Sprintf("https://%s/v3/groups/%s/%s/ids/%s", config.GetAPIHost(), networkParam, groupId, fileId)
295323

296324
req, err := http.NewRequest("DELETE", url, nil)
297325
if err != nil {

internal/types/types.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ type ListResponse struct {
5353

5454
type GroupResponseItem struct {
5555
Id string `json:"id"`
56-
IsPublic bool `json:"is_public"`
5756
Name string `json:"name"`
5857
CreatedAt string `json:"created_at"`
5958
}

0 commit comments

Comments
 (0)