Skip to content

Commit

Permalink
sync local changes to sash
Browse files Browse the repository at this point in the history
* greatly speed up sash-parse
* split out sash-trap from sash-err-stack
* add tests
* add sash-parse level CLI.
  • Loading branch information
Cynthia Coan committed Aug 3, 2019
1 parent 9314441 commit b607d6e
Show file tree
Hide file tree
Showing 29 changed files with 596 additions and 201 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## Unreleased

* Split out trap functionality to: `sash-trap`
* Add Tests for `sash-err-stack`
* Add Tests for `sash-parse`
* Add Tests for `sash-trap`
* Speed up `sash-parse` by multiple times.
* Create: `sash <subcommand>` commands.
* Basic Documentation for: `sash package` in README.

## 1.2.0 - (February 23rd, 2019)

* Don't source `sash-parse.sh` before plugins load as plugins can overwrite that.
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ I want to make contributing to this project as easy and transparent as
possible.

## Pull Requests ##

I actively welcome your pull requests.

1. Fork the repo and create your branch from `master`.
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License

Copyright (c) 2018 Security Insanity
Copyright (c) 2019 Security Insanity

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ categories if you didn't manually create them first.

## Usage ##

### sash_add ###
### sash add ###

Whenever you need to add something to your modularized bashrc instead of manually
`mkdir && vim`'ing a file reach for `sash_add`:

```bash
ecoan@kappa:~$ sash_add
ecoan@kappa:~$ sash add
Please Choose a Category:
1) /home/CORP.INSTRUCTURE.COM/ecoan/.bash/plugins/utilities
2) /home/CORP.INSTRUCTURE.COM/ecoan/.bash/plugins/work
Expand All @@ -70,15 +70,15 @@ to type the content you want to add the bashrc, and one for you to type comments
(without the annoying '#' at the beginning of the line for long comments) so you can
know what it is when coming back to it later.

### sash_show ###
### sash show ###

If you're like me you've probably created a lot of individual files under a specific subcategory.
To the point where you have so many files you don't want to manually cat them all out. Enter sash_show.
Sash show allows you to get a materialized view of an entire sub category so that way you know exactly what
content is in it.

```bash
ecoan@kappa:~$ sash_show
ecoan@kappa:~$ sash show
Please Choose a Category:
1) /home/CORP.INSTRUCTURE.COM/ecoan/.bash/plugins/utilities
2) /home/CORP.INSTRUCTURE.COM/ecoan/.bash/plugins/work
Expand All @@ -96,7 +96,7 @@ Please Choose a SubCategory:
# adding some content to sash with multiple lines
# for testing
export SASH_TEST=1
ecoan@kappa:~$ sash_show utilities/test
ecoan@kappa:~$ sash show utilities test

