Skip to content

Commit dd95436

Browse files
authored
- Add settings.yml as an alternative to environment variables
1 parent 8efb62e commit dd95436

File tree

19 files changed

+415
-148
lines changed

19 files changed

+415
-148
lines changed

Runfile

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ end
2020
help "Run shellcheck on all examples"
2121
action :shellcheck do
2222
Example.executables.each do |example|
23-
success = system "shellcheck #{example}"
24-
color = success ? 'txtgrn' : 'txtred'
25-
say "- shellcheck !#{color}!#{example}"
26-
exit 1 unless success
23+
if File.exist? example
24+
success = system "shellcheck #{example}"
25+
color = success ? 'txtgrn' : 'txtred'
26+
say "- shellcheck !#{color}!#{example}"
27+
exit 1 unless success
28+
else
29+
say "- skip !txtcyn!#{example}"
30+
end
2731
end
2832
end
2933

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,4 @@ Each of these examples demonstrates one aspect or feature of bashly.
5858
## Other Examples
5959

6060
- [heredoc](heredoc#readme) - using heredoc strings
61+
- [settings](settings#readme) - using the settings.yml file to adjust bashly's behavior

examples/settings/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/src/*.sh
2+
/out/cli

examples/settings/README.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Settings Example
2+
3+
Demonstrates how to use the bashly `settings.yml` file to configure some
4+
aspects of the script generation commands.
5+
6+
This example was generated with:
7+
8+
```bash
9+
$ bashly add settings
10+
# ... now edit settings.yml file to match the example ...
11+
$ bashly init
12+
$ bashly generate
13+
```
14+
15+
<!-- include: settings.yml -->
16+
17+
-----
18+
19+
## `bashly.yml`
20+
21+
```yaml
22+
name: cli
23+
help: Sample application
24+
version: 0.1.0
25+
26+
environment_variables:
27+
- name: api_key
28+
help: Set your API key
29+
30+
commands:
31+
- name: download
32+
short: d
33+
help: Download a file
34+
35+
args:
36+
- name: source
37+
required: true
38+
help: URL to download from
39+
- name: target
40+
help: "Target filename (default: same as source)"
41+
42+
flags:
43+
- long: --force
44+
short: -f
45+
help: Overwrite existing files
46+
47+
examples:
48+
- cli download example.com
49+
- cli download example.com ./output -f
50+
51+
environment_variables:
52+
- name: default_target_location
53+
help: Set the default location to download to
54+
55+
- name: upload
56+
short: u
57+
help: Upload a file
58+
args:
59+
- name: source
60+
required: true
61+
help: File to upload
62+
63+
flags:
64+
- long: --user
65+
short: -u
66+
arg: user
67+
help: Username to use for logging in
68+
required: true
69+
- long: --password
70+
short: -p
71+
arg: password
72+
help: Password to use for logging in
73+
```
74+
75+
## `settings.yml`
76+
77+
```yaml
78+
target_dir: out
79+
strict: true
80+
81+
```
82+
83+
84+
## Generated script output
85+
86+
### `$ ./out/cli`
87+
88+
```shell
89+
cli - Sample application
90+
91+
Usage:
92+
cli [command]
93+
cli [command] --help | -h
94+
cli --version | -v
95+
96+
Commands:
97+
download Download a file
98+
upload Upload a file
99+
100+
101+
102+
```
103+
104+
105+

examples/settings/out/.keep

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

examples/settings/settings.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
target_dir: out
2+
strict: true

examples/settings/src/bashly.yml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: cli
2+
help: Sample application
3+
version: 0.1.0
4+
5+
environment_variables:
6+
- name: api_key
7+
help: Set your API key
8+
9+
commands:
10+
- name: download
11+
short: d
12+
help: Download a file
13+
14+
args:
15+
- name: source
16+
required: true
17+
help: URL to download from
18+
- name: target
19+
help: "Target filename (default: same as source)"
20+
21+
flags:
22+
- long: --force
23+
short: -f
24+
help: Overwrite existing files
25+
26+
examples:
27+
- cli download example.com
28+
- cli download example.com ./output -f
29+
30+
environment_variables:
31+
- name: default_target_location
32+
help: Set the default location to download to
33+
34+
- name: upload
35+
short: u
36+
help: Upload a file
37+
args:
38+
- name: source
39+
required: true
40+
help: File to upload
41+
42+
flags:
43+
- long: --user
44+
short: -u
45+
arg: user
46+
help: Username to use for logging in
47+
required: true
48+
- long: --password
49+
short: -p
50+
arg: password
51+
help: Password to use for logging in

examples/settings/test.sh

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

lib/bashly/commands/add.rb

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,31 @@ module Commands
33
class Add < Base
44
help "Add extra features and customization to your script"
55

6-
usage "bashly add strings [--force]"
7-
usage "bashly add lib [--force]"
8-
usage "bashly add config [--force]"
96
usage "bashly add colors [--force]"
10-
usage "bashly add yaml [--force]"
11-
usage "bashly add validations [--force]"
12-
usage "bashly add test [--force]"
137
usage "bashly add comp FORMAT [OUTPUT --force]"
8+
usage "bashly add config [--force]"
9+
usage "bashly add lib [--force]"
10+
usage "bashly add settings [--force]"
11+
usage "bashly add strings [--force]"
12+
usage "bashly add test [--force]"
13+
usage "bashly add validations [--force]"
14+
usage "bashly add yaml [--force]"
1415
usage "bashly add (-h|--help)"
1516

1617
option "-f --force", "Overwrite existing files"
1718

1819
param "FORMAT", "Output format, can be one of:\n function : generate a function file to be included in your script.\n script : generate a standalone bash completions script.\n yaml : generate a yaml compatible with completely."
1920
param "OUTPUT", "For the 'comp function' command: Name of the generated function.\nFor the 'comp script' or 'comp yaml' commands: path to output file.\nIn all cases, this is optional and will have sensible defaults."
2021

21-
command "strings", "Copy an additional configuration file to your project, allowing you to customize all the tips and error strings."
22-
command "lib", "Create the additional lib directory for additional user scripts. All *.sh scripts in this folder will be included in the final bash script."
23-
command "config", "Add standard functions for handling INI files to the lib directory."
2422
command "colors", "Add standard functions for printing colorful and formatted text to the lib directory."
25-
command "yaml", "Add standard functions for reading YAML files to the lib directory."
26-
command "validations", "Add argument validation functions to the lib directory."
27-
command "test", "Add approval testing."
2823
command "comp", "Generate a bash completions script or function."
24+
command "config", "Add standard functions for handling INI files to the lib directory."
25+
command "lib", "Create the additional lib directory for additional user scripts. All *.sh scripts in this folder will be included in the final bash script."
26+
command "settings", "Copy a sample settings.yml file to your project, allowing you to customize some bashly options."
27+
command "strings", "Copy an additional configuration file to your project, allowing you to customize all the tips and error strings."
28+
command "test", "Add approval testing."
29+
command "validations", "Add argument validation functions to the lib directory."
30+
command "yaml", "Add standard functions for reading YAML files to the lib directory."
2931

3032
example "bashly add strings --force"
3133
example "bashly add comp function"
@@ -34,45 +36,51 @@ class Add < Base
3436
environment "BASHLY_SOURCE_DIR", "The path containing the bashly configuration and source files [default: src]"
3537
environment "BASHLY_LIB_DIR", "The path to use for creating the library files, relative to the source dir [default: lib]"
3638

37-
def strings_command
38-
add_lib 'strings'
39+
attr_reader :skip_src_check
40+
41+
def colors_command
42+
add_lib 'colors'
3943
end
4044

41-
def lib_command
42-
add_lib 'lib'
45+
def comp_command
46+
format = args['FORMAT']
47+
output = args['OUTPUT']
48+
49+
case format
50+
when "script" then add_lib 'completions_script', output
51+
when "function" then add_lib 'completions', output
52+
when "yaml" then add_lib 'completions_yaml', output
53+
else raise Error, "Unrecognized format: #{format}"
54+
end
4355
end
4456

4557
def config_command
4658
add_lib 'config'
4759
end
4860

49-
def colors_command
50-
add_lib 'colors'
61+
def settings_command
62+
@skip_src_check = true
63+
add_lib 'settings'
5164
end
5265

53-
def yaml_command
54-
add_lib 'yaml'
66+
def strings_command
67+
add_lib 'strings'
5568
end
5669

57-
def validations_command
58-
add_lib 'validations'
70+
def lib_command
71+
add_lib 'lib'
5972
end
6073

6174
def test_command
6275
add_lib 'test'
6376
end
6477

65-
def comp_command
66-
format = args['FORMAT']
67-
output = args['OUTPUT']
68-
69-
case format
70-
when "script" then add_lib 'completions_script', output
71-
when "function" then add_lib 'completions', output
72-
when "yaml" then add_lib 'completions_yaml', output
73-
else raise Error, "Unrecognized format: #{format}"
74-
end
78+
def yaml_command
79+
add_lib 'yaml'
80+
end
7581

82+
def validations_command
83+
add_lib 'validations'
7684
end
7785

7886
private
@@ -89,7 +97,7 @@ def add_lib(name, *args)
8997
end
9098

9199
def safe_write(path, content)
92-
if !Dir.exist? Settings.source_dir
100+
if !skip_src_check and !Dir.exist? Settings.source_dir
93101
raise InitError, "Directory !txtgrn!#{Settings.source_dir}!txtrst! does not exist\nRun !txtpur!bashly init!txtrst! first"
94102
end
95103

lib/bashly/libraries.yml

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,25 @@ colors:
33
- source: "templates/lib/colors.sh"
44
target: "%{user_lib_dir}/colors.sh"
55

6+
completions: :CompletionsFunction
7+
completions_script: :CompletionsScript
8+
completions_yaml: :CompletionsYAML
9+
610
config:
711
files:
812
- source: "templates/lib/config.sh"
913
target: "%{user_lib_dir}/config.sh"
1014

11-
yaml:
12-
files:
13-
- source: "templates/lib/yaml.sh"
14-
target: "%{user_lib_dir}/yaml.sh"
15-
1615
lib:
1716
files:
1817
- source: "templates/lib/sample_function.sh"
1918
target: "%{user_lib_dir}/sample_function.sh"
2019

20+
settings:
21+
files:
22+
- source: "templates/settings.yml"
23+
target: "settings.yml"
24+
2125
strings:
2226
files:
2327
- source: "templates/strings.yml"
@@ -37,7 +41,6 @@ test:
3741
3842
Docs: !undblu!https://github.com/DannyBen/approvals.bash
3943
40-
4144
validations:
4245
files:
4346
- source: "templates/lib/validations/validate_dir_exists.sh"
@@ -49,6 +52,7 @@ validations:
4952
- source: "templates/lib/validations/validate_not_empty.sh"
5053
target: "%{user_lib_dir}/validations/validate_not_empty.sh"
5154

52-
completions: :CompletionsFunction
53-
completions_script: :CompletionsScript
54-
completions_yaml: :CompletionsYAML
55+
yaml:
56+
files:
57+
- source: "templates/lib/yaml.sh"
58+
target: "%{user_lib_dir}/yaml.sh"

0 commit comments

Comments
 (0)