Roles

Roles provide a way to organize and reuse variables across user-defined commands.spring-doc.cn

By default, the Spring CLI includes an unnamed role that is always available. If no specific role is specified, commands use the default role.spring-doc.cn

To further customize and differentiate roles, you can associate them with specific names. These named roles are stored as YAML files within the .spring/roles directory, located alongside the .spring/commands directory.spring-doc.cn

Roles let you define variables that can be accessed in an action file by using Handlebars, letting you share data between commands.spring-doc.cn

Furthermore, you can use roles to supply values for command line options. If a command line option does not have a specified value and a role variable with the same name as the command option exists, the command automatically uses the value of the role variable for that specific option.spring-doc.cn

File Structure

For each role, a corresponding file is created in the .spring/roles/vars directory. For example, if there is a qa and prod role, the directory would look like the following:spring-doc.cn

$ tree .spring/roles/vars -lr
.spring/roles/vars
├── vars.yml
├── vars-qa.yml
└── vars-prod.yml

The vars.yml file is used for the default role.spring-doc.cn

This structure follows a similar pattern to using profile-specific Spring application configuration files. However, role variables do not exhibit the same behavior as Spring profiles, such as retrieving values from other locations in addition to the file (such as environment variables).spring-doc.cn

Quick Start

In this quick start, we demonstrate how to add variables to the default role and use their values when generating a file.spring-doc.cn

Let’s begin by setting the value of the role variable 'greeting' to 'Mondo':spring-doc.cn

$ spring role set --key greeting --value Mondo
Key-value pair added to the default role

The key-value pair is stored in the ./spring/roles/vars/vars.yml file under the root project directory.spring-doc.cn

To retrieve the value of the variable, use the following command:spring-doc.cn

$ spring role get --key greeting
Mondo

Now we create another role variable:spring-doc.cn

$ spring role set --key language --value Italian
Key-value pair added to the default role

Now we can incorporate these variables into a user-defined command. We create a user-defined command named hello say:spring-doc.cn

$ spring command new --command-name hello --sub-command-name say
Created user defined command /home/mark/testing-spring-cli/roles/myapp/.spring/commands/hello/say

Inside the .spring/commands/hello/say directory, you can find a file named command.yaml that has the following contents:spring-doc.cn

command:
  description: Generate a new file with a hello message
  options:
    #
    - name: greeting
      description: who or what to say hello to
      dataType: string
      defaultValue: World
      inputType: text

Note that the command line option name is greeting, which matches the name of the role variable we created.spring-doc.cn

Within the .spring/commands/hello/say directory, there is an action file named hello.yaml with the following contents:spring-doc.cn

actions:
  - generate:
      to: hello.txt
      text: Hello {{greeting}} on {{os-name}}.

Now we update the file to include:spring-doc.cn

actions:
  - generate:
      to: hello.txt
      text: Hello {{greeting}} on {{os-name}}. {{#if language}} {{language}} {{/if}}

When running the command without passing the greeting command-line option, the value of greeting is obtained from the role variable with the same name instead of using the default value of World.spring-doc.cn

Additionally, since we have defined the role variable language, we can test its existence and include its value in the output. Note that language is not a command line option. The following command (show with its output) does so:spring-doc.cn

$ spring hello say
Using Role variable instead of default command line option for key = greeting , value = Mondo from the default role
Generated /home/mark/testing-spring-cli/roles/myapp/hello.txt

The generated file contains:spring-doc.cn

Hello Mondo on Linux.  Italian

The value of {{greeting}} comes from the role variable because it was not provided as a command-line option.spring-doc.cn

The {{language}} variable was not a command line option, but it is available to use with Handlebars expressions.spring-doc.cn

Now we can remove the generated file. in the interactive shell, we run . ! rm hello.txt and pass in the greeting command line option:spring-doc.cn

$ spring hello say --greeting amico

The generated file contains:spring-doc.cn

Hello amico on Linux.  Italian

Setting Variables

To set a value for a role variable, use the spring role set command:spring-doc.cn

spring role set --key greeting --value Mondo

You can optionally specify the role by using the --role option.spring-doc.cn

Getting Variables

To retrieve the value of a role variable, use the following command:spring-doc.cn

spring role get --key greeting

You can optionally specify the role by using the --role option.spring-doc.cn

The role variable greeting can then be accessed inside action files that use Handlebars templating. See the quick start section for an example.spring-doc.cn

The role variable is also used to match against user-defined command option names. If a value is not explicitly provided as a command-line option, the value of the role variable is used.spring-doc.cn

You can also use the special command, . !, to view the full contents of the file that contains role variables when you are in the interactive shell:spring-doc.cn

spring:>. ! cat .spring/roles/vars/vars.yml
greeting: mondo

Adding a Role

To add a role, use the following command:spring-doc.cn

spring role add qa

This command creates a role named qa.spring-doc.cn

A file named ./spring/roles/vars/vars-qa.yml is created under the root project directory.spring-doc.cn

Listing Roles

To list the available roles, use the following command:spring-doc.cn

spring role list

This command displays the list of roles:spring-doc.cn

┌────┐
│Name│
├────┤
│qa  │
└────┘

Removing a Role

To remove a role, use the following command:spring-doc.cn

spring role remove qa

This command removes the role named qa.spring-doc.cn