How to implement formulas

In this section, you will find how to implement formulas on Ritchie.

How to implement?

Once you have created and tested your formula, Ritchie will generate a local workspace. This is where you can start implementing the new automation by updating the Hello World template.

Editable files

To do this configuration, you will have to update 3 files located inside the /src package of the new formula’s root folder.

1. config.json

The config.json file contains the formula's input parameters. It allows the CLI to know what datas to ask the user when he runs the command in the terminal in order to process the formula correctly.

These input parameters are made up of the following fields:

  • a docker image builder

  • the formula inputs parameters.

Each input is composed of the following fields:

  • name: variable name to extract.

  • type: text (string), bool (boolean), password (hidden string) or credentials.

  • label : text appearing on the CLI asking for the input.

  • default (optional): default input value (if null).

  • required (optional): boolean that indicates if the input value is required or optional.

  • tutorial (optional): input helper [? for help]

  • items (optional): list of input variable options.

  • cache (optional):

    • active: if cache is enabled or not.

    • qty: amount of values to store.

    • newLabel: text appearing on the CLI asking for a new input.

  • condition (optional): Only shows this input if the given condition succeeds

    • variable: The variable name used on a previous input for comparison.

    • operator: A logical operator to compare. Supports ==, !=, <, >, <=, and >=.

    • value: The desired value to compare to.

  • pattern (optional):

    • regex: The regex pattern to validate the input

    • mismatchText: error message when input doesn't match the regex pattern

Conditional input example with Regex pattern:

"inputs": [
{
"label": "Select a system:",
"name": "system",
"type": "text",
"required": true,
"items": [
"LINUX",
"MACOS",
"WINDOWS"
]
},
{
"condition": {
"variable":"system",
"operator":"==",
"value":"LINUX"
},
"label": "Select a LINUX OS:",
"name": "linux-os",
"required": false,
"type": "text",
"items": [
"UBUNTU",
"FEDORA",
"CENTOS"
],
"pattern": {
"regex": "UBUNTU|FEDORA|CENTOS",
"mismatchText": "Invalid option"
}
}
]

Once an input is informed on Ritchie CLI, it is saved as a local variable during the formula execution.

2. main file

  • This file is used to extract the inputs asked in the config.json file. Inputs are extracted by the field name informed on the config.json file.

  • This is also where the methods are called to perform the task / automation of the formula, manipulating the input parameters.

3. formula/* folder

This folder contains the files with the formula's behavior. Methods called by the main.file are located inside the files inside this folder.

Depending on the chosen language, it may be necessary to update more files, for example to manipulate dependencies.

Examples

Golang
Java
Node
Python
Shell
Golang

According to the image above, you can see the editable files:

  • config.json: Update the inputs configurations.

  • main.go: Extract the inputs and call the formula's methods (coded on formula/*).

  • formula/*: Code the formula's behavior.

  • help.json: Update the command or subcommand description message.

Java

According to the image above, you can see the editable files:

  • config.json: Update the inputs configurations.

  • main.java: Extract the inputs and call the formula's methods (coded on formula/*).

  • formula/*: Code the formula's behavior.

  • help.json: Update the command or subcommand description message.

Node

According to the image above, you can see the editable files:

  • config.json: Update the inputs configurations.

  • index.js: Extract the inputs and call the formula's methods (coded on formula/*).

  • formula/*: Code the formula's behavior.

  • help.json: Update the command or subcommand description message.

Python

According to the image above, you can see the editable files:

  • config.json: Update the inputs configurations.

  • main.py: Extract the inputs and call the formula's methods (coded on formula/*).

  • formula/*: Code the formula's behavior.

  • help.json: Update the command or subcommand description message.

Shell

According to the image above, you can see the editable files:

  • config.json: Update the inputs configurations.

  • main.sh: Extract the inputs and call the formula's methods (coded on formula/*).

  • formula/*: Code the formula's behavior.

  • help.json: Update the command or subcommand description message.

The structures defines the command, so it's not indicated to update folders names or include more files/folders if not in the src/* folder.

Next steps

On this section, you saw how to implement a formula on Ritchie. To keep configuring the formula:

👉 Go to build a formula to see how to continue the tests on the formulas you create.

👉 Go to publish a formula to see how to transfer your work to a public repository.