Skip to content

Commit 1da477d

Browse files
authored
Merge pull request #356 from DannyBen/change/config-dependencies
Update dependencies option to support 'any' (e.g. wget or curl)
2 parents b5c3ec6 + 817d1dd commit 1da477d

30 files changed

+455
-43
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
/spec/tmp
1010
/tmp
1111
/toc.txt
12-
/helpers/debug.runfile
12+
/support/runfile/debug.runfile

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Each of these examples demonstrates one aspect or feature of bashly.
1515
- [command-aliases](command-aliases#readme) - allowing a command to be called with multiple names
1616
- [command-examples](command-examples#readme) - configuring command examples
1717
- [dependencies](dependencies#readme) - halting script execution unless certain dependencies are installed
18+
- [dependencies-alt](dependencies-alt#readme) - halting script execution unless one of the required dependencies is installed
1819
- [environment-variables](environment-variables#readme) - halting script execution unless certain environment variables are set
1920
- [default-values](default-values#readme) - arguments and flags with default values
2021
- [minus-v](minus-v#readme) - using `-v` and `-h` in your script

examples/dependencies-alt/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cli

examples/dependencies-alt/README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Alternate Dependencies Example
2+
3+
Demonstrates how to require your script's user to have at least one of a list
4+
of dependencies (commands) installed prior to using your script.
5+
6+
This example was generated with:
7+
8+
```bash
9+
$ bashly init
10+
$ bashly add colors
11+
# ... now edit src/bashly.yml to match the example ...
12+
$ bashly generate
13+
```
14+
15+
-----
16+
17+
## `bashly.yml`
18+
19+
```yaml
20+
name: cli
21+
help: Sample application that requires alternate dependencies
22+
version: 0.1.0
23+
24+
commands:
25+
- name: download
26+
help: Download something
27+
28+
# This is the explicit way of defining dependencies.
29+
# The generated script will be halted (with a friendly error message) if
30+
# any of these programs are not installed:
31+
dependencies:
32+
# Abort with just the default 'missing dependency: git' message if git
33+
# is not found
34+
git:
35+
36+
# Abort with an additional help message if ruby is not found
37+
ruby: visit $(blue_underlined https://www.ruby-lang.org) to install
38+
39+
# Abort if both curl and wget are not found, and show an additional help
40+
# message.
41+
# Note that the path to the first found dependency will be available to
42+
# your script in the `deps` associative array.
43+
http_client:
44+
command: [curl, wget]
45+
help: install with $(green sudo apt install curl)
46+
```
47+
48+
49+
50+
## Generated script output
51+
52+
### `$ ./cli download`
53+
54+
```shell
55+
# this file is located in 'src/download_command.sh'
56+
# code for 'cli download' goes here
57+
# you can edit it freely and regenerate (it will not be overwritten)
58+
args: none
59+
60+
deps:
61+
- ${deps[git]} = /usr/bin/git
62+
- ${deps[http_client]} = /usr/bin/curl
63+
- ${deps[ruby]} = /home/vagrant/.rbenv/versions/3.1.3/bin/ruby
64+
65+
66+
```
67+
68+
69+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: cli
2+
help: Sample application that requires alternate dependencies
3+
version: 0.1.0
4+
5+
commands:
6+
- name: download
7+
help: Download something
8+
9+
# This is the explicit way of defining dependencies.
10+
# The generated script will be halted (with a friendly error message) if
11+
# any of these programs are not installed:
12+
dependencies:
13+
# Abort with just the default 'missing dependency: git' message if git
14+
# is not found
15+
git:
16+
17+
# Abort with an additional help message if ruby is not found
18+
ruby: visit $(blue_underlined https://www.ruby-lang.org) to install
19+
20+
# Abort if both curl and wget are not found, and show an additional help
21+
# message.
22+
# Note that the path to the first found dependency will be available to
23+
# your script in the `deps` associative array.
24+
http_client:
25+
command: [curl, wget]
26+
help: install with $(green sudo apt install curl)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
echo "# this file is located in 'src/download_command.sh'"
2+
echo "# code for 'cli download' goes here"
3+
echo "# you can edit it freely and regenerate (it will not be overwritten)"
4+
inspect_args
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## Code here runs inside the initialize() function
2+
## Use it for anything that you need to run before any other function, like
3+
## setting environment variables:
4+
## CONFIG_FILE=settings.ini
5+
##
6+
## Feel free to empty (but not delete) this file.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
## Color functions [@bashly-upgrade colors]
2+
## This file is a part of Bashly standard library
3+
##
4+
## Usage:
5+
## Use any of the functions below to color or format a portion of a string.
6+
##
7+
## echo "before $(red this is red) after"
8+
## echo "before $(green_bold this is green_bold) after"
9+
##
10+
## Color output will be disabled if `NO_COLOR` environment variable is set
11+
## in compliance with https://no-color.org/
12+
##
13+
print_in_color() {
14+
local color="$1"
15+
shift
16+
if [[ -z ${NO_COLOR+x} ]]; then
17+
printf "$color%b\e[0m\n" "$*"
18+
else
19+
printf "%b\n" "$*"
20+
fi
21+
}
22+
23+
red() { print_in_color "\e[31m" "$*"; }
24+
green() { print_in_color "\e[32m" "$*"; }
25+
yellow() { print_in_color "\e[33m" "$*"; }
26+
blue() { print_in_color "\e[34m" "$*"; }
27+
magenta() { print_in_color "\e[35m" "$*"; }
28+
cyan() { print_in_color "\e[36m" "$*"; }
29+
bold() { print_in_color "\e[1m" "$*"; }
30+
underlined() { print_in_color "\e[4m" "$*"; }
31+
red_bold() { print_in_color "\e[1;31m" "$*"; }
32+
green_bold() { print_in_color "\e[1;32m" "$*"; }
33+
yellow_bold() { print_in_color "\e[1;33m" "$*"; }
34+
blue_bold() { print_in_color "\e[1;34m" "$*"; }
35+
magenta_bold() { print_in_color "\e[1;35m" "$*"; }
36+
cyan_bold() { print_in_color "\e[1;36m" "$*"; }
37+
red_underlined() { print_in_color "\e[4;31m" "$*"; }
38+
green_underlined() { print_in_color "\e[4;32m" "$*"; }
39+
yellow_underlined() { print_in_color "\e[4;33m" "$*"; }
40+
blue_underlined() { print_in_color "\e[4;34m" "$*"; }
41+
magenta_underlined() { print_in_color "\e[4;35m" "$*"; }
42+
cyan_underlined() { print_in_color "\e[4;36m" "$*"; }

examples/dependencies-alt/test.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
3+
rm -f src/lib/colors.sh
4+
rm -f src/*.sh
5+
6+
set -x
7+
8+
bashly add colors
9+
bashly generate
10+
11+
### Try Me ###
12+
13+
./cli download

lib/bashly/concerns/validation_helpers.rb

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,13 @@ def assert_array(key, value, of: nil)
3535
end
3636
end
3737

38-
def assert_hash(key, value, keys: nil, of: nil)
38+
def assert_hash(key, value, keys: nil)
3939
assert value.is_a?(Hash), "#{key} must be a hash"
4040

41-
if keys
42-
invalid_keys = value.keys.map(&:to_sym) - keys
43-
assert invalid_keys.empty?, "#{key} contains invalid options: #{invalid_keys.join ', '}"
44-
end
45-
46-
return unless of
41+
return unless keys
4742

48-
value.each do |k, v|
49-
send "assert_#{of}".to_sym, "#{key}.#{k}", v
50-
end
43+
invalid_keys = value.keys.map(&:to_sym) - keys
44+
assert invalid_keys.empty?, "#{key} contains invalid options: #{invalid_keys.join ', '}"
5145
end
5246

5347
def assert_uniq(key, value, array_keys)

0 commit comments

Comments
 (0)