How can I define Run actions that let me pass thro...
# 🤖|ask-kapa
f
@microscopic-river-74022 How can I define Run actions that let me pass through additional flags to commands.
m
Please stand by @fresh-yak-35965 as I search the knowledge base. I normally take about 20 seconds or so. :wave:
---------------------- In Garden, you can define Run actions that allow you to pass through additional flags to commands. This can be done by using the
args
and
opts
fields to explicitly define positional arguments and options that are expected or required for your command. These are validated and parsed before running the command. Here's an example of how you can define a Run action:
Copy code
yaml
kind: Command
name: wrapped
description:
  short: Execute a Run action with arguments and option flags
args:
  - name: action-name
    description: The name of the Run action
    required: true
opts:
  - name: db
    description: Override the database hostname
    type: string
gardenCommand:
  - run
  - ${args.action-name}
  - --var
  - dbHostname=${opts.db || "db"}
In this example, a wrapper command is made for executing
Run
actions in your project. One positional argument is required for the name of the action to run. An option is also defined for overriding a project variable. To further accept any additional arguments and append to the Garden command, you can add the following:
Copy code
yaml
...
gardenCommand:
  - run
  - ${args.action-name}
  - --var
  - dbHostname=${opts.db || "db"}
  - $concat: ${args.$rest}  # <- pass any additional parameters through to the command without validation
Now you could, for example, run
garden wrapped my-action --db test --force
and the additional
--force
parameter gets passed to the underlying Garden command. For more details, you can refer to the Garden documentation on defining arguments and option flags (). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
message has been deleted
f
@microscopic-river-74022 I want to be able to run a command
revision -m "create table foobar"
which calls the underlying
alembic-revision
Run action. How can I accomplish this while also passing the additional option through to the underlying command called by
alembic-revision
?
m
Let me check the knowledge base for you. One moment! :books:
---------------------- To accomplish this, you can define a custom command in Garden that wraps the
alembic-revision
Run action and passes through additional options to the underlying command. You can use the
args
and
opts
fields to define positional arguments and options for your command. Here's an example of how you can define the custom command:
Copy code
yaml
kind: Command
name: revision
description:
  short: Execute alembic-revision with arguments and option flags
args:
  - name: message
    description: The message for the alembic revision
    required: true
gardenCommand:
  - run
  - alembic-revision
  - --message
  - ${{args.message}}
  - $concat: ${{args.$rest}}  # <- pass any additional parameters through to the command without validation
In this example, a wrapper command named
revision
is created for executing the
alembic-revision
Run action. One positional argument named
message
is required for the revision message. The
$concat: ${{args.$rest}}
line allows any additional parameters to be passed through to the
alembic-revision
command without validation. Now you can run your command as
garden revision -m "create table foobar"
and the
-m "create table foobar"
parameter gets passed to the underlying
alembic-revision
command. Please note that this is a hypothetical example and you may need to adjust it based on the actual requirements of the
alembic-revision
Run action. For more details, you can refer to the Garden documentation on defining arguments and option flags (). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
message has been deleted
Let me check the knowledge base for you. One moment! :books:
f
@microscopic-river-74022 The following command gives me an error:
Copy code
kind: Command
name: alembic
description:
  short: Execute alembic-revision with arguments and option flags
args:
  - name: action-name
    description: The name of the alembic command
    required: true
opts:
  - name: message
    description: Optional message (for revisions)
    type: string
gardenCommand:
  - run
  - alembic-${args.action-name}
  - --var
  - message=${args.message || ""}
The error is as follows:
Copy code
Encountered an unexpected Garden error. This is likely a bug 🍂

You can help by reporting this on GitHub: https://github.com/garden-io/garden/issues/new?labels=bug,crash&template=CRASH.md&title=Crash%3A%20Missing%20cli%20argument%20in%20custom%20command%20wrapper.

Please attach the following information to the bug report after making sure that the error message does not contain sensitive information:

Error: Missing cli argument in custom command wrapper.
    at CustomCommandWrapper.action (file:///Users/jackhall/Library/Application%20Support/io.garden.garden/1715180386-vPlqQaa.r/rollup/garden.mjs:425756:23)
    at file:///Users/jackhall/Library/Application%20Support/io.garden.garden/1715180386-vPlqQaa.r/rollup/garden.mjs:358999:41
    at async file:///Users/jackhall/Library/Application%20Support/io.garden.garden/1715180386-vPlqQaa.r/rollup/garden.mjs:358787:20
m
---------------------- The error message suggests that there's a missing CLI argument in your custom command wrapper. In your
gardenCommand
field, you're trying to use
args.action-name
and
args.message
as variables. However, the error might be due to the fact that these variables are not being correctly interpolated. In Garden, when you want to use variables in the
gardenCommand
field, you should wrap them in
${}
. This tells Garden to replace the
${}
with the value of the variable. Here's how you can modify your command:
Copy code
yaml
kind: Command
name: alembic
description:
  short: Execute alembic-revision with arguments and option flags
args:
  - name: action-name
    description: The name of the alembic command
    required: true
opts:
  - name: message
    description: Optional message (for revisions)
    type: string
gardenCommand:
  - run
  - alembic-${{args.action-name}}
  - --var
  - message=${{opts.message || ""}}
In this corrected version,
${{args.action-name}}
and
${{opts.message || ""}}
are used in the
gardenCommand
field, which should correctly interpolate the variables. For more details, you can refer to the Garden documentation on defining arguments and option flags (). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
message has been deleted
3 Views