Skip to content

Commit

Permalink
NOISSUE - Fix assign and unassign commands in CLI (absmach#2102)
Browse files Browse the repository at this point in the history
Signed-off-by: Arvindh <[email protected]>
  • Loading branch information
arvindh123 authored Feb 26, 2024
1 parent 9ed0fe0 commit eb90526
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 121 deletions.
152 changes: 93 additions & 59 deletions cli/channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,36 +185,64 @@ var cmdChannels = []cobra.Command{
},
},
{
Use: "assign user <relation> <user_ids> <channel_id> <user_auth_token>",
Short: "Assign user",
Long: "Assign user to a channel\n" +
Use: "users <channel_id> <user_auth_token>",
Short: "List users",
Long: "List users of a channel\n" +
"Usage:\n" +
"\tmagistrala-cli channels assign user <relation> '[\"<user_id_1>\", \"<user_id_2>\"]' <channel_id> $USERTOKEN\n",
"\tmagistrala-cli channels users <channel_id> $USERTOKEN\n",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 5 {
if len(args) != 2 {
logUsage(cmd.Use)
return
}
var userIDs []string
if err := json.Unmarshal([]byte(args[1]), &userIDs); err != nil {
pm := mgxsdk.PageMetadata{
Offset: Offset,
Limit: Limit,
}
ul, err := sdk.ListChannelUsers(args[0], pm, args[1])
if err != nil {
logError(err)
return
}
if err := sdk.AddUserToChannel(args[2], mgxsdk.UsersRelationRequest{Relation: args[0], UserIDs: userIDs}, args[3]); err != nil {

logJSON(ul)
},
},
{
Use: "groups <channel_id> <user_auth_token>",
Short: "List groups",
Long: "List groups of a channel\n" +
"Usage:\n" +
"\tmagistrala-cli channels groups <channel_id> $USERTOKEN\n",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 2 {
logUsage(cmd.Use)
return
}
pm := mgxsdk.PageMetadata{
Offset: Offset,
Limit: Limit,
}
ul, err := sdk.ListChannelUserGroups(args[0], pm, args[1])
if err != nil {
logError(err)
return
}
logOK()

logJSON(ul)
},
},
}

