In this tutorial, the idea is to create a formula to execute another formula inside it.
See more information on how to run formula inside other on groups formulas section.
Here, we will group at least two formulas:
rit math sum numbers
(level 1).rit math multiply numbers
(TODO).rit math calculate
This formula must have (at least) three input parameters. See below:
RIT_NUMBER_ONE
).RIT_NUMBER_TWO
).RIT_OPERATION
).Search for the config.json
file of your formula and replace it the inputs
field for the block below:
{
"label": "Number one : ",
"name": "rit_number_one",
"type": "text"
},
{
"label": "Number two : ",
"name": "rit_number_two",
"type": "text"
},
{
"label": "Operation : ",
"name": "rit_operation",
"type": "text",
"items": [
"sum",
"multiplication"
]
}
]
Follow the steps below to create your formula:
Search for the main
file of tour formula and extract all the inputs before using them to call your formula’s method. See some code example below:
import os
from formula import formula
number_one = int(os.environ.get("RIT_NUMBER_ONE"))
number_two = int(os.environ.get("RIT_NUMBER_TWO"))
operation = os.environ.get("RIT_OPERATION")
formula.run(number_one, number_two, operation)
package main
import (
"formula/pkg/formula"
"os"
)
func main() {
numberOne := os.Getenv("RIT_NUMBER_ONE")
numberTwo := os.Getenv("RIT_NUMBER_TWO")
operation := os.Getenv("RIT_OPERATION")
formula.Formula{
NumberOne: numberOne,
NumberTwo: numberTwo,
Operation: operation,
}.Run()
}
#!/bin/bash
# shellcheck source=/dev/null
. "$(dirname "$0")"/formula/formula.sh --source-only
runFormula
main.*
nomenclature for most languages, except Node that it calls index.js
.Run the rit formula according to the selected operation:
rit math multiply numbers
formula;rit math sum numbers
formula.Search for the formula
file of your formula and implement the method performing the operation of your automation. See some code examples:
import os
import json
def run(number_one, number_two, operation):
if operation == "sum":
input_flag_cmd = f"rit python math sum numbers --rit_number_one={number_one} --rit_number_two={number_two}"
elif operation == "multiplication":
input_flag_cmd = f"rit python math multiply numbers --rit_number_one={number_one} --rit_number_two={number_two}"
else:
print("Unexpected operation")
os.system(f'{input_flag_cmd}')
package formula
import (
"fmt"
"log"
"os/exec"
)
type Formula struct {
NumberOne string
NumberTwo string
Operation string
}
func (h Formula) Run() {
cmdLine := exec.Command("", "")
switch h.Operation {
case "sum":
cmdLine = exec.Command(
"rit",
"math",
"sum",
"numbers",
fmt.Sprintf("--rit_number_one=%s", h.NumberOne),
fmt.Sprintf("--rit_number_two=%s", h.NumberTwo),
)
case "multiplication":
cmdLine = exec.Command(
"rit",
"math",
"multiply",
"numbers",
fmt.Sprintf("--rit_number_one=%s", h.NumberOne),
fmt.Sprintf("--rit_number_two=%s", h.NumberTwo),
)
default:
fmt.Print("Unexpected operation type:", h.Operation)
}
out, err := cmdLine.CombinedOutput()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
fmt.Printf(string(out))
}
#!/bin/sh
runFormula() {
if [ "sum" = "$RIT_OPERATION" ]; then
rit math sum numbers --rit_number_one=$RIT_NUMBER_ONE --rit_number_two=$RIT_NUMBER_TWO
elif [ "multiplication" = "$RIT_OPERATION" ]; then
rit math multiply numbers --rit_number_one=$RIT_NUMBER_ONE --rit_number_two=$RIT_NUMBER_TWO
else
echo "Unexpected operation type: $RIT_OPERATION"
fi
}
pom.xml
for Java, requirements.txt
for Python, package.json
for Node, go.mod
for Golang, etc).~ rit math calculate
? Number one : 1
? Number two : 2
? Operation: multiplication
The multiplication is 2
~ ~ rit math calculate
? Number one : 1
? Number two : 2
? Operation: sum
The sum is 3
If you want to test your formula more, see some suggestions below:
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.