Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Feat: show(). It scans all functions avail in bashlava #12

Merged
merged 9 commits into from
May 6, 2022
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
323 changes: 206 additions & 117 deletions bashlava.sh

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions bashlava_config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# wip brainstrom


export APP_NAME="app_no_name"
export APP_VERSION="0.1.0"


export USING_PRIVATE_SCRIPTS="false"

export USER_IS="${USER}"
export LOG_LINE_NUMBERS="8"

export DEFAULT_BRANCH="main"
export DEV_BRANCH="edge_${USER_IS}"

90 changes: 21 additions & 69 deletions components/alias.sh
Original file line number Diff line number Diff line change
@@ -1,72 +1,24 @@
#!/usr/bin/env bash

function m {
mainbranch
}
function e {
edge
}
function c {
commit
}
m() { "mainbranch" ; }
e() { "edge" ; }
c() { "commit" ; }
h() { "help" ; }
1() { "help" ; }
te() { "test" ; }
tt() { "test" ; }
2() { "test" ; }
v() { "version" ; }
t() { "tag" ; }
r() { "release" ; }
om() { "git checkout ${default_branch}" ; }
oe() { "git checkout edge" ; }
l() { "log" ; }
sq() { "squash" ; }
s() { "show" ; }
hash() { "git rev-parse HEAD && git rev-parse --short HEAD" ; }
Print_Yellow() { "Print_Warning" ; }

function h {
help
}
function 1 {
help
}
function te {
test
}
function tt {
test
}
function 2 {
test
}

### not shortcuts: pr / ci / mrg

function v {
version
}
function t {
tag
}
function r {
release
}
function om {
git checkout ${default_branch}
}
function oe {
git checkout edge
}
function l {
log
}
function sq {
squash
}

function s {
show
}

function hash {
git rev-parse HEAD && git rev-parse --short HEAD
}
function Print_Yellow {
Print_Warning
}
### capture common typos
function sh {
App_invalid_cmd
}
function oo {
App_invalid_cmd
}
function App_invalid_cmd {
export my_message="Invalid command" && Print_Warning_Stop
}
Utility_Invalid_Cmd() { "Utility_Invalid_Cmd" ; }
sh() { "Utility_Invalid_Cmd" ; }
00() { "Utility_Invalid_Cmd" ; }
87 changes: 68 additions & 19 deletions components/example.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,66 @@
#!/usr/bin/env bash

function str_not_eq {

function ex_11 () {
ls_dir_file=$(ls)
echo "${ls_dir_file}"

echo
# you can read last command return value via $?
echo "$?"

if [[ $?! = 0 ]]; then
echo "command failed"
fi
if [[ $?==0 ]]; then
echo command succeed
fi
}

function ex_12 () {
#pdf: page 20

POSITIONAL=()
while (( $# > 0 )); do
case "${1}" in
-f|--flag)
echo flag: "${1}"
shift # shift once since flags have no values
;;
-s|--switch)
numOfArgs=1 # number of switch arguments
if (( $# < numOfArgs + 1 )); then
shift $#
else
echo "switch: ${1} with value: ${2}"
shift $((numOfArgs + 1)) # shift 'numOfArgs + 1' to bypass switch and its value
fi
;;
*) # unknown flag/switch
POSITIONAL+=("${1}")
shift
;;
esac
done

set -- "${POSITIONAL[@]}" # restore positional params

}

function ex_str_not_eq {
# Get this extension: https://marketplace.visualstudio.com/items?itemName=Remisa.shellman
# Read her book: https://github.com/yousefvand/shellman-ebook
if [[ "${string1}" != "${string2}" ]]; then
echo "The two strings are different"
fi
}

function ex_random_11 {
openssl rand -hex 3
}

# Refer to 'Condition_File_Must_Be_Present' instead of copy paste this fct
function idempotent_file_exist {
function ex_11_idempotent_file_exist {
_file_is="somefile.sh"
if [[ -f "${_path_components}/${_file_is}" ]]; then
echo "idempotent checkpoint passed" > /dev/null 2>&1
Expand All @@ -23,7 +75,7 @@ function idempotent_file_exist {
}

# Now we use 'Condition_Vars_Must_Be_Not_Empty' instead of copy paste this fct
function idempotent_empty_var {
function ex_11_idempotent_empty_var {
if [[ -n "${run_id}" ]]; then #if not empty
echo "idempotent checkpoint passed" > /dev/null 2>&1
my_message="SOME_MESSAGE_HERE" && Print_Blue
Expand All @@ -36,8 +88,7 @@ function idempotent_empty_var {
fi
}

# Now we use 'App_Does_Var_Notset' instead of copy paste this fct
function idempotent_compare_var {
function ex_idempotent_compare_var {
if [[ "${input_2}" != "not_set" ]]; then
echo "idempotent checkpoint passed" > /dev/null 2>&1
my_message="SOME_MESSAGE_HERE" && Print_Blue
Expand All @@ -51,7 +102,7 @@ function idempotent_compare_var {
}

# Example 1: Output a Description for Each Option
function case_a {
function ex_case_a {
echo && echo "Which color do you like best?"
my_message="1 - Blue" && Print_Blue
echo "2 - Red"
Expand All @@ -70,7 +121,7 @@ function case_a {
}

# Example 2: Using Multiple Patterns
function case_b {
function ex_case_b {
shopt -s nocasematch
echo "Enter the name of a month."
read -r month
Expand All @@ -87,7 +138,7 @@ function case_b {
}

# Example 3: for Loops
function case_c {
function ex_case_c {

for f in *.wav
do
Expand All @@ -97,7 +148,7 @@ function case_c {
}

#Example 4: Create an Address Book
function case_d {
function ex_case_d {
echo "Choose a contact to display information:"
echo "[C]hris Ramsey"
echo "[J]ames Gardner"
Expand All @@ -122,40 +173,38 @@ function case_d {
}

# Read: Asking input from the User
function case_ex1 {
function ex_read {
echo "What is your name?"
read -r name
echo "Your name is ${name}!"
}

# Mapfile: Assigning a variable the values of a file's lines
function case_ex2 {
function ex_mapfile {
mapfile -t file_var < ${_path_components}/list.txt

for i in "${file_var[@]}"; do
echo "${i}"
done
}

# Setting the value when a variable isn't set
function case_ex3 {
function ex_case_21 {
echo "What is your name?"
read -r name
echo "Your name is ${name}!"
}

# Mapfile: Assigning a variable the values of a file's lines
function case_ex4 {
function ex_case_22 {
echo "Hello ${name:-nobody}!"
}

function var_as_file {
function ex_case_23 {
# sometime it's useful to have a variable as a file
_my_var=(Yes No Maybe)
cat <(echo "${_my_var[@]}")
}

function rlwrap_example {
function ex_rlwrap_example {
# https://unix.stackexchange.com/questions/278631/bash-script-auto-complete-for-user-input-based-on-array-data#278666
# works but it's not clean 2022-04-28_20h26

Expand All @@ -166,13 +215,13 @@ function rlwrap_example {
echo "reply='${reply}'"
}

function lint {
function ex_lint {
docker run -it --rm \
-v "$(pwd)"/Dockerfile:/Dockerfile:ro \
redcoolbeans/dockerlint
}

function demo_set_var_if_empty {
function ex_demo_set_var_if_empty {
# Setting variable value (if unset)
echo ${country:-Greenland}
echo ${country:=Greenland}
Expand Down
7 changes: 4 additions & 3 deletions components/list.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
one
two
three
fct: ex_mapfile:
one
two
three
33 changes: 33 additions & 0 deletions components/show_fct_category_filter.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env bash

# As these fct scan the bashlava.sh file, it was seaching for itself causing display issues.
# That why we run these fct under this file.

function Show_Fct_Category_Filter {
my_message="${_fct_name}" && Print_Blue && echo
my_message="$(cat ${_file_path} | grep "${_filter}" | awk '{print $2}')" && Print_Gray && echo
}

# These pass attributs to 'Show_Fct_Category_Filter'
Show_Fct_Category_Condition() { _fct_name="2) Condition" _filter="function Condition_" _file_path="${_path_bashlava}/bashlava.sh" && Show_Fct_Category_Filter ; }
Show_Fct_Category_Core() { _fct_name="3) Core" _filter="function Core_" _file_path="${_path_bashlava}/bashlava.sh" && Show_Fct_Category_Filter ; }
Show_Fct_Category_Example() { _fct_name="4) Example" _filter="function ex_" _file_path="${_path_components}/example.sh" && Show_Fct_Category_Filter ; }
Show_Fct_Category_Print() { _fct_name="5) Print" _filter="function Print_" _file_path="${_path_bashlava}/bashlava.sh" && Show_Fct_Category_Filter ; }
Show_Fct_Category_Show() { _fct_name="6) Show" _filter="function Show_" _file_path="${_path_bashlava}/bashlava.sh" && Show_Fct_Category_Filter ; }
Show_Fct_Category_User() { _fct_name="7) User" _filter="# User_" _file_path="${_path_bashlava}/bashlava.sh" && Show_Fct_Category_Filter ; }
Show_Fct_Category_Utility() { _fct_name="8) Utility" _filter="function Utility_" _file_path="${_path_components}/utilities.sh" && Show_Fct_Category_Filter ; }
Show_Fct_Category_F1() { my_message="Function f1 does not exist yet." && Print_Gray && echo ; }
Show_Fct_Category_F2() { my_message="Function f2 does not exist yet." && Print_Gray && echo ; }

function Show_Fct_Category_All {
arr=( "Alias" "Condition" "Core" "Example" "Print" "Show" "User" "Utility" )
for action in "${arr[@]}"; do
Show_Fct_Category_"${action}" && sleep 0.2
done
}

# Function in alias are very different so I can't reuse 'Show_Fct_Category_Filter'
function Show_Fct_Category_Alias {
my_message="1) Alias" && Print_Blue && echo
my_message="$(cat ${_path_components}/alias.sh | grep "()" | awk '{print $1}' | sed 's/()//g')" && Print_Gray && echo
}
Loading