var channelAssignCmds = []cobra.Command{
{
Use: "unassign user <relation> <user_ids> <channel_id> <user_auth_token>",
Short: "Unassign user",
Long: "Unassign user from a channel\n" +
Use: "users <relation> <user_ids> <channel_id> <user_auth_token>",
Short: "Assign users",
Long: "Assign users to a channel\n" +
"Usage:\n" +
"\tmagistrala-cli channels unassign user <relation> '[\"<user_id_1>\", \"<user_id_2>\"]' <channel_id> $USERTOKEN\n",
"\tmagistrala-cli channels assign users <relation> '[\"<user_id_1>\", \"<user_id_2>\"]' <channel_id> $USERTOKEN\n",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 5 {
if len(args) != 4 {
logUsage(cmd.Use)
return
}
Expand All @@ -223,21 +251,22 @@ var cmdChannels = []cobra.Command{
logError(err)
return
}
if err := sdk.RemoveUserFromChannel(args[2], mgxsdk.UsersRelationRequest{Relation: args[0], UserIDs: userIDs}, args[3]); err != nil {
if err := sdk.AddUserToChannel(args[2], mgxsdk.UsersRelationRequest{Relation: args[0], UserIDs: userIDs}, args[3]); err != nil {
logError(err)
return
}
logOK()
},
},
{
Use: "assign group <group_ids> <channel_id> <user_auth_token>",
Short: "Assign group",
Long: "Assign group to a channel\n" +
Use: "groups <group_ids> <channel_id> <user_auth_token>",
Short: "Assign groups",
Long: "Assign groups to a channel\n" +
"Usage:\n" +
"\tmagistrala-cli channels assign group '[\"<group_id_1>\", \"<group_id_2>\"]' <channel_id> $USERTOKEN\n",
"\tmagistrala-cli channels assign groups '[\"<group_id_1>\", \"<group_id_2>\"]' <channel_id> $USERTOKEN\n",

Run: func(cmd *cobra.Command, args []string) {
if len(args) != 5 {
if len(args) != 3 {
logUsage(cmd.Use)
return
}
Expand All @@ -253,14 +282,17 @@ var cmdChannels = []cobra.Command{
logOK()
},
},
}

var channelUnassignCmds = []cobra.Command{
{
Use: "unassign group <group_ids> <channel_id> <user_auth_token>",
Short: "Unassign group",
Long: "Unassign group from a channel\n" +
Use: "groups <group_ids> <channel_id> <user_auth_token>",
Short: "Unassign groups",
Long: "Unassign groups from a channel\n" +
"Usage:\n" +
"\tmagistrala-cli channels unassign group '[\"<group_id_1>\", \"<group_id_2>\"]' <channel_id> $USERTOKEN\n",
"\tmagistrala-cli channels unassign groups '[\"<group_id_1>\", \"<group_id_2>\"]' <channel_id> $USERTOKEN\n",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 5 {
if len(args) != 3 {
logUsage(cmd.Use)
return
}
Expand All @@ -276,56 +308,56 @@ var cmdChannels = []cobra.Command{
logOK()
},
},

{
Use: "users <channel_id> <user_auth_token>",
Short: "List users",
Long: "List users of a channel\n" +
Use: "users <relation> <user_ids> <channel_id> <user_auth_token>",
Short: "Unassign users",
Long: "Unassign users from a channel\n" +
"Usage:\n" +
"\tmagistrala-cli channels users <channel_id> $USERTOKEN\n",
"\tmagistrala-cli channels unassign users <relation> '[\"<user_id_1>\", \"<user_id_2>\"]' <channel_id> $USERTOKEN\n",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 2 {
if len(args) != 4 {
logUsage(cmd.Use)
return
}
pm := mgxsdk.PageMetadata{
Offset: Offset,
Limit: Limit,
}
ul, err := sdk.ListChannelUsers(args[0], pm, args[1])
if err != nil {
var userIDs []string
if err := json.Unmarshal([]byte(args[1]), &userIDs); err != nil {
logError(err)
return
}

logJSON(ul)
},
},
{
Use: "groups <channel_id> <user_auth_token>",
Short: "List groups",
Long: "List groups of a channel\n" +
"Usage:\n" +
"\tmagistrala-cli channels groups <channel_id> $USERTOKEN\n",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 2 {
logUsage(cmd.Use)
return
}
pm := mgxsdk.PageMetadata{
Offset: Offset,
Limit: Limit,
}
ul, err := sdk.ListChannelUserGroups(args[0], pm, args[1])
if err != nil {
if err := sdk.RemoveUserFromChannel(args[2], mgxsdk.UsersRelationRequest{Relation: args[0], UserIDs: userIDs}, args[3]); err != nil {
logError(err)
return
}

logJSON(ul)
logOK()
},
},
}

func NewChannelAssignCmds() *cobra.Command {
cmd := cobra.Command{
Use: "assign [users | groups]",
Short: "Assign users or groups to a channel",
Long: "Assign users or groups to a channel",
}
for i := range channelAssignCmds {
cmd.AddCommand(&channelAssignCmds[i])
}
return &cmd
}

func NewChannelUnassignCmds() *cobra.Command {
cmd := cobra.Command{
Use: "unassign [users | groups]",
Short: "Unassign users or groups from a channel",
Long: "Unassign users or groups from a channel",
}
for i := range channelUnassignCmds {
cmd.AddCommand(&channelUnassignCmds[i])
}
return &cmd
}

// NewChannelsCmd returns channels command.
func NewChannelsCmd() *cobra.Command {
cmd := cobra.Command{
Expand All @@ -338,5 +370,7 @@ func NewChannelsCmd() *cobra.Command {
cmd.AddCommand(&cmdChannels[i])
}

cmd.AddCommand(NewChannelAssignCmds())
cmd.AddCommand(NewChannelUnassignCmds())
return &cmd
}
2 changes: 1 addition & 1 deletion cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func ParseConfig(sdkConf mgxsdk.Config) (mgxsdk.Config, error) {
ReaderURL: "http://localhost",
HTTPAdapterURL: "http://localhost/http:9016",
BootstrapURL: "http://localhost",
CertsURL: "https://localhost:9019",
CertsURL: "http://localhost:9019",
TLSVerification: false,
},
}
Expand Down
59 changes: 45 additions & 14 deletions cli/domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,47 +171,52 @@ var cmdDomains = []cobra.Command{
logOK()
},
},
}

var domainAssignCmds = []cobra.Command{
{
Use: "assign user <relation> <user_ids> <domain_id> <token>",
Short: "Assign user",
Long: "Assign user to a domain\n" +
Use: "users <relation> <user_ids> <domain_id> <token>",
Short: "Assign users",
Long: "Assign users to a domain\n" +
"Usage:\n" +
"\tmagistrala-cli groups assign user <relation> '[\"<user_id_1>\", \"<user_id_2>\"]' <domain_id> $TOKEN\n",
"\tmagistrala-cli groups assign users <relation> '[\"<user_id_1>\", \"<user_id_2>\"]' <domain_id> $TOKEN\n",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 5 {
if len(args) != 4 {
logUsage(cmd.Use)
return
}
var userIDs []string
if err := json.Unmarshal([]byte(args[2]), &userIDs); err != nil {
if err := json.Unmarshal([]byte(args[1]), &userIDs); err != nil {
logError(err)
return
}
if err := sdk.AddUserToDomain(args[3], mgxsdk.UsersRelationRequest{Relation: args[1], UserIDs: userIDs}, args[4]); err != nil {
if err := sdk.AddUserToDomain(args[2], mgxsdk.UsersRelationRequest{Relation: args[0], UserIDs: userIDs}, args[3]); err != nil {
logError(err)
return
}
logOK()
},
},
}

var domainUnassignCmds = []cobra.Command{
{
Use: "unassign user <relation> <user_ids> <domain_id> <token>",
Short: "Unassign user",
Long: "Unassign user from a domain\n" +
Use: "users <relation> <user_ids> <domain_id> <token>",
Short: "Unassign users",
Long: "Unassign users from a domain\n" +
"Usage:\n" +
"\tmagistrala-cli groups unassign user <relation> '[\"<user_id_1>\", \"<user_id_2>\"]' <domain_id> $TOKEN\n",
"\tmagistrala-cli groups unassign users <relation> '[\"<user_id_1>\", \"<user_id_2>\"]' <domain_id> $TOKEN\n",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 5 {
if len(args) != 4 {
logUsage(cmd.Use)
return
}
var userIDs []string
if err := json.Unmarshal([]byte(args[2]), &userIDs); err != nil {
if err := json.Unmarshal([]byte(args[1]), &userIDs); err != nil {
logError(err)
return
}
if err := sdk.RemoveUserFromDomain(args[3], mgxsdk.UsersRelationRequest{Relation: args[1], UserIDs: userIDs}, args[4]); err != nil {
if err := sdk.RemoveUserFromDomain(args[2], mgxsdk.UsersRelationRequest{Relation: args[0], UserIDs: userIDs}, args[3]); err != nil {
logError(err)
return
}
Expand All @@ -220,6 +225,30 @@ var cmdDomains = []cobra.Command{
},
}

func NewDomainAssignCmds() *cobra.Command {
cmd := cobra.Command{
Use: "assign [users]",
Short: "Assign users to a domain",
Long: "Assign users to a domain",
}
for i := range domainAssignCmds {
cmd.AddCommand(&domainAssignCmds[i])
}
return &cmd
}

func NewDomainUnassignCmds() *cobra.Command {
cmd := cobra.Command{
Use: "unassign [users]",
Short: "Unassign users from a domain",
Long: "Unassign users from a domain",
}
for i := range domainUnassignCmds {
cmd.AddCommand(&domainUnassignCmds[i])
}
return &cmd
}

// NewDomainsCmd returns domains command.
func NewDomainsCmd() *cobra.Command {
cmd := cobra.Command{
Expand All @@ -232,5 +261,7 @@ func NewDomainsCmd() *cobra.Command {
cmd.AddCommand(&cmdDomains[i])
}

cmd.AddCommand(NewDomainAssignCmds())
cmd.AddCommand(NewDomainUnassignCmds())
return &cmd
}
Loading

0 comments on commit eb90526

Please sign in to comment.