Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add variables as arguments #29

Closed
l1n3n01z opened this issue Jul 11, 2024 · 3 comments
Closed

Feature: Add variables as arguments #29

l1n3n01z opened this issue Jul 11, 2024 · 3 comments

Comments

@l1n3n01z
Copy link

Passing variables as environment variables is fine for configuration variables, but having used ain a bit, I feel that passing variables as arguments would be much more natural when the variable in question varies often. This applies both when scripting and on the command line.

Using a double hyphen to denote that the following arguments are variables might be a good way to do this. It would allow easy editing of parameters when calling the same endpoint a few times.

$ ain -e config.env base.ain specific_query.ain -- --id=2
push up arrow and change the id
$ ain -e config.env base.ain specific_query.ain -- --id=3

When scripting

NB: I'm using camelCase for my variables that I know change often

Before

#!/usr/bin/env bash
packageSearch=$1
#search for the package to find the id
packageId=$(searchTerm=$packageSearch ain package_service_template.ain search_package.ain | jq '.[] | .id')
#view simplified event history 
id=$packageId ain keycafe_template.ain package_events.ain | jq '.[] | {eventName, user, dateCreated, location}'

After

#!/usr/bin/env bash
packageSearch=$1
#search for the package to find the id
packageId=$(ain package_service_template.ain search_package.ain -- --searchTerm=$packageSearch | jq '.[] | .id')
#view simplified event history 
ain keycafe_template.ain package_events.ain -- --id=$packageId | jq '.[] | {eventName, user, dateCreated, location}'
@jonaslu
Copy link
Owner

jonaslu commented Jul 12, 2024

Hi, is it merely because of convenience?

The reason I'm asking is because scripting out having arguments last after some special syntax such as '--' via a wrapper-script is very doable and doesn't introduce another syntax for something that is already solved via widely used methods.

@jonaslu
Copy link
Owner

jonaslu commented Aug 11, 2024

Closing due to inactivity.

For reference, here's a bash-script that inverts the way variables are passed:

#!/bin/bash

delim_found=false
for arg in "$@"; do
    if $delim_found; then
        env_vars+=("$arg")
    elif [ "$arg" == "--" ]; then
        delim_found=true
    else
        command_args+=("$arg")
    fi
done

# Execute the command with environment variables first
env "${env_vars[@]}" ain "${command_args[@]}"

Usage with script saved as ain.sh. A single -- delimiter between command line arguments and variables:

ain.sh template1.ain template2.ain -- VAR1=1 VAR2=2 

@jonaslu jonaslu closed this as completed Aug 11, 2024
jonaslu added a commit that referenced this issue Oct 11, 2024
As suggested in #29 have a
special parameter after
the template-names to make
ain more usable.

Instead of inputting vars
before the invocation, --vars
can be added afterwards
and will set env-vars if not
already defined, after the
VAR=1 bash, but before any
env-var file.

NB
If template-names are given
over a pipe and adding
--vars, ain will fail because
of --vars not being recognized
by flag.

Test plan:
* Verify ain -v still shows version.
* Have a template with a ${VAR} in it.
  Have an .env file defining that ${VAR}.
  Verify .env file is still picked up.
* Verify ain -b still works.
* Have a template with a [Body].
  Run ain with -l and verify
  the body-file is left when done.
* Send a filename over a pipe
  (e g echo temp2.ain | ain temp.ain)
  and verify passing files over templates
  still works.
* Have a template with two variables
  ${VAR1} and ${VAR2}. Set
  both before ain is invoked
  and pass other values with
  --vars
  (e g VAR1=1 VAR2=2 ain -p temp.ain --vars VAR1=aah VAR2=yes).
  Verify envvars set via bash
  takes precedence over --vars.
* Edit the command line above and
  and remove one of the two
  bash-set variables. Verify
  --vars is only applied if
  the environment variable is not
  set.
* Pass a malformed envvar missing
  and = sign, verify error message
  on malformed env-var value.
* Pass --vars but do not
  pass any values. Verify
  error message on missing
  envvar values.
@jonaslu
Copy link
Owner

jonaslu commented Nov 4, 2024

Hi @l1n3n01z ! I thought about this some more and decided to add --vars VAR=value as documented here thanks to your issue!

Not only does it make arrow up easier, but on windows cmd.exe setting environment-variables is involved, and --vars will help there.

It's on master right now but hasn't been released yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants