Skip to content

Commit

Permalink
Added GetGroupIDBYName and AddGroupMembers options (#23)
Browse files Browse the repository at this point in the history
* Added GetGroupIDBYName and AddGroupMembers options

* Modified README.md for new commands

* Removed GetGroupIDByName. Corrections AddGroupMembers and README

* Corrections for print and error messages in AddGroupMembers

* Corrections to AddGroupMembers
  • Loading branch information
Euquimides authored Nov 25, 2024
1 parent 2b8abc5 commit b952ac7
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/main/groovy/org/craftercms/cli/Main.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.craftercms.cli

import org.craftercms.cli.commands.AddEnvironment
import org.craftercms.cli.commands.group.AddGroupMembers
import org.craftercms.cli.commands.group.CreateGroup
import org.craftercms.cli.commands.group.ListGroups
import org.craftercms.cli.commands.marketplace.CopyPlugin
Expand All @@ -30,7 +31,7 @@ import picocli.CommandLine
name = 'crafter-cli', usageHelpAutoWidth = true,
versionProvider = VersionProvider.class, mixinStandardHelpOptions = true,
subcommands = [CommandLine.HelpCommand, AddEnvironment, AddRemote, CreateSite, ListRemotes, SyncFrom, SyncTo,
ListSites, CopyPlugin, CreateUser, ListUsers, CreateAccessToken, PublishContent, CreateGroup, ListGroups])
ListSites, CopyPlugin, CreateUser, ListUsers, CreateAccessToken, PublishContent, CreateGroup, ListGroups, AddGroupMembers])
class Main {

static def main(args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.craftercms.cli.commands.group

import org.craftercms.cli.commands.AbstractCommand
import picocli.CommandLine

@CommandLine.Command(name = 'add-group-members', description = 'Adds one or more users to a group')
class AddGroupMembers extends AbstractCommand {

@CommandLine.Spec
CommandLine.Model.CommandSpec commandSpec

@CommandLine.Option(names = ['-gn', '--group-name'], required = true, description = 'Group name. Required if not bulk importing from a file.')
String groupName

@CommandLine.Option(names = ['-u', '--users'], split = ',', required = true, description = 'Comma separated list of users to add')
String[] users

def validateParameters() {
if (!groupName) {
throw new CommandLine.ParameterException(commandSpec.commandLine(), 'Missing required option group-name')
}

if (!users) {
throw new CommandLine.ParameterException(commandSpec.commandLine(), 'Missing required option users')
}
}

@Override
def run(client) {
validateParameters()
addMembers(client, groupName, users)
}

/**
* Add one or more users to a group
* @param client HTTPClient object
* @param groupName group name
* @param users list of users to add
*/
static def addMembers(client, groupName, users) {
def response = getGroupIdByName(client, groupName)

if (!response || !response.group) {
println "Error retrieving group '${groupName}'"
return
}

def groupId = response.group.id
def path = "/studio/api/2/groups/${groupId}/members"
def body = [usernames: users]

def result = client.post(path, body)
if (!result) {
println "Failed to add members '${users}' to group '${groupName}'"
return
}

println "Members '${users}' added to group '${groupName}' succesfully"
}

/**
* Get a group by name
* @param client HTTPClient object
* @param groupName group name
*/
static def getGroupIdByName(client, groupName) {
def path = "/studio/api/2/groups/by_name/${groupName}"
def result = client.get(path)
if (!result) {
println "Failed to retrieve group '${groupName}'"
return
}

if (!result.group) {
println "Failed to retrieve group '${groupName}', error '${result.message}'"
return
}

return result
}
}

0 comments on commit b952ac7

Please sign in to comment.