Skip to content

Commit 9b8c1f3

Browse files
authored
Merge pull request #705 from bashly-framework/add/argfile-env-var
Allow disabling and overriding argfile with env var
2 parents 3363442 + e985b90 commit 9b8c1f3

File tree

12 files changed

+117
-13
lines changed

12 files changed

+117
-13
lines changed

lib/bashly/libraries/settings/settings.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ show_examples_on_error: false
120120
# all the private elements in the usage texts, as if they were public.
121121
private_reveal_key: ~
122122

123+
# When enabling argfile for any command, this setting controls the name of the
124+
# environment variable that can override or disable argfile loading at runtime.
125+
# Set the variable to `0`, `off`, `no` or `false` to disable argfile loading,
126+
# or to a different path to load another argfile instead.
127+
argfile_var: ARGFILE
128+
123129
# Display various usage elements in color by providing the name of the color
124130
# function. The value for each property is a name of a function that is
125131
# available in your script, for example: `green` or `bold`.

lib/bashly/settings.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ class << self
44
include AssetHelper
55

66
attr_writer(
7+
:argfile_var,
78
:commands_dir,
89
:compact_short_flags,
910
:conjoined_flag_args,
@@ -37,6 +38,10 @@ def all_lib_dirs
3738
@all_lib_dirs = [full_lib_dir] + extra_lib_dirs
3839
end
3940

41+
def argfile_var
42+
@argfile_var ||= get :argfile_var
43+
end
44+
4045
def commands_dir
4146
@commands_dir ||= get :commands_dir
4247
end
Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
= view_marker
22

3-
> local argfile_line argfile_key argfile_value escaped
4-
> [[ -f "{{ argfile }}" ]] || return
3+
> local argfile argfile_line argfile_key argfile_value env_argfile env_argfile_var
4+
> argfile="{{ argfile }}"
5+
> env_argfile_var="{{ Settings.argfile_var }}"
6+
> env_argfile="${!env_argfile_var:-}"
57
>
6-
> while IFS= read -r argfile_line || [[ -n "$argfile_line" ]]; do
7-
> [[ "$argfile_line" =~ ^[[:space:]]*(-{1,2}[^[:space:]]+)([[:space:]]+(.+))?[[:space:]]*$ ]] || continue
8-
> argfile_key="${BASH_REMATCH[1]}"
9-
> argfile_value="${BASH_REMATCH[3]:-}"
10-
> argfile_value="${argfile_value#"${argfile_value%%[![:space:]]*}"}"
11-
> argfile_value="${argfile_value%"${argfile_value##*[![:space:]]}"}"
12-
> [[ "$argfile_value" =~ ^\"(.*)\"$ || "$argfile_value" =~ ^\'(.*)\'$ ]] && argfile_value="${BASH_REMATCH[1]}"
8+
> case "${env_argfile,,}" in
9+
> 0 | off | no | false)
10+
> argfile=''
11+
> ;;
12+
> esac
1313
>
14-
> case "$argfile_key" in
15-
= flags.map { |flag| flag.render(:argfile_case) }.join.indent 4
16-
> esac
17-
> done <"{{ argfile }}"
14+
> [[ -n "$env_argfile" ]] && argfile="$env_argfile"
15+
> if [[ -f "$argfile" ]]; then
16+
> while IFS= read -r argfile_line || [[ -n "$argfile_line" ]]; do
17+
> [[ "$argfile_line" =~ ^[[:space:]]*(-{1,2}[^[:space:]]+)([[:space:]]+(.+))?[[:space:]]*$ ]] || continue
18+
> argfile_key="${BASH_REMATCH[1]}"
19+
> argfile_value="${BASH_REMATCH[3]:-}"
20+
> argfile_value="${argfile_value#"${argfile_value%%[![:space:]]*}"}"
21+
> argfile_value="${argfile_value%"${argfile_value##*[![:space:]]}"}"
22+
> [[ "$argfile_value" =~ ^\"(.*)\"$ || "$argfile_value" =~ ^\'(.*)\'$ ]] && argfile_value="${BASH_REMATCH[1]}"
23+
>
24+
> case "$argfile_key" in
25+
= flags.map { |flag| flag.render(:argfile_case) }.join.indent 6
26+
> esac
27+
> done <"$argfile"
28+
> fi
1829
>

schemas/settings.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,13 @@
289289
}
290290
]
291291
},
292+
"argfile_var": {
293+
"title": "argfile var",
294+
"description": "The name of the environment variable that can override or disable command argfiles at runtime\nhttps://bashly.dev/usage/settings/#argfile_var",
295+
"type": "string",
296+
"minLength": 1,
297+
"default": "ARGFILE"
298+
},
292299
"watch_evented": {
293300
"title": "watch evented",
294301
"description": "Whether to use evented file system watch instead of the default polling\nhttps://bashly.dev/usage/settings/#watch_evented",
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
creating user files in src
2+
created src/root_command.sh
3+
created ./download
4+
run ./download --help to test your bash script
5+
+ ./download somesource
6+
# This file is located at 'src/root_command.sh'.
7+
# It contains the implementation for the 'download' command.
8+
# The code you write here will be wrapped by a function named 'root_command()'.
9+
# Feel free to edit this file; your changes will persist when regenerating.
10+
args:
11+
- ${args[--force]} = 1
12+
- ${args[--log]} = from-default.log
13+
- ${args[source]} = somesource
14+
+ DOWNLOAD_ARGFILE=off
15+
+ ./download somesource
16+
# This file is located at 'src/root_command.sh'.
17+
# It contains the implementation for the 'download' command.
18+
# The code you write here will be wrapped by a function named 'root_command()'.
19+
# Feel free to edit this file; your changes will persist when regenerating.
20+
args:
21+
- ${args[source]} = somesource
22+
+ DOWNLOAD_ARGFILE=.alt-download
23+
+ ./download somesource
24+
# This file is located at 'src/root_command.sh'.
25+
# It contains the implementation for the 'download' command.
26+
# The code you write here will be wrapped by a function named 'root_command()'.
27+
# Feel free to edit this file; your changes will persist when regenerating.
28+
args:
29+
- ${args[--log]} = from-override.log
30+
- ${args[source]} = somesource
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--log from-override.log
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--force
2+
--log from-default.log
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
download
2+
src/*.sh
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
argfile_var: DOWNLOAD_ARGFILE
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: download
2+
help: Test argfile env var behavior
3+
version: 0.1.0
4+
5+
argfile: .download
6+
7+
args:
8+
- name: source
9+
required: true
10+
help: URL to download from
11+
12+
flags:
13+
- long: --force
14+
short: -f
15+
help: Overwrite existing files
16+
- long: --log
17+
short: -l
18+
arg: path
19+
help: Path to log file

0 commit comments

Comments
 (0)