Repository navigation
Command-Level Templating #248
|
I think we would benefit significantly from supporting templating at the command level. Right now, there’s no clean way to pass only parts of the arguments to individual submodules, or to reuse the same arguments across multiple modules. The current model forces all arguments to be handed off verbatim to a single module, which limits flexibility and reduces the potential use-cases of each module. My proposal is to introduce an args list in the command configuration. This list would define the command’s arguments and make them available to any submodule. For example, imagine we want to copy a non-static file to the dutagent and then execute it there. Using the existing copy module (purely as a placeholder) we could pass arguments in a templated way: $X would refer to the X-th argument of the list and could be used multiple times across modules. The args list would also double as metadata for user-facing help: argument descriptions, prompts, etc. For backward compatibility, we keep the current main option as-is. So, a command may:
|
Replies: 3 comments 9 replies
|
Yeah, maybe. But feels a bit redundant to yaml anchoring. |
|
When defining a command that wraps a module (e.g. ssh), you sometimes want to forward all or part of the caller's arguments down to the module rather than binding them explicitly. Or allow the remaining arguments to be passed to another module. Approach 1Arguments are forwarded by referencing special implicit variables inside the args list using the existing ${...} template syntax:
exec:
desc: "SSH exec"
cmds:
ssh:
desc: "Holla"
uses:
- module: ssh
args: ["${args}"]
with:
host: enigma
user: oscar
privatekey: ...mixed:
desc: "Flash firmware and exec ssh"
args:
- name: duration
desc: "Duration to press GPIO Pin 1"
- name: firmware-path
desc: "Path to firmware binary"
modules:
- module: file
args: ["${firmware-path}", "/tmp/fw.bin"]
- module: gpio-button
args: ["${duration}"]
with:
pin: 1
activelow: true
- module: flash
args: ["/tmp/fw.bin"]
- module: time-wait
args: ["500"]
- module: ssh
args: ["${remaining}"]
with:
host: enigma
user: oscar
privatekey: ...Approach 2Instead of using template variables, argument forwarding is expressed as dedicated keys on the module:
exec:
desc: "SSH exec"
cmds:
ssh:
desc: "Holla"
uses:
- module: ssh
forward_args: true
with:
host: enigma
user: oscar
privatekey: ....mixed:
desc: "Flash firmware and exec ssh"
args:
- name: duration
desc: "Duration to press GPIO Pin 1"
- name: firmware-path
desc: "Path to firmware binary"
modules:
- module: file
args: ["${firmware-path}", "/tmp/fw.bin"]
- module: gpio-button
args: ["${duration}"]
with:
pin: 1
activelow: true
- module: flash
args: ["/tmp/fw.bin"]
- module: time-wait
args: ["500"]
- module: ssh
rest_args: true
with:
host: enigma
user: oscar
privatekey: ...Reviewing this now myself i would prefer approach 2. It has the benefit of being more explicit and not using any magic templating values (or reserved words for that matter). What do you think? |
I've reduced the complexity a bit more with a new approach. Instead of having a difference between remaining_args and forward_args. forward_args handles both cases. It essentially forwards all the args to a module which are not mapped by the templating to a positional argument:
dutctl run my-device run-command ls -la /tmp # shell receives: ["ls", "-la", "/tmp"]When combined with command-level templating, the first
Nruntime args (one per declaredargsentry) are consumed for template substitution. The remaining args are forwarded: