Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions examples/colors/src/lib/colors.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# [@bashly-upgrade validations]
## [@bashly-upgrade validations]
validate_dir_exists() {
[[ -d "$1" ]] || echo "must be an existing directory"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# [@bashly-upgrade validations]
## [@bashly-upgrade validations]
validate_file_exists() {
[[ -f "$1" ]] || echo "must be an existing file"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# [@bashly-upgrade validations]
## [@bashly-upgrade validations]
validate_integer() {
[[ "$1" =~ ^[0-9]+$ ]] || echo "must be an integer"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# [@bashly-upgrade validations]
## [@bashly-upgrade validations]
validate_not_empty() {
[[ -z "$1" ]] && echo "must not be empty"
}
2 changes: 1 addition & 1 deletion lib/bashly/extensions/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 12 additions & 15 deletions lib/bashly/templates/lib/colors.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down
69 changes: 34 additions & 35 deletions lib/bashly/templates/lib/config.sh
Original file line number Diff line number Diff line change
@@ -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 *= *(.+)$"
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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_\-\/\.]+) *="

Expand All @@ -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") ]]
}
20 changes: 10 additions & 10 deletions lib/bashly/templates/lib/sample_function.sh
Original file line number Diff line number Diff line change
@@ -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"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# [@bashly-upgrade validations]
## [@bashly-upgrade validations]
validate_dir_exists() {
[[ -d "$1" ]] || echo "must be an existing directory"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# [@bashly-upgrade validations]
## [@bashly-upgrade validations]
validate_file_exists() {
[[ -f "$1" ]] || echo "must be an existing file"
}
2 changes: 1 addition & 1 deletion lib/bashly/templates/lib/validations/validate_integer.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# [@bashly-upgrade validations]
## [@bashly-upgrade validations]
validate_integer() {
[[ "$1" =~ ^[0-9]+$ ]] || echo "must be an integer"
}
2 changes: 1 addition & 1 deletion lib/bashly/templates/lib/validations/validate_not_empty.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# [@bashly-upgrade validations]
## [@bashly-upgrade validations]
validate_not_empty() {
[[ -z "$1" ]] && echo "must not be empty"
}
27 changes: 12 additions & 15 deletions lib/bashly/templates/lib/yaml.sh
Original file line number Diff line number Diff line change
@@ -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_]*'
Expand Down
8 changes: 4 additions & 4 deletions spec/approvals/library/base/dir
Original file line number Diff line number Diff line change
@@ -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"
}
16 changes: 13 additions & 3 deletions spec/bashly/extensions/string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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