-
Notifications
You must be signed in to change notification settings - Fork 674
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NOISSUE - Add user groups #1228
Changes from all commits
ef462ee
59c1b15
056f02a
1dee666
de67a01
5927f8b
3011c46
25b5352
c4e3d00
ab94a31
d7ee466
3f5dad2
29167c3
a6d217e
b3aff9c
68beace
ca5f7e8
4bb7865
be0b90d
f64f387
c901485
63ead05
cc18524
936a32f
23b5dd7
0a24e7f
a755af4
e018593
cc3d35b
5bc4828
9246672
db8221f
1a8711c
8a66eed
d6c482a
b1669d1
a49ea50
2c1a9ac
6ea0c5e
267247b
8e7beac
c21fc2b
d5482bf
d7b27ca
f69ef82
3cca7c0
4722eb0
28d4992
12416dc
1621e76
beb9344
adbdddd
d35dfb3
53c4168
a00b38d
0b036e3
d557160
a230292
8643520
531fd83
9773d2b
771c0d8
a317ce4
9d8bb65
8f90d22
76f061e
28f7d41
f49e35d
063e165
b6caf7b
0fb9195
2e1136b
9f6158b
d6cc349
7f685b7
b12dcbb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
// Copyright (c) Mainflux | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package cli | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
mfxsdk "github.com/mainflux/mainflux/pkg/sdk/go" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var cmdGroups = []cobra.Command{ | ||
cobra.Command{ | ||
Use: "create", | ||
Short: "create <JSON_group> <user_auth_token>", | ||
Long: `Creates new group | ||
JSON_group: | ||
{ | ||
"Name":<group_name>, | ||
"Description":<description>, | ||
"ParentID":<parent_id>, | ||
"Metadata":<metadata>, | ||
} | ||
Name - is unique group name | ||
ParentID - ID of a group that is a parent to the creating group | ||
Metadata - JSON structured string`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(args) != 2 { | ||
logUsage(cmd.Short) | ||
return | ||
} | ||
var group mfxsdk.Group | ||
if err := json.Unmarshal([]byte(args[0]), &group); err != nil { | ||
logError(err) | ||
return | ||
} | ||
id, err := sdk.CreateGroup(group, args[1]) | ||
if err != nil { | ||
logError(err) | ||
return | ||
} | ||
logCreated(id) | ||
}, | ||
}, | ||
cobra.Command{ | ||
Use: "get", | ||
Short: "get [all | children <group_id> | group_id] <user_auth_token>", | ||
Long: `Get all users groups, group children or group by id. | ||
all - lists all groups | ||
children <group_id> - lists all children groups of <group_id> | ||
<group_id> - shows group with provided group ID`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(args) < 2 { | ||
logUsage(cmd.Short) | ||
return | ||
} | ||
if args[0] == "all" { | ||
l, err := sdk.Groups(args[1], uint64(Offset), uint64(Limit), "") | ||
if err != nil { | ||
logError(err) | ||
return | ||
} | ||
logJSON(l) | ||
return | ||
} | ||
if args[0] == "children" { | ||
l, err := sdk.Groups(args[2], uint64(Offset), uint64(Limit), args[1]) | ||
if err != nil { | ||
logError(err) | ||
return | ||
} | ||
logJSON(l) | ||
return | ||
} | ||
t, err := sdk.Group(args[0], args[1]) | ||
if err != nil { | ||
logError(err) | ||
return | ||
} | ||
logJSON(t) | ||
}, | ||
}, | ||
cobra.Command{ | ||
Use: "assign", | ||
Short: "assign <user_id> <group_id> <user_auth_token>", | ||
Long: `Assign user to a group.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(args) != 3 { | ||
logUsage(cmd.Short) | ||
return | ||
} | ||
if err := sdk.Assign(args[0], args[1], args[2]); err != nil { | ||
logError(err) | ||
return | ||
} | ||
logOK() | ||
}, | ||
}, | ||
cobra.Command{ | ||
Use: "unassign", | ||
Short: "unassign <user_id> <group_id> <user_auth_token>", | ||
Long: `Unassign user from a group.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(args) != 3 { | ||
logUsage(cmd.Short) | ||
return | ||
} | ||
if err := sdk.Unassign(args[0], args[1], args[2]); err != nil { | ||
logError(err) | ||
return | ||
} | ||
logOK() | ||
}, | ||
}, | ||
cobra.Command{ | ||
Use: "delete", | ||
Short: "delete <group_id> <user_auth_token>", | ||
Long: `Delete users group.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(args) != 2 { | ||
logUsage(cmd.Short) | ||
return | ||
} | ||
if err := sdk.DeleteGroup(args[0], args[1]); err != nil { | ||
logError(err) | ||
return | ||
} | ||
logOK() | ||
}, | ||
}, | ||
cobra.Command{ | ||
Use: "members", | ||
Short: "members <group_id> <user_auth_token>", | ||
Long: `Lists all user members of a group.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(args) != 2 { | ||
logUsage(cmd.Short) | ||
return | ||
} | ||
up, err := sdk.Members(args[0], args[1], uint64(Offset), uint64(Limit)) | ||
if err != nil { | ||
logError(err) | ||
return | ||
} | ||
logJSON(up) | ||
}, | ||
}, | ||
cobra.Command{ | ||
Use: "membership", | ||
Short: "membership <user_id> <user_auth_token>", | ||
Comment on lines
+150
to
+151
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
Long: `List user groups membership`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(args) != 2 { | ||
logUsage(cmd.Short) | ||
return | ||
} | ||
up, err := sdk.Memberships(args[0], args[1], uint64(Offset), uint64(Limit)) | ||
if err != nil { | ||
logError(err) | ||
return | ||
} | ||
logJSON(up) | ||
}, | ||
}, | ||
} | ||
|
||
// NewGroupsCmd returns users command. | ||
func NewGroupsCmd() *cobra.Command { | ||
cmd := cobra.Command{ | ||
Use: "groups", | ||
Short: "Groups management", | ||
Long: `Groups management: create groups and assigns user to groups"`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
logUsage("Usage: Groups [create | get | delete | assign | unassign | members | membership]") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you change previous one be careful here |
||
}, | ||
} | ||
for i := range cmdGroups { | ||
cmd.AddCommand(&cmdGroups[i]) | ||
} | ||
return &cmd | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the full command?
group create ...
? Would it not be confusing when we add other entities groups (things, channels)?