Skip to content

Commit

Permalink
add in sash-package command
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric committed Nov 5, 2018
1 parent 188e761 commit b6543a8
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* Source sash commands after `post` section, so functions can't be overwritten by plugins.
* Add `sash-parse.sh` which allows for parsing arguments without using `getopt` which if different
for bsd/gnu.
* Add in first iteration of: `sash-package.sh` which allows you to package up a particular category,
or subcategory for distribution to others. Includes checking for secrets.

## 1.1.0 (October 15th, 2018)

Expand Down
4 changes: 2 additions & 2 deletions sash-add.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

# Implements the sash_add commands.
# Implements the sash_add command.
#
# S.A.S.H. is the main way to add things to your ~/.bashrc and still
# maintain structure.
Expand Down Expand Up @@ -46,7 +46,7 @@ sash_add() {
local subcategory="$(_sash_create_or_choose_subcategory $category)"
subcategory="${subcategory#./}"
fi
if ! _sash_get_multiline_input "# Please insert what you want to add to your bashrc below:\n"; then
if ! _sash_get_multiline_input "# Please insert what you want to add to your bashrc below:" ""; then
exit 1
fi
local content_to_add="$sash_multiline_content"
Expand Down
173 changes: 173 additions & 0 deletions sash-package.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
#!/usr/bin/env bash

# Implements the sash_package command.
#
# S.A.S.H. is the main way to add things to your ~/.bashrc and still
# maintain structure.

# _sash_package_subcategory(full_category: String, checks: (0 || 1)) -> int
#
# full_category:
# * the full path to the category to package.
# checks:
# * Whether or not to run checks for secrets.
_sash_package_subcategory() {
local full_category_path="${1%/}"
local run_checks="$2"

if [[ ! -d "$full_category_path" ]]; then
echo "[-] Can't find category at: [ $full_category_path ]!" >&2
return 1
fi

local temp_dir=$(mktemp -d "${TMPDIR:-/tmp}/sash-package-subcategory.XXXXXXXXXX") || return 1

local filename=""
for filename in $full_category_path/*.sh; do
[[ -x $filename ]] || continue

if [[ "$run_checks" == "1" ]]; then
local options=("YES" "NO")
echo ""
echo "###############################################################"
echo "# Content from: $filename"
echo "###############################################################"
echo ""
cat $filename
echo ""
echo ""
echo "Would you like to filter this file to remove secrets?: "
local option="$(_sash_choose_from_options ${options[@]})"
if [[ "$option" == "YES" ]]; then
if ! _sash_get_multiline_input "$(cat $filename)"; then
echo "[-] Couldn't get content! Exiting"
return 2
fi
echo "$sash_multiline_content" > "$temp_dir/${filename##*/}"
else
cat "$filename" > "$temp_dir/${filename##*/}"
fi
else
cat "$filename" > "$temp_dir/${filename##*/}"
fi
done

local current_dir="$(pwd)"
(cd "$temp_dir" && tar cfJ "$current_dir/${full_category_path##*/}.tar.xz" .) || return 10
rm -r "$temp_dir"
return 0
}

# _sash_package_category(category: String, checks: (0 || 1)) -> int
#
# category:
# * the category to package.
# checks:
# * whether or not to run checks on files for secrets.
_sash_package_category() {
local full_category_path="${1%/}"
local run_checks="$2"

if [[ ! -d "$full_category_path" ]]; then
echo "[-] Can't find category at: [ $full_category_path ]!" >&2
return 1
fi

local temp_dir=$(mktemp -d "${TMPDIR:-/tmp}/sash-package-category.XXXXXXXXXX") || return 1
local subcategories=( $(find "$full_category_path" -maxdepth 1 -type d -printf '%P\n' | grep -v "^\.$" | grep -v "^\.\.$" | grep -v "^$") )

local subcategory=""
local filename=""
for subcategory in "${subcategories[@]}"; do
mkdir -p "$temp_dir/$subcategory"
for filename in $full_category_path/$subcategory/*.sh; do
[[ -x $filename ]] || continue

if [[ "$run_checks" == "1" ]]; then
local options=("YES" "NO")
echo ""
echo "###############################################################"
echo "# Content from: $filename"
echo "###############################################################"
echo ""
cat $filename
echo ""
echo ""
echo "Would you like to filter this file to remove secrets?: "
local option="$(_sash_choose_from_options ${options[@]})"
if [[ "$option" == "YES" ]]; then
if ! _sash_get_multiline_input "$(cat $filename)"; then
echo "[-] Couldn't get content! Exiting"
return 2
fi
echo "$sash_multiline_content" > "$temp_dir/$subcategory/${filename##*/}"
else
cat "$filename" > "$temp_dir/$subcategory/${filename##*/}"
fi
else
cat "$filename" > "$temp_dir/$subcategory/${filename##*/}"
fi
done
done

local current_dir="$(pwd)"
(cd "$temp_dir" && tar cfJ "$current_dir/${full_category_path##*/}.tar.xz" .) || return 10
rm -r "$temp_dir"
return 0
}

# sash_package(args: Array<String>) -> Int
#
# sash_package will package up a category, or sub category for you to distribute to your
# friends. Note: Sash package will give you an option to filter out content for each file
# incase of secrets, unless you specify: `--package-without-checks`.
sash_package() {
local arguments="${@}"
local flags=("package-without-checks" "c|category" "s|subcategory" "f|full-category")

if ! __sash_parse_args "$arguments" "${flags[@]}" ; then
echo "Failed to parse arguments for sash_package" >&2
return 1
fi

local category=""
local subcategory=""
local is_full_category=""
local run_checks="1"

if [[ "${__sash_parse_results[package-without-checks]}" == "0" ]]; then
run_checks="0"
fi
if [[ "x${__sash_parse_results[category]}" == "x" ]]; then
echo "[/] Please Choose a Category to Package: "
category="$(_sash_choose_a_directory "$HOME/.bash/plugins/")"
else
category="$HOME/.bash/plugins/${__sash_parse_results[category]}"
fi
if [[ ! -d "$category" ]]; then
echo -e "${white}[${red}-${white}]${restore} Category: [$1] doesn't exist!"
fi

# TODO: Implement Signing + Versioning.

if [[ "$category" == "$HOME/.bash/plugins/post" ]]; then
echo "[+] Packaing Post Section..."
_sash_package_subcategory "$category" "$run_checks"
return $?
fi
if [[ "${__sash_parse_results[full-category]}" == "0" ]]; then
echo "[+] Packaging Category: $category..."
_sash_package_category "$category" "$run_checks"
return $?
fi

if [[ "x${__sash_parse_results[subcategory]}" == "x" ]]; then
echo "[/] Please Choose a Subcategory to Package: "
subcategory="$(cd "$category" && _sash_choose_a_directory ".")"
else
subcategory="${__sash_parse_results[subcategory]}"
fi
echo "Packaging Subcategory: $category/$subcategory"
_sash_package_subcategory "$category/$subcategory" "$run_checks"
return $?
}
6 changes: 6 additions & 0 deletions sash-parse.sh
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,12 @@ __sash_parse_args() {
fi
done

if [[ "$state" == "1" ]]; then
__sash_parse_results["$value_to_write_to"]="0"
state=0
value_to_write_to=""
fi

__sash_parse_results["__STDIN"]="$str_buffer"
return 0
}
2 changes: 1 addition & 1 deletion sash-show.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ sash_show() {
if [[ -n "$1" ]]; then
local full_category="$HOME/.bash/plugins/$1"
if [[ ! -d $full_category ]]; then
echo -e "${white}[${red}-${white}]${restore} Category doesn't exist!"
echo -e "${white}[${red}-${white}]${restore} Category: [$1] doesn't exist!"
fi
if [[ -n "$2" ]]; then
_sash_materialize_view "$full_category" "$2"
Expand Down
8 changes: 5 additions & 3 deletions sash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ SASH_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# ```
_sash_get_multiline_input() {
local temp=$(mktemp "${TMPDIR:-/tmp}/sash-input.XXXXXXXXXX") || return 1
if [[ -n $1 ]]; then
echo "$1" >> "$temp"
fi
local arg
for arg in "$@"; do
echo "$arg" >> "$temp"
done
local ret_code
if "$EDITOR" -- "$temp" && [[ -s $temp ]]; then
sash_multiline_content=$(<"$temp")
Expand Down Expand Up @@ -198,5 +199,6 @@ fi

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

export SASH_RUNNING=1

0 comments on commit b6543a8

Please sign in to comment.