From efe00a30bae1f044a3a031a6f5607fbbb491ce88 Mon Sep 17 00:00:00 2001 From: Danny Ben Shitrit Date: Fri, 29 Oct 2021 15:41:30 +0000 Subject: [PATCH 1/2] - Add support for hidden comments --- examples/colors/src/lib/colors.sh | 25 +++---- .../lib/validations/validate_dir_exists.sh | 2 +- .../lib/validations/validate_file_exists.sh | 2 +- .../src/lib/validations/validate_integer.sh | 2 +- .../src/lib/validations/validate_not_empty.sh | 2 +- lib/bashly/extensions/string.rb | 2 +- lib/bashly/templates/lib/colors.sh | 27 ++++---- lib/bashly/templates/lib/config.sh | 69 +++++++++---------- lib/bashly/templates/lib/sample_function.sh | 20 +++--- .../lib/validations/validate_dir_exists.sh | 2 +- .../lib/validations/validate_file_exists.sh | 2 +- .../lib/validations/validate_integer.sh | 2 +- .../lib/validations/validate_not_empty.sh | 2 +- lib/bashly/templates/lib/yaml.sh | 27 ++++---- spec/approvals/library/base/dir | 8 +-- spec/bashly/extensions/string_spec.rb | 16 ++++- 16 files changed, 105 insertions(+), 105 deletions(-) diff --git a/examples/colors/src/lib/colors.sh b/examples/colors/src/lib/colors.sh index 5d7106a3..960a4f82 100644 --- a/examples/colors/src/lib/colors.sh +++ b/examples/colors/src/lib/colors.sh @@ -1,17 +1,14 @@ -# --- -# Color functions [@bashly-upgrade colors] -# This file is a part of Bashly standard library -# -# Usage: -# Use any of the functions below to color or format a portion of a string. -# -# echo "before $(red this is red) after" -# echo "before $(green_bold this is green_bold) after" -# -# Color output will be disabled if `NO_COLOR` environment variable is set -# in compliance with https://no-color.org/ -# -# --- +## Color functions [@bashly-upgrade colors] +## This file is a part of Bashly standard library +## +## Usage: +## Use any of the functions below to color or format a portion of a string. +## +## echo "before $(red this is red) after" +## echo "before $(green_bold this is green_bold) after" +## +## Color output will be disabled if `NO_COLOR` environment variable is set +## in compliance with https://no-color.org/ print_in_color() { local color="$1" diff --git a/examples/validations/src/lib/validations/validate_dir_exists.sh b/examples/validations/src/lib/validations/validate_dir_exists.sh index 9602b9e1..76b3d6db 100644 --- a/examples/validations/src/lib/validations/validate_dir_exists.sh +++ b/examples/validations/src/lib/validations/validate_dir_exists.sh @@ -1,4 +1,4 @@ -# [@bashly-upgrade validations] +## [@bashly-upgrade validations] validate_dir_exists() { [[ -d "$1" ]] || echo "must be an existing directory" } diff --git a/examples/validations/src/lib/validations/validate_file_exists.sh b/examples/validations/src/lib/validations/validate_file_exists.sh index b4024852..2143687d 100644 --- a/examples/validations/src/lib/validations/validate_file_exists.sh +++ b/examples/validations/src/lib/validations/validate_file_exists.sh @@ -1,4 +1,4 @@ -# [@bashly-upgrade validations] +## [@bashly-upgrade validations] validate_file_exists() { [[ -f "$1" ]] || echo "must be an existing file" } diff --git a/examples/validations/src/lib/validations/validate_integer.sh b/examples/validations/src/lib/validations/validate_integer.sh index da99c9fc..021168be 100644 --- a/examples/validations/src/lib/validations/validate_integer.sh +++ b/examples/validations/src/lib/validations/validate_integer.sh @@ -1,4 +1,4 @@ -# [@bashly-upgrade validations] +## [@bashly-upgrade validations] validate_integer() { [[ "$1" =~ ^[0-9]+$ ]] || echo "must be an integer" } \ No newline at end of file diff --git a/examples/validations/src/lib/validations/validate_not_empty.sh b/examples/validations/src/lib/validations/validate_not_empty.sh index d81c16c9..9578dab8 100644 --- a/examples/validations/src/lib/validations/validate_not_empty.sh +++ b/examples/validations/src/lib/validations/validate_not_empty.sh @@ -1,4 +1,4 @@ -# [@bashly-upgrade validations] +## [@bashly-upgrade validations] validate_not_empty() { [[ -z "$1" ]] && echo "must not be empty" } diff --git a/lib/bashly/extensions/string.rb b/lib/bashly/extensions/string.rb index 940e9bcb..b68efc1e 100644 --- a/lib/bashly/extensions/string.rb +++ b/lib/bashly/extensions/string.rb @@ -24,7 +24,7 @@ def wrap(length = 80) end def lint - gsub(/\s+\n/m, "\n\n") + gsub(/\s+\n/m, "\n\n").lines.reject { |l| l =~ /^\s*##/ }.join "" end end diff --git a/lib/bashly/templates/lib/colors.sh b/lib/bashly/templates/lib/colors.sh index 5d7106a3..25bc07e9 100644 --- a/lib/bashly/templates/lib/colors.sh +++ b/lib/bashly/templates/lib/colors.sh @@ -1,18 +1,15 @@ -# --- -# Color functions [@bashly-upgrade colors] -# This file is a part of Bashly standard library -# -# Usage: -# Use any of the functions below to color or format a portion of a string. -# -# echo "before $(red this is red) after" -# echo "before $(green_bold this is green_bold) after" -# -# Color output will be disabled if `NO_COLOR` environment variable is set -# in compliance with https://no-color.org/ -# -# --- - +## Color functions [@bashly-upgrade colors] +## This file is a part of Bashly standard library +## +## Usage: +## Use any of the functions below to color or format a portion of a string. +## +## echo "before $(red this is red) after" +## echo "before $(green_bold this is green_bold) after" +## +## Color output will be disabled if `NO_COLOR` environment variable is set +## in compliance with https://no-color.org/ +## print_in_color() { local color="$1" shift diff --git a/lib/bashly/templates/lib/config.sh b/lib/bashly/templates/lib/config.sh index 4c76db3f..a7a8a7fd 100644 --- a/lib/bashly/templates/lib/config.sh +++ b/lib/bashly/templates/lib/config.sh @@ -1,24 +1,23 @@ -# --- -# Config functions [@bashly-upgrade config] -# This file is a part of Bashly standard library -# -# Usage: -# - In your script, set the CONFIG_FILE variable. For rxample: -# CONFIG_FILE=settings.ini. -# If it is unset, it will default to 'config.ini'. -# - Use any of the functions below to access the config file. -# --- - -# Create a new config file. -# There is normally no need to use this function, it is used by other -# functions as needed. +## Config functions [@bashly-upgrade config] +## This file is a part of Bashly standard library +## +## Usage: +## - In your script, set the CONFIG_FILE variable. For rxample: +## CONFIG_FILE=settings.ini. +## If it is unset, it will default to 'config.ini'. +## - Use any of the functions below to access the config file. +## +## Create a new config file. +## There is normally no need to use this function, it is used by other +## functions as needed. +## config_init() { CONFIG_FILE=${CONFIG_FILE:=config.ini} [[ -f "$CONFIG_FILE" ]] || touch "$CONFIG_FILE" } -# Get a value from the config. -# Usage: result=$(config_get hello) +## Get a value from the config. +## Usage: result=$(config_get hello) config_get() { local key=$1 local regex="^$key *= *(.+)$" @@ -36,8 +35,8 @@ config_get() { echo "$value" } -# Add or update a key=value pair in the config. -# Usage: config_set key value +## Add or update a key=value pair in the config. +## Usage: config_set key value config_set() { local key=$1 shift @@ -68,8 +67,8 @@ config_set() { printf "%b\n" "$output" > "$CONFIG_FILE" } -# Delete a key from the config. -# Usage: config_del key +## Delete a key from the config. +## Usage: config_del key config_del() { local key=$1 @@ -87,19 +86,19 @@ config_del() { printf "%b\n" "$output" > "$CONFIG_FILE" } -# Show the config file +## Show the config file config_show() { config_init cat "$CONFIG_FILE" } -# Return an array of the keys in the config file. -# Usage: -# -# for k in $(config_keys); do -# echo "- $k = $(config_get "$k")"; -# done -# +## Return an array of the keys in the config file. +## Usage: +## +## for k in $(config_keys); do +## echo "- $k = $(config_get "$k")"; +## done +## config_keys() { local regex="^([a-zA-Z0-9_\-\/\.]+) *=" @@ -117,13 +116,13 @@ config_keys() { echo "${keys[@]}" } -# Returns true if the specified key exists in the config file. -# Usage: -# -# if config_has_key "key" ; then -# echo "key exists" -# fi -# +## Returns true if the specified key exists in the config file. +## Usage: +## +## if config_has_key "key" ; then +## echo "key exists" +## fi +## config_has_key() { [[ $(config_get "$1") ]] } diff --git a/lib/bashly/templates/lib/sample_function.sh b/lib/bashly/templates/lib/sample_function.sh index b975d7a2..bbe5656f 100644 --- a/lib/bashly/templates/lib/sample_function.sh +++ b/lib/bashly/templates/lib/sample_function.sh @@ -1,13 +1,13 @@ -# Add any function here that is needed in more than one parts of your -# application, or that you otherwise wish to extract from the main function -# scripts. -# -# Note that code here should be wrapped inside bash functions, and it is -# recommended to have a separate file for each function. -# -# Subdirectories will also be scanned for *.sh, so you have no reason not -# to organize your code neatly. -# +## Add any function here that is needed in more than one parts of your +## application, or that you otherwise wish to extract from the main function +## scripts. +## +## Note that code here should be wrapped inside bash functions, and it is +## recommended to have a separate file for each function. +## +## Subdirectories will also be scanned for *.sh, so you have no reason not +## to organize your code neatly. +## sample_function() { echo "it works" } diff --git a/lib/bashly/templates/lib/validations/validate_dir_exists.sh b/lib/bashly/templates/lib/validations/validate_dir_exists.sh index 9602b9e1..76b3d6db 100644 --- a/lib/bashly/templates/lib/validations/validate_dir_exists.sh +++ b/lib/bashly/templates/lib/validations/validate_dir_exists.sh @@ -1,4 +1,4 @@ -# [@bashly-upgrade validations] +## [@bashly-upgrade validations] validate_dir_exists() { [[ -d "$1" ]] || echo "must be an existing directory" } diff --git a/lib/bashly/templates/lib/validations/validate_file_exists.sh b/lib/bashly/templates/lib/validations/validate_file_exists.sh index b4024852..2143687d 100644 --- a/lib/bashly/templates/lib/validations/validate_file_exists.sh +++ b/lib/bashly/templates/lib/validations/validate_file_exists.sh @@ -1,4 +1,4 @@ -# [@bashly-upgrade validations] +## [@bashly-upgrade validations] validate_file_exists() { [[ -f "$1" ]] || echo "must be an existing file" } diff --git a/lib/bashly/templates/lib/validations/validate_integer.sh b/lib/bashly/templates/lib/validations/validate_integer.sh index da99c9fc..021168be 100644 --- a/lib/bashly/templates/lib/validations/validate_integer.sh +++ b/lib/bashly/templates/lib/validations/validate_integer.sh @@ -1,4 +1,4 @@ -# [@bashly-upgrade validations] +## [@bashly-upgrade validations] validate_integer() { [[ "$1" =~ ^[0-9]+$ ]] || echo "must be an integer" } \ No newline at end of file diff --git a/lib/bashly/templates/lib/validations/validate_not_empty.sh b/lib/bashly/templates/lib/validations/validate_not_empty.sh index d81c16c9..9578dab8 100644 --- a/lib/bashly/templates/lib/validations/validate_not_empty.sh +++ b/lib/bashly/templates/lib/validations/validate_not_empty.sh @@ -1,4 +1,4 @@ -# [@bashly-upgrade validations] +## [@bashly-upgrade validations] validate_not_empty() { [[ -z "$1" ]] && echo "must not be empty" } diff --git a/lib/bashly/templates/lib/yaml.sh b/lib/bashly/templates/lib/yaml.sh index 85854820..97fd2ee7 100644 --- a/lib/bashly/templates/lib/yaml.sh +++ b/lib/bashly/templates/lib/yaml.sh @@ -1,18 +1,15 @@ -# --- -# YAML parser [@bashly-upgrade yaml] -# This file is a part of Bashly standard library -# Does not support arrays, only hashes -# -# Source: https://stackoverflow.com/a/21189044/413924 -# -# Usage: -# -# yaml_load "settings.yml" # print variables -# yaml_load "settings.yml" "config_" # use prefix -# eval $(yaml_load "settings.yml") # create variables in scope -# -# --- - +## YAML parser [@bashly-upgrade yaml] +## This file is a part of Bashly standard library +## Does not support arrays, only hashes +## +## Source: https://stackoverflow.com/a/21189044/413924 +## +## Usage: +## +## yaml_load "settings.yml" # print variables +## yaml_load "settings.yml" "config_" # use prefix +## eval $(yaml_load "settings.yml") # create variables in scope +## yaml_load() { local prefix=$2 local s='[[:space:]]*' w='[a-zA-Z0-9_]*' diff --git a/spec/approvals/library/base/dir b/spec/approvals/library/base/dir index 6cdcf97c..55ef31ed 100644 --- a/spec/approvals/library/base/dir +++ b/spec/approvals/library/base/dir @@ -1,25 +1,25 @@ --- - :path: spec/tmp/src/lib/validations/validate_dir_exists.sh :content: | - # [@bashly-upgrade validations] + ## [@bashly-upgrade validations] validate_dir_exists() { [[ -d "$1" ]] || echo "must be an existing directory" } - :path: spec/tmp/src/lib/validations/validate_file_exists.sh :content: | - # [@bashly-upgrade validations] + ## [@bashly-upgrade validations] validate_file_exists() { [[ -f "$1" ]] || echo "must be an existing file" } - :path: spec/tmp/src/lib/validations/validate_integer.sh :content: |- - # [@bashly-upgrade validations] + ## [@bashly-upgrade validations] validate_integer() { [[ "$1" =~ ^[0-9]+$ ]] || echo "must be an integer" } - :path: spec/tmp/src/lib/validations/validate_not_empty.sh :content: | - # [@bashly-upgrade validations] + ## [@bashly-upgrade validations] validate_not_empty() { [[ -z "$1" ]] && echo "must not be empty" } diff --git a/spec/bashly/extensions/string_spec.rb b/spec/bashly/extensions/string_spec.rb index c28829f5..1713b0d4 100644 --- a/spec/bashly/extensions/string_spec.rb +++ b/spec/bashly/extensions/string_spec.rb @@ -56,10 +56,20 @@ end describe '#lint' do - subject { "one\n two\n \n three\n \n \nfour\n\n\n\n" } + context "with a string that contains multiple consecutive newlines" do + subject { "one\n two\n \n three\n \n \nfour\n\n\n\n" } - it "replaces two or more newlines with two newlines" do - expect(subject.lint).to eq "one\n two\n\n three\n\nfour\n\n" + it "replaces two or more newlines with two newlines" do + expect(subject.lint).to eq "one\n two\n\n three\n\nfour\n\n" + end + end + + context "with a string that contains double-hash comments" do + subject { "this is important\n## SECRET\n ## ANOTHER SECRET\n also important\n" } + + it "removes these comments" do + expect(subject.lint).to eq "this is important\n also important\n" + end end end end From fb11eba42cde4550c79c8efece4f2301dc4224aa Mon Sep 17 00:00:00 2001 From: Danny Ben Shitrit Date: Fri, 29 Oct 2021 15:44:50 +0000 Subject: [PATCH 2/2] regenerate example --- examples/colors/src/lib/colors.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/colors/src/lib/colors.sh b/examples/colors/src/lib/colors.sh index 960a4f82..25bc07e9 100644 --- a/examples/colors/src/lib/colors.sh +++ b/examples/colors/src/lib/colors.sh @@ -9,7 +9,7 @@ ## ## Color output will be disabled if `NO_COLOR` environment variable is set ## in compliance with https://no-color.org/ - +## print_in_color() { local color="$1" shift