bashly 0.3.7 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +78 -18
- data/lib/bashly/commands/generate.rb +10 -3
- data/lib/bashly/commands/preview.rb +2 -1
- data/lib/bashly/models/command.rb +1 -1
- data/lib/bashly/models/flag.rb +4 -0
- data/lib/bashly/models/script.rb +33 -0
- data/lib/bashly/templates/lib/config.sh +15 -4
- data/lib/bashly/templates/strings.yml +1 -1
- data/lib/bashly/version.rb +1 -1
- data/lib/bashly/views/command/inspect_args.erb +7 -2
- data/lib/bashly/views/command/master_script.erb +0 -4
- data/lib/bashly/views/flag/case.erb +4 -4
- data/lib/bashly/views/script/header.erb +3 -0
- data/lib/bashly/views/script/wrapper.erb +6 -0
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7e2d8e44019822a048ede2fadd9da870df078afad2931470748912b25acfb93f
|
4
|
+
data.tar.gz: 84738b8835be3af778b0df351154c2a73c966ee906317f61534c8100a17da9f8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 653e9a5a685d321837472cb2522f93aeabc0ed0de479f97ca35dcbb1d08e2bd997d50e17653c464ce201388f6b21ac59fa4b9d28a01beed5d5119beb8b278e1b
|
7
|
+
data.tar.gz: 79c111478814fb30246a214511946331c7a581196187c41ef6bc249be3858123786bba3596484b27d493288714fb9d257fc18e7b4432b0f77effe8ed7baa740b
|
data/README.md
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
<div align='center'>
|
2
2
|
<img src='logo.svg' width=280>
|
3
3
|
|
4
|
-
Bashly - Bash CLI Framework and Generator
|
5
|
-
==================================================
|
4
|
+
# Bashly - Bash CLI Framework and Generator
|
6
5
|
|
7
6
|
Create beautiful bash scripts from simple YAML configuration
|
8
7
|
|
@@ -16,25 +15,46 @@ Create beautiful bash scripts from simple YAML configuration
|
|
16
15
|
|
17
16
|
</div>
|
18
17
|
|
18
|
+
## Table of Contents
|
19
|
+
|
20
|
+
- [Table of Contents](#table-of-contents)
|
21
|
+
- [Installation](#installation)
|
22
|
+
- [Prerequisites](#prerequisites)
|
23
|
+
- [What is Bashly](#what-is-bashly)
|
24
|
+
- [Usage](#usage)
|
25
|
+
- [Using the input arguemnts in your code](#using-the-input-arguemnts-in-your-code)
|
26
|
+
- [Examples](#examples)
|
27
|
+
- [Sample configuraiton for a script without commands](#sample-configuraiton-for-a-script-without-commands)
|
28
|
+
- [Sample configuraiton for a script with commands](#sample-configuraiton-for-a-script-with-commands)
|
29
|
+
- [Configuration Reference](#configuration-reference)
|
30
|
+
- [Command options](#command-options)
|
31
|
+
- [Argument options](#argument-options)
|
32
|
+
- [Flag options](#flag-options)
|
33
|
+
- [Environment Variable options](#environment-variable-options)
|
34
|
+
- [Real World Examples](#real-world-examples)
|
35
|
+
- [Contributing / Support](#contributing--support)
|
36
|
+
|
19
37
|
---
|
20
38
|
|
21
|
-
Installation
|
22
|
-
--------------------------------------------------
|
39
|
+
## Installation
|
23
40
|
|
24
41
|
```shell
|
25
42
|
$ gem install bashly
|
26
43
|
```
|
27
44
|
|
45
|
+
or with Docker:
|
28
46
|
|
29
|
-
|
30
|
-
|
47
|
+
```shell
|
48
|
+
$ alias bashly='docker run --rm -it --volume "$PWD:/app" dannyben/bashly'
|
49
|
+
```
|
50
|
+
|
51
|
+
## Prerequisites
|
31
52
|
|
32
53
|
The bash scripts generated by bashly require bash 4 or higher due to heavy
|
33
54
|
use of associative arrays.
|
34
55
|
|
35
56
|
|
36
|
-
What is Bashly
|
37
|
-
--------------------------------------------------
|
57
|
+
## What is Bashly
|
38
58
|
|
39
59
|
Bashly is a command line application (written in Ruby) that lets you generate
|
40
60
|
feature-rich bash command line tools.
|
@@ -61,9 +81,9 @@ Bahsly is responsible for:
|
|
61
81
|
- **Color output**.
|
62
82
|
- **Config file management** (INI format).
|
63
83
|
- **YAML parsing**.
|
84
|
+
- and more.
|
64
85
|
|
65
|
-
Usage
|
66
|
-
--------------------------------------------------
|
86
|
+
## Usage
|
67
87
|
|
68
88
|
In an empty directory, create a sample configuration file by running
|
69
89
|
|
@@ -93,9 +113,46 @@ Finally, edit the files in the `src` folder. Each of your script's commands
|
|
93
113
|
get their own file. Once you edit, run `bashly generate` again to merge the
|
94
114
|
content from your functions back into the script.
|
95
115
|
|
116
|
+
### Using the input arguemnts in your code
|
117
|
+
|
118
|
+
In order to access the parsed arguments in any of your partial scripts, you
|
119
|
+
may simply access the `$args` associative array.
|
120
|
+
|
121
|
+
For example:
|
122
|
+
|
123
|
+
1. Generate a minimal configuration with `bashly init --minimal`
|
124
|
+
2. Generate the bash script with `bashly generate`
|
125
|
+
3. Run the script with `./download hello --force`
|
126
|
+
|
127
|
+
You will notice that all the arguments of the associative array are printed
|
128
|
+
on screen. This is done by the `inspect_args` function that was inserted into
|
129
|
+
the generated partial script `src/root_command.sh`.
|
130
|
+
|
131
|
+
You can now access these variables by modifying `sec/root_command.sh` like
|
132
|
+
this:
|
133
|
+
|
96
134
|
|
97
|
-
|
98
|
-
|
135
|
+
```bash
|
136
|
+
# src/root_command.sh
|
137
|
+
source_url=${args[source]}
|
138
|
+
force=${args[--force]}
|
139
|
+
|
140
|
+
if [[ $force ]]; then
|
141
|
+
echo "downloading $source_url with --force"
|
142
|
+
else
|
143
|
+
echo "downloading $source_url"
|
144
|
+
fi
|
145
|
+
```
|
146
|
+
|
147
|
+
After editing the file, run `bashly generate` (or `bashly g` for short) and
|
148
|
+
run:
|
149
|
+
|
150
|
+
```
|
151
|
+
$ ./download a --force
|
152
|
+
downloading a with --force
|
153
|
+
```
|
154
|
+
|
155
|
+
## Examples
|
99
156
|
|
100
157
|
The `bashly.yml` file can be set up to generate two types of scripts:
|
101
158
|
|
@@ -123,8 +180,7 @@ See the [examples](examples) folder for more examples.
|
|
123
180
|
|
124
181
|
|
125
182
|
|
126
|
-
Configuration Reference
|
127
|
-
--------------------------------------------------
|
183
|
+
## Configuration Reference
|
128
184
|
|
129
185
|
The `bashly.yml` configuration file consists of these types:
|
130
186
|
|
@@ -204,17 +260,21 @@ set.
|
|
204
260
|
`required` | Specify if this variable is required.
|
205
261
|
|
206
262
|
|
207
|
-
Real World Examples
|
208
|
-
--------------------------------------------------
|
263
|
+
## Real World Examples
|
209
264
|
|
210
265
|
- [Rush][rush] - a Personal Package Manager
|
266
|
+
- [Alf][alf] - a generator for bash aliases and sub-aliases
|
267
|
+
- [git-changelog][git-changelog] - a change log generator
|
211
268
|
|
212
269
|
|
213
|
-
Contributing / Support
|
214
|
-
--------------------------------------------------
|
270
|
+
## Contributing / Support
|
215
271
|
|
216
272
|
If you experience any issue, have a question or a suggestion, or if you wish
|
217
273
|
to contribute, feel free to [open an issue][issues].
|
218
274
|
|
219
275
|
[issues]: https://github.com/DannyBen/bashly/issues
|
220
276
|
[rush]: https://github.com/DannyBen/rush-cli
|
277
|
+
[alf]: https://github.com/DannyBen/alf
|
278
|
+
[git-changelog]: https://github.com/DannyBen/git-changelog
|
279
|
+
|
280
|
+
|
@@ -3,14 +3,18 @@ module Bashly
|
|
3
3
|
class Generate < Base
|
4
4
|
help "Generate the bash script and required files"
|
5
5
|
|
6
|
-
usage "bashly generate [--force]"
|
6
|
+
usage "bashly generate [--force --wrap FUNCTION]"
|
7
7
|
usage "bashly generate (-h|--help)"
|
8
8
|
|
9
9
|
option "-f --force", "Overwrite existing files"
|
10
|
+
option "-w --wrap FUNCTION", "Wrap the entire script in a function so it can also be sourced"
|
10
11
|
|
11
12
|
environment "BASHLY_SOURCE_DIR", "The path containing the bashly configuration and source files [default: src]"
|
12
13
|
environment "BASHLY_TARGET_DIR", "The path to use for creating the bash script [default: .]"
|
13
14
|
|
15
|
+
example "bashly generate --force"
|
16
|
+
example "bashly generate --wrap my_function"
|
17
|
+
|
14
18
|
def run
|
15
19
|
create_user_files
|
16
20
|
create_master_script
|
@@ -54,12 +58,15 @@ module Bashly
|
|
54
58
|
end
|
55
59
|
|
56
60
|
def create_master_script
|
57
|
-
|
58
|
-
File.write master_script_path, master_script
|
61
|
+
File.write master_script_path, script.code
|
59
62
|
FileUtils.chmod "+x", master_script_path
|
60
63
|
say "created !txtgrn!#{master_script_path}"
|
61
64
|
end
|
62
65
|
|
66
|
+
def script
|
67
|
+
@script ||= Models::Script.new(command, args['--wrap'])
|
68
|
+
end
|
69
|
+
|
63
70
|
def master_script_path
|
64
71
|
"#{Settings.target_dir}/#{command.name}"
|
65
72
|
end
|
data/lib/bashly/models/flag.rb
CHANGED
@@ -0,0 +1,33 @@
|
|
1
|
+
module Bashly
|
2
|
+
module Models
|
3
|
+
class Script
|
4
|
+
include Renderable
|
5
|
+
|
6
|
+
attr_reader :command, :function_name
|
7
|
+
|
8
|
+
def initialize(command, function_name = nil)
|
9
|
+
@command, @function_name = command, function_name
|
10
|
+
end
|
11
|
+
|
12
|
+
def code
|
13
|
+
if function_name
|
14
|
+
result = [header, render('wrapper')].join "\n"
|
15
|
+
else
|
16
|
+
result = [header, body].join "\n"
|
17
|
+
end
|
18
|
+
|
19
|
+
result.lint
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def header
|
25
|
+
@header ||= render('header')
|
26
|
+
end
|
27
|
+
|
28
|
+
def body
|
29
|
+
@body ||= command.render('master_script')
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -21,7 +21,7 @@ config_init() {
|
|
21
21
|
# Usage: result=$(config_get hello)
|
22
22
|
config_get() {
|
23
23
|
key=$1
|
24
|
-
regex="^$key
|
24
|
+
regex="^$key *= *(.+)$"
|
25
25
|
|
26
26
|
config_init
|
27
27
|
|
@@ -44,7 +44,7 @@ config_set() {
|
|
44
44
|
|
45
45
|
config_init
|
46
46
|
|
47
|
-
regex="^($key)
|
47
|
+
regex="^($key) *= *.+$"
|
48
48
|
output=""
|
49
49
|
found_key=""
|
50
50
|
|
@@ -71,7 +71,7 @@ config_set() {
|
|
71
71
|
config_del() {
|
72
72
|
key=$1
|
73
73
|
|
74
|
-
regex="^($key)
|
74
|
+
regex="^($key) *="
|
75
75
|
output=""
|
76
76
|
|
77
77
|
config_init
|
@@ -100,7 +100,7 @@ config_show() {
|
|
100
100
|
# done
|
101
101
|
#
|
102
102
|
config_keys() {
|
103
|
-
regex="^(
|
103
|
+
regex="^([a-zA-Z0-9_\-\/\.]+) *="
|
104
104
|
|
105
105
|
config_init
|
106
106
|
|
@@ -113,3 +113,14 @@ config_keys() {
|
|
113
113
|
done < "$CONFIG_FILE"
|
114
114
|
echo "${keys[@]}"
|
115
115
|
}
|
116
|
+
|
117
|
+
# Returns true if the specified key exists in the config file
|
118
|
+
# Usage:
|
119
|
+
#
|
120
|
+
# if config_has_key "key" ; then
|
121
|
+
# echo "key exists"
|
122
|
+
# fi
|
123
|
+
#
|
124
|
+
config_has_key() {
|
125
|
+
[[ $(config_get "$1") ]]
|
126
|
+
}
|
@@ -21,7 +21,7 @@ help_flag_text: Show this help
|
|
21
21
|
version_flag_text: Show version number
|
22
22
|
|
23
23
|
# Error messages
|
24
|
-
flag_requires_an_argument: "%{
|
24
|
+
flag_requires_an_argument: "%{name} requires an argument: %{usage}"
|
25
25
|
invalid_argument: "invalid argument: %s"
|
26
26
|
invalid_flag: "invalid option: %s"
|
27
27
|
missing_required_argument: "missing required argument: %{arg}\\nusage: %{usage}"
|
data/lib/bashly/version.rb
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
# :command.inspect_args
|
2
2
|
inspect_args() {
|
3
|
-
|
4
|
-
|
3
|
+
readarray -t sorted_keys < <(printf '%s\n' "${!args[@]}" | sort)
|
4
|
+
if (( ${#args[@]} )); then
|
5
|
+
echo args:
|
6
|
+
for k in "${sorted_keys[@]}"; do echo "- \${args[$k]} = ${args[$k]}"; done
|
7
|
+
else
|
8
|
+
echo args: none
|
9
|
+
fi
|
5
10
|
}
|
@@ -1,16 +1,16 @@
|
|
1
1
|
# :flag.case
|
2
2
|
<%= aliases.join " | " %> )
|
3
3
|
<%- if arg -%>
|
4
|
-
if [[ $2
|
5
|
-
args[<%=
|
4
|
+
if [[ $2 ]]; then
|
5
|
+
args[<%= name %>]="$2"
|
6
6
|
shift
|
7
7
|
shift
|
8
8
|
else
|
9
|
-
printf "%s\n" "<%= strings[:flag_requires_an_argument] % {
|
9
|
+
printf "%s\n" "<%= strings[:flag_requires_an_argument] % { name: name, usage: usage_string } %>"
|
10
10
|
exit 1
|
11
11
|
fi
|
12
12
|
<%- else -%>
|
13
|
-
args[<%=
|
13
|
+
args[<%= name %>]=1
|
14
14
|
shift
|
15
15
|
<%- end -%>
|
16
16
|
;;
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bashly
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Danny Ben Shitrit
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-02-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: colsole
|
@@ -80,6 +80,7 @@ files:
|
|
80
80
|
- lib/bashly/models/command.rb
|
81
81
|
- lib/bashly/models/environment_variable.rb
|
82
82
|
- lib/bashly/models/flag.rb
|
83
|
+
- lib/bashly/models/script.rb
|
83
84
|
- lib/bashly/polyfills/hash.rb
|
84
85
|
- lib/bashly/settings.rb
|
85
86
|
- lib/bashly/templates/bashly.yml
|
@@ -123,6 +124,8 @@ files:
|
|
123
124
|
- lib/bashly/views/environment_variable/usage.erb
|
124
125
|
- lib/bashly/views/flag/case.erb
|
125
126
|
- lib/bashly/views/flag/usage.erb
|
127
|
+
- lib/bashly/views/script/header.erb
|
128
|
+
- lib/bashly/views/script/wrapper.erb
|
126
129
|
homepage: https://github.com/dannyben/bashly
|
127
130
|
licenses:
|
128
131
|
- MIT
|
@@ -142,7 +145,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
142
145
|
- !ruby/object:Gem::Version
|
143
146
|
version: '0'
|
144
147
|
requirements: []
|
145
|
-
rubygems_version: 3.
|
148
|
+
rubygems_version: 3.2.3
|
146
149
|
signing_key:
|
147
150
|
specification_version: 4
|
148
151
|
summary: Bash Command Line Tool Generator
|