Skip to main content

Examples

The API for scripts is capable of a number of different tasks. This section provides examples for script writing, publishing and executing them.

The simple-shell-example project in the scripting section of the example project includes a number of JSON file with simple scripts:

maven.json

simplest script to create a hosted Maven repository

npm.json

simple script to create a hosted and proxy repository as well as a repository group for npm usage

bower.json

simple script to create a hosted and proxy repository as well as a repository group for bower usage

anonymous.json

parameterized script to enable or disable anonymous access

Simple shell scripts are added to contain the curl invocations to manage scripts via the REST API:

create.sh

Upload a specified JSON file

delete.sh

Delete a script specified by its name

list.sh

List all deployed scripts

run.sh

Run a script specified by its name

setAnonymous.sh

Run the anonymous script on the server with the parameter true or false

update.sh

Update an existing script by specifying the name and the JSON file to use for the update

An example sequence of creating and running a script is:

./create.sh maven.json
./run.sh maven

Subsequently you could list all scripts and delete the maven script with:

./list.sh
./delete.sh maven

Since scripts are typically longer than a single line and creating them in a separate file in the IDE is recommended, using a helper script that formats a .groovy file into a JSON file and submits it to the repository manager can be a convenient approach.

The complex-script project in the scripting section of example project includes an example implementation using Groovy invoked from a shell script. All scripts in this folder can be published and executed via the provision.sh file. This results in the download of all required dependencies and the upload and execution of the referenced script. Alternatively, you can provision the scripts individually:

groovy addUpdateScript.groovy -u "admin" -p "admin123" -n "raw" -f "rawRepositories.groovy" -h "http://localhost:8081"
curl -v -X POST -u admin:admin123 --header "Content-Type: text/plain" "http://localhost:8081/service/rest/v1/script/raw/run"

The following scripts are available:

npmAndBowerRepositories.groovy

for NPM and Bower repositories suitable for server-side and client JavaScript-based development

rawRepositories.groovy

creates a new blob store and uses it for a hosted raw repository

security.groovy

disables anonymous access, creates a new administrator account, creates a new role with a simple expansion to anonymous user role and a user, creates a new role with publishing access to all repositories and a user

core.groovy

configures the base URL capability and a proxy server

Logging from your scripts into the repository manager logs is automatically available and performed with the usual calls:

log.info('User jane.doe created')

The result of the last script line is by default returned as a string. This can be a message as simple as Success! or more complex structured data.

For instance, you can easily return JSON using built-in Groovy classes like:

return groovy.json.JsonOutput.toJson([result: 'Success!'])

which looks like:

{
    "result": "Success!"
}

Passing parameters to the script can use JSON encoded arguments like:

{
  "id": "foo",
  "name": "bar",
  "description": "baz",
  "privilegeIds": ["nx-all"],
  "roleIds": ["nx-admin"]
}

which in turn can be parsed using the JsonSlurper class in the script:

import groovy.json.JsonSlurper
 
//expects json string with appropriate content to be passed in
def role = new JsonSlurper().parseText(args)
 
security.addRole(role.id, role.name, role.description, role.privilegeIds, role.roleIds)

You can read more about how to work with XML and JSON with Groovy on http://groovy-lang.org/processing-xml.html and http://groovy-lang.org/json.html.