bashly 1.0.7 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a333ce419f5ba64e38000497f245fc5baf822243cd2765c1805ebdf6de2e3b46
4
- data.tar.gz: 0c9ab24cc2cecd88cbd20a06ab11028740ce3000aec75080ca9a12c6769110c1
3
+ metadata.gz: b82e5ead650cc1774cf3870af91b9046b1e5f69f05484d833f72dd4090eab091
4
+ data.tar.gz: 26626b09b79382d2d4530241b5517ed04d3e4a27c004130adadad3b27421f100
5
5
  SHA512:
6
- metadata.gz: ff95fd919a29a16d610d8f3c069f2de17b9984a41251de56e484a5dc49e86dd4114934c47ccb2274521f7f8bd5078287a54ef10478c5e18b6c8a93f462f1c503
7
- data.tar.gz: 65a1be57601cc7a560a19ef48dc75b09647244fe477327f23ae52a37f8114afca4ee3871a1f72e1b046589e252a57701956a93e9015907bf76fd24455da341ff
6
+ metadata.gz: 9e3b78d5975c92954347be94d567f28d240d628e080fb3d136382cc4f8fa1ae81827057ad3b61ac144b5ab930c3f5353128f9d6068b983d0e2e77ac996d4ab0d
7
+ data.tar.gz: 2595428ca74b3ada25619fc7839f99993f98bcda169b47fcda84c82b279ebcaa1882864d2d842c67f05ca1ca946f2f64867d6905892f2ff5f5f5107f32f49037
@@ -4,7 +4,7 @@ module Bashly
4
4
  summary 'Install bash completions for bashly itself'
5
5
  help 'Display the bash completions script or install it directly to your bash completions directory'
6
6
 
7
- usage 'bashly completions [--install --uninstall]'
7
+ usage 'bashly completions [--install|--uninstall]'
8
8
  usage 'bashly completions (-h|--help)'
9
9
 
10
10
  option '-i --install', 'Install the completions script to your bash completions directory'
@@ -1,128 +1,108 @@
1
- ## Config functions [@bashly-upgrade config]
1
+ ## Config (INI) functions [@bashly-upgrade config]
2
2
  ## This file is a part of Bashly standard library
3
3
  ##
4
4
  ## Usage:
5
- ## - In your script, set the CONFIG_FILE variable. For rxample:
6
- ## CONFIG_FILE=settings.ini.
7
- ## If it is unset, it will default to 'config.ini'.
8
- ## - Use any of the functions below to access the config file.
9
5
  ##
10
- ## Create a new config file.
11
- ## There is normally no need to use this function, it is used by other
12
- ## functions as needed.
6
+ ## - Set the global variable CONFIG_FILE to the path of your INI file somewhere
7
+ ## in your script (for example, in src/initialize.sh).
8
+ ## - Use any of the following functions to access and manipulate the values.
9
+ ## - INI sections are optional (i.e., sectionless key=value pairs are allowed).
13
10
  ##
14
- config_init() {
15
- CONFIG_FILE=${CONFIG_FILE:=config.ini}
16
- [[ -f "$CONFIG_FILE" ]] || touch "$CONFIG_FILE"
11
+
12
+ ## Show all the key=value pairs from your config file
13
+ config_show() {
14
+ config_load
15
+ ini_show
17
16
  }
18
17
 
19
- ## Get a value from the config.
20
- ## Usage: result=$(config_get hello)
18
+ ## Get a single value from the config file:
19
+ ##
20
+ ## config_get login.email
21
+ ##
22
+ ## Get the value or a default one if it is not set:
23
+ ##
24
+ ## config_get login.user guest
25
+ ##
26
+ ## Assign the result to a variable:
27
+ ##
28
+ ## theme="$(config_get interface.theme)"
29
+ ##
21
30
  config_get() {
22
- local key=$1
23
- local regex="^$key *= *(.+)$"
24
- local value=""
25
-
26
- config_init
31
+ local key="$1"
32
+ local default_value="$2"
27
33
 
28
- while IFS= read -r line || [ -n "$line" ]; do
29
- if [[ $line =~ $regex ]]; then
30
- value="${BASH_REMATCH[1]}"
31
- break
32
- fi
33
- done <"$CONFIG_FILE"
34
-
35
- echo "$value"
34
+ config_load
35
+ echo "${ini["$key"]:-$default_value}"
36
36
  }
37
37
 
38
- ## Add or update a key=value pair in the config.
39
- ## Usage: config_set key value
38
+ ## Create/update a key=value pair:
39
+ ##
40
+ ## config_set cloud.provider aws
41
+ ##
40
42
  config_set() {
41
- local key=$1
43
+ local key="$1"
42
44
  shift
43
45
  local value="$*"
44
46
 
45
- config_init
46
-
47
- local regex="^($key) *= *.+$"
48
- local output=""
49
- local found_key=""
50
- local newline
51
-
52
- while IFS= read -r line || [ -n "$line" ]; do
53
- newline=$line
54
- if [[ $line =~ $regex ]]; then
55
- found_key="${BASH_REMATCH[1]}"
56
- newline="$key = $value"
57
- output="$output$newline\n"
58
- elif [[ $line ]]; then
59
- output="$output$line\n"
60
- fi
61
- done <"$CONFIG_FILE"
62
-
63
- if [[ -z $found_key ]]; then
64
- output="$output$key = $value\n"
65
- fi
66
-
67
- printf "%b\n" "$output" >"$CONFIG_FILE"
47
+ config_load
48
+ ini["$key"]="$value"
49
+ config_save
68
50
  }
69
51
 
70
- ## Delete a key from the config.
71
- ## Usage: config_del key
52
+ ## Delete a key=value pair:
53
+ ##
54
+ ## config_del login.email
55
+ ##
72
56
  config_del() {
73
- local key=$1
57
+ local key="$1"
74
58
 
75
- local regex="^($key) *="
76
- local output=""
77
-
78
- config_init
79
-
80
- while IFS= read -r line || [ -n "$line" ]; do
81
- if [[ $line ]] && [[ ! $line =~ $regex ]]; then
82
- output="$output$line\n"
83
- fi
84
- done <"$CONFIG_FILE"
85
-
86
- printf "%b\n" "$output" >"$CONFIG_FILE"
87
- }
88
-
89
- ## Show the config file
90
- config_show() {
91
- config_init
92
- cat "$CONFIG_FILE"
59
+ config_load
60
+ unset "ini[$key]"
61
+ config_save
93
62
  }
94
63
 
95
- ## Return an array of the keys in the config file.
96
- ## Usage:
64
+ ## Get an array of all keys:
97
65
  ##
98
- ## for k in $(config_keys); do
99
- ## echo "- $k = $(config_get "$k")";
66
+ ## for key in $(config_keys); do
67
+ ## echo "- $key = $(config_get "$key")";
100
68
  ## done
101
69
  ##
102
70
  config_keys() {
103
- local regex="^([a-zA-Z0-9_\-\/\.]+) *="
104
-
105
- config_init
106
-
107
- local keys=()
108
- local key
109
-
110
- while IFS= read -r line || [ -n "$line" ]; do
111
- if [[ $line =~ $regex ]]; then
112
- key="${BASH_REMATCH[1]}"
113
- keys+=("$key")
114
- fi
115
- done <"$CONFIG_FILE"
116
- echo "${keys[@]}"
71
+ config_load
72
+ ini_keys
117
73
  }
118
74
 
119
- ## Returns true if the specified key exists in the config file.
120
- ## Usage:
75
+ ## Check if a key exists:
121
76
  ##
122
- ## if config_has_key "key"; then
77
+ ## if config_has_key login.password; then
123
78
  ## echo "key exists"
124
79
  ## fi
125
80
  ##
126
81
  config_has_key() {
127
82
  [[ $(config_get "$1") ]]
128
83
  }
84
+
85
+ ## Force-load from file
86
+ ## This should normally not be called, unless you suspect that the INI file
87
+ ## was modified by external means during the run of your script.
88
+ config_reload() {
89
+ declare -g config_loaded=false
90
+ config_load
91
+ }
92
+
93
+ ## Load an INI file (unless loaded) and populate the associative array
94
+ ## NOTE: Normally there is no need to call this function, it is called as needed
95
+ config_load() {
96
+ [[ "$config_loaded" == "true" ]] && return
97
+
98
+ declare -g CONFIG_FILE=${CONFIG_FILE:=config.ini}
99
+ declare -g config_loaded=true
100
+ [[ -f "$CONFIG_FILE" ]] || touch "$CONFIG_FILE"
101
+ ini_load "$CONFIG_FILE"
102
+ }
103
+
104
+ ## Save the associative array back to a file
105
+ ## NOTE: Normally there is no need to call this function, it is called as needed
106
+ config_save() {
107
+ ini_save "$CONFIG_FILE"
108
+ }
@@ -0,0 +1,110 @@
1
+ ## INI functions [@bashly-upgrade ini]
2
+ ## This file is a part of Bashly standard library
3
+ ##
4
+ ## Usage:
5
+ ##
6
+ ## - In your script, call `ini_load path/to/config.ini`.
7
+ ## - A global associative array named `ini` will become available to you,
8
+ ## - When updating any of the associative array's values, call
9
+ ## `ini_save path/to/config.ini` to save the data.
10
+ ## - INI sections are optional.
11
+ ##
12
+ ## Get a value:
13
+ ##
14
+ ## ${ini[section1.key1]} # for keys under a [section]
15
+ ## ${ini[key1]} # for keys not under a [section]
16
+ ## ${ini[key1]:-default} # get a default value if the INI key is unset
17
+ ##
18
+ ## Update/create a value:
19
+ ##
20
+ ## ini[section1.key1]="value"
21
+ ## ini_save path/to/config.ini
22
+ ##
23
+ ## Delete a value
24
+ ##
25
+ ## unset ini[section1.key1]
26
+ ## ini_save path/to/config.ini
27
+ ##
28
+
29
+ ## Load an INI file and populate the associative array `ini`.
30
+ ini_load() {
31
+ declare -gA ini
32
+
33
+ local ini_file="$1"
34
+
35
+ local section=""
36
+ local key=""
37
+ local value=""
38
+ local section_regex="^\[(.+)\]"
39
+ local key_regex="^([^ =]+) *= *(.*) *$"
40
+ local comment_regex="^;"
41
+
42
+ while IFS= read -r line; do
43
+ if [[ $line =~ $comment_regex ]]; then
44
+ continue
45
+ elif [[ $line =~ $section_regex ]]; then
46
+ section="${BASH_REMATCH[1]}."
47
+ elif [[ $line =~ $key_regex ]]; then
48
+ key="${BASH_REMATCH[1]}"
49
+ value="${BASH_REMATCH[2]}"
50
+ ini["${section}${key}"]="$value"
51
+ fi
52
+ done <"$ini_file"
53
+ }
54
+
55
+ ## Save the associative array `ini` back to a file
56
+ ini_save() {
57
+ declare -gA ini
58
+
59
+ local ini_file="$1"
60
+
61
+ local current_section=""
62
+ local has_free_keys=false
63
+
64
+ rm -f "$ini_file"
65
+
66
+ for key in $(ini_keys); do
67
+ [[ $key == *.* ]] && continue
68
+ has_free_keys=true
69
+ value="${ini[$key]}"
70
+ echo "$key = $value" >>"$ini_file"
71
+ done
72
+
73
+ [[ "${has_free_keys}" == "true" ]] && echo >>"$ini_file"
74
+
75
+ for key in $(ini_keys); do
76
+ [[ $key == *.* ]] || continue
77
+ value="${ini[$key]}"
78
+ IFS="." read -r section_name key_name <<<"$key"
79
+
80
+ if [[ "$current_section" != "$section_name" ]]; then
81
+ [[ $current_section ]] && echo >>"$ini_file"
82
+ echo "[$section_name]" >>"$ini_file"
83
+ current_section="$section_name"
84
+ fi
85
+
86
+ echo "$key_name = $value" >>"$ini_file"
87
+ done
88
+ }
89
+
90
+ ## Show all loaded key-value pairs
91
+ ini_show() {
92
+ declare -gA ini
93
+
94
+ for key in $(ini_keys); do
95
+ echo "$key = ${ini[$key]}"
96
+ done
97
+ }
98
+
99
+ ## Get an array of all keys:
100
+ ##
101
+ ## for key in $(ini_keys); do
102
+ ## echo "- $key = ${ini[$key]}";
103
+ ## done
104
+ ##
105
+ ini_keys() {
106
+ declare -gA ini
107
+
108
+ local keys=("${!ini[@]}")
109
+ for a in "${keys[@]}"; do echo "$a"; done | sort
110
+ }
@@ -20,10 +20,12 @@ completions_yaml:
20
20
  handler: Bashly::Libraries::CompletionsYAML
21
21
 
22
22
  config:
23
- help: Add standard functions for handling INI files to the lib directory.
23
+ help: Add functions for handling INI configuration files to the lib directory.
24
24
  files:
25
25
  - source: "config/config.sh"
26
26
  target: "%{user_lib_dir}/config.%{user_ext}"
27
+ - source: "ini/ini.sh"
28
+ target: "%{user_lib_dir}/ini.%{user_ext}"
27
29
 
28
30
  help:
29
31
  help: Add a help command, in addition to the standard --help flag.
@@ -39,6 +41,12 @@ hooks:
39
41
  - source: "hooks/after.sh"
40
42
  target: "%{user_source_dir}/after.%{user_ext}"
41
43
 
44
+ ini:
45
+ help: Add low level functions for reading/writing INI files to the lib directory.
46
+ files:
47
+ - source: "ini/ini.sh"
48
+ target: "%{user_lib_dir}/ini.%{user_ext}"
49
+
42
50
  lib:
43
51
  help: |-
44
52
  Create the lib directory for any additional user scripts.
@@ -1,3 +1,3 @@
1
1
  module Bashly
2
- VERSION = '1.0.7'
2
+ VERSION = '1.0.8'
3
3
  end
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: 1.0.7
4
+ version: 1.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Ben Shitrit
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-07-07 00:00:00.000000000 Z
11
+ date: 2023-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colsole
@@ -114,6 +114,26 @@ dependencies:
114
114
  - - "~>"
115
115
  - !ruby/object:Gem::Version
116
116
  version: '1.0'
117
+ - !ruby/object:Gem::Dependency
118
+ name: psych
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: 3.3.2
124
+ - - "<"
125
+ - !ruby/object:Gem::Version
126
+ version: '7'
127
+ type: :runtime
128
+ prerelease: false
129
+ version_requirements: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: 3.3.2
134
+ - - "<"
135
+ - !ruby/object:Gem::Version
136
+ version: '7'
117
137
  description: Generate bash command line tools using YAML configuration
118
138
  email: db@dannyben.com
119
139
  executables:
@@ -164,6 +184,7 @@ files:
164
184
  - lib/bashly/libraries/hooks/after.sh
165
185
  - lib/bashly/libraries/hooks/before.sh
166
186
  - lib/bashly/libraries/hooks/initialize.sh
187
+ - lib/bashly/libraries/ini/ini.sh
167
188
  - lib/bashly/libraries/lib/sample_function.sh
168
189
  - lib/bashly/libraries/libraries.yml
169
190
  - lib/bashly/libraries/settings/settings.yml
@@ -253,7 +274,7 @@ metadata:
253
274
  homepage_uri: https://bashly.dannyb.co/
254
275
  source_code_uri: https://github.com/DannyBen/bashly
255
276
  rubygems_mfa_required: 'true'
256
- post_install_message:
277
+ post_install_message:
257
278
  rdoc_options: []
258
279
  require_paths:
259
280
  - lib
@@ -268,8 +289,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
268
289
  - !ruby/object:Gem::Version
269
290
  version: '0'
270
291
  requirements: []
271
- rubygems_version: 3.4.10
272
- signing_key:
292
+ rubygems_version: 3.4.18
293
+ signing_key:
273
294
  specification_version: 4
274
295
  summary: Bash Command Line Tool Generator
275
296
  test_files: []