###############################################################
# Content from: /home/CORP.INSTRUCTURE.COM/ecoan/.bash/plugins/utilities/test//test.sh
Expand All @@ -108,6 +108,14 @@ ecoan@kappa:~$ sash_show utilities/test
export SASH_TEST=1
```

### sash package ###

sash package is a tool for distributing parts (a category or subcategory) to
other users. Not only that it signs the package using keybase for you so people
can validate that it came from you.

Simply run: `sash package`.

### sash_trace ###

If you aren't able to find out where a particular command is executing (or you want to see which
Expand Down
47 changes: 47 additions & 0 deletions sash-command-handler.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env bash

source "$SASH_DIR/subcommands/sash-add.sh"
source "$SASH_DIR/subcommands/sash-package.sh"
source "$SASH_DIR/subcommands/sash-show.sh"

SASH_ARGS=("h|help")

sash() {
__sash_parse_args "$*" "${SASH_ARGS[@]}" || {
printf 'Failure to parse arguments!\nRemember argument flags should not be between sash and subcommand.'
return 10
}

local split_stdin=($(__sash_split_str "${__sash_parse_results[__STDIN]}" " "))

if [[ "${split_stdin[0]}" == "add" ]]; then
sash:add
return $?
fi
if [[ "${split_stdin[0]}" == "show" ]]; then
sash:show "${split_stdin[@]:1}"
return $?
fi
if [[ "${split_stdin[0]}" == "package" ]]; then
sash:package "${split_stdin[@]:1}"
return $?
fi

echo "
Welcome to S.A.S.H.!
S.A.S.H. offers the following subcommands:
* \`add\` - add something to your bashrc
(and automatically source it).
* \`show (category) (sub-category)\` - show the contents of a paritcular
subcategory.
* \`package\` - package up a particular category
or subcategory for distribution to
others.
NOTE: anything between: \`()\` above denotes optional arguments.
If you don't provide them, and they're needed, you will be asked inline.
"
return 0
}
13 changes: 12 additions & 1 deletion sash-libs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ problems for other scripts around you. However, you still want the benefits
`set -e` provides in order to ensure your script doesn't keep running. This
is where `sash-err-stack` fits in.

Dependencies: None
Dependencies:
* Sash-Trap

## Sash-Parse ##

Expand All @@ -36,3 +37,13 @@ one you might expect inside of a full blown language (such as Go, Rust, etc.)

Dependencies:
* Sash-Err-Stack
* Sash-Trap (dependency of Sash-Err-Stack)

## Sash-Trap ##

`sash-trap` is a library built for "safely-handling" traps. More specifically
built to get around the fact that the `trap` command by default, stomps all
over a command that was previously in trap. Making it impossible, to actually
recursively set traps without everyone knowing how to properly add to a trap.

Dependencies: None
6 changes: 5 additions & 1 deletion sash-libs/sash-err-stack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@ __sash_guard_errors()
That's it! Simply call each one of these functions whenever you want to either
allow errors, or disallow errors for the runtime of your function.

It should be noted: We check for the error mode being correct at the return of
every function. So even if you call a function that messes with the global error
mode, we will set it back before control is returned to your function.

### Notes ###

Sash-Err-Stack depends very, very heavily on the presence of the RETURN trap being
fired. However, by default trap overwrites the entire trap. Meaning if someone else
clears the return trap we're swell out of luck.

If this happens Undefined Behavior will happen, so you should always ensure traps
are properly set, and unset.
are properly set, and unset. To help with this, I recommend looking at `sash-trap`.
117 changes: 53 additions & 64 deletions sash-libs/sash-err-stack/sash-err-stack.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,50 +10,36 @@
# S.A.S.H. is the main way to add things to your ~/.bashrc and still
# maintain structure.

declare -f __sash_pop_err_mode_stack >/dev/null
__sash_intermediate_check="$?"
_sash_initial_err_mode=""

if [[ "$__sash_intermediate_check" == "1" ]]; then
if [[ "$-" =~ "e" ]]; then
_sash_initial_err_mode="0"
else
_sash_initial_err_mode="1"
fi

# _sash_get_trapped_text(signal: String)
#
# Modifies Variables: None
#
# parses trap output to get you the command for a signal.
_sash_get_trapped_text() {
local signal="$1"
echo "$(trap -p "$signal" | sed "s/trap -- '//g; s/' $signal$//g; s/\\\''//g")"
}
set +e

# _sash_safe_add_to_trap(signal: String, command: String)
#
# Modifies Variables: None
#
# safely adds a command to a trap.
_sash_safe_add_to_trap() {
local command_to_add="$1"
local signal="$2"
declare -f _sash_safe_add_to_trap >/dev/null
__sash_trap_intermediate_check="$?"

if [[ "x$(trap -p "$signal")" == "x" ]]; then
trap "$command_to_add" "$signal"
if [[ "$__sash_trap_intermediate_check" == "1" ]]; then
if [[ "x$SASH_TRAP_DIR" == "x" ]]; then
source "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/../sash-trap/sash-trap.sh"
else
local trapped_text="$(_sash_get_trapped_text "$signal")"
echo "$trapped_text" | grep "^.*;" > /dev/null 2>&1
if [[ "$?" == "0" ]]; then
trap "$trapped_text $command_to_add;" "$signal"
else
trap "$trapped_text; $command_to_add;" "$signal"
fi
source "$SASH_TRAP_DIR/sash-trap.sh"
fi
}
fi

unset __sash_trap_intermediate_check


declare -f __sash_pop_err_mode_stack >/dev/null
__sash_intermediate_check="$?"

if [[ "$__sash_intermediate_check" == "1" ]]; then

_sash_global_err_mode_stack=()
_sash_initial_err_mode=""
if [[ "$-" =~ "e" ]]; then
_initial_err_mode="0"
else
_initial_err_mode="1"
fi

# __sash_reset_initial_stack()
#
Expand All @@ -79,10 +65,9 @@ __sash_reset_initial_stack() {
#
# Pushes onto the error stack.
__sash_push_err_mode_stack() {
local func_name="$1"
local err_mode="$2"
_sash_global_err_mode_stack=("${_sash_global_err_mode_stack[@]}" "$func_name|$err_mode")
if [[ "$err_mode" == "1" ]]; then
_sash_global_err_mode_stack=("${_sash_global_err_mode_stack[@]}" "$1|$2")

if [[ "$2" == "1" ]]; then
set -e
else
set +e
Expand All @@ -99,34 +84,39 @@ __sash_push_err_mode_stack() {
# Should be called when a function exits. Checks to see
# if the function exiting had an artificial error_state, and unsets it.
__sash_pop_err_mode_stack() {
local func_name="${FUNCNAME[1]}"
local new_arr=()
local func_iter=
for func_iter in "${_sash_global_err_mode_stack[@]}"; do
local func_iter_inner_arr=(${func_iter//|/\ })
if [[ "${func_iter_inner_arr[0]}" == "$func_name" ]]; then
local was_enabled_err="${func_iter_inner_arr[1]}"
if [[ "$was_enabled_err" == "1" ]]; then
set +e
else
set -e
fi
local readonly func_name="${FUNCNAME[1]}"

local readonly stack_size="${#_sash_global_err_mode_stack[@]}"
local readonly stack_le_index=$(( stack_size - 1 ))
if [[ "$stack_size" == "0" ]]; then
return 0
fi

local readonly stack_last_element="${_sash_global_err_mode_stack[$stack_le_index]}"
local readonly stack_split_name=(${stack_last_element//|/\ })
if [[ "${stack_split_name[0]}" == "$func_name" ]]; then
if [[ "$stack_size" == "1" ]]; then
__sash_reset_initial_stack
_sash_global_err_mode_stack=()
return 0
fi

if [[ "${stack_split_name[1]}" == "1" ]]; then
set +e
else
new_arr=("${new_arr[@]}" "$func_iter")
set -e
fi
done
_sash_global_err_mode_stack=("${new_arr[@]}")
if [[ "${#_sash_global_err_mode_stack[@]}" == "0" ]]; then
__sash_reset_initial_stack

_sash_global_err_mode_stack=(${_sash_global_err_mode_stack[@]:0:$stack_le_index})
else
local last_elem="${_sash_global_err_mode_stack[-1]}"
local inner_split=(${last_elem//|/\ })
if [[ "${inner_split[1]}" == "1" ]]; then
if [[ "${stack_split_name[1]}" == "1" ]]; then
set -e
else
set +e
fi
fi

return 0
}

set -o functrace
Expand All @@ -139,8 +129,7 @@ _sash_safe_add_to_trap "__sash_pop_err_mode_stack" "RETURN"
#
# sets the error mode to be off for the runtime of this function.
__sash_allow_errors() {
local func_name="${FUNCNAME[1]}"
__sash_push_err_mode_stack "$func_name" "0"
__sash_push_err_mode_stack "${FUNCNAME[1]}" "0"
}

# __sash_guard_errors()
Expand All @@ -150,9 +139,9 @@ __sash_allow_errors() {
#
# sets the error mode to be on for the runtime of this function.
__sash_guard_errors() {
local func_name="${FUNCNAME[1]}"
__sash_push_err_mode_stack "$func_name" "1"
__sash_push_err_mode_stack "${FUNCNAME[1]}" "1"
}
fi

unset __sash_intermediate_check
__sash_reset_initial_stack
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

set -e

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$SCRIPT_DIR/../../sash-err-stack.sh"

false
echo "hey"
19 changes: 19 additions & 0 deletions sash-libs/sash-err-stack/tests/test-cases/test-err-mode-mixup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$SCRIPT_DIR/../../sash-err-stack.sh"

function messWithGlobalErrorMode() {
set +e
false
echo "hey"
}

function guard() {
__sash_guard_errors
messWithGlobalErrorMode
false
echo "hello world"
}

guard
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$SCRIPT_DIR/../../sash-err-stack.sh"
source "$SCRIPT_DIR/../../sash-err-stack.sh"
Loading

0 comments on commit b607d6e

Please sign in to comment.