From 1d4098369fdb2e8abd77c1cd9d380c4fce12816b Mon Sep 17 00:00:00 2001 From: Ryan Caloras Date: Sun, 27 Mar 2016 21:48:42 -0400 Subject: [PATCH 01/10] Start work on 1.0.2-dev --- bashhub/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bashhub/version.py b/bashhub/version.py index 87e98e0..0dbb755 100644 --- a/bashhub/version.py +++ b/bashhub/version.py @@ -1 +1 @@ -__version__ = '1.0.1' +__version__ = '1.0.2-dev' From 2cab5bdfb0d9c377a9300255d275291447af4b76 Mon Sep 17 00:00:00 2001 From: Ryan Caloras Date: Mon, 18 Apr 2016 15:18:08 -0400 Subject: [PATCH 02/10] Fix if statements in rest_client.py --- bashhub/bh.py | 2 ++ bashhub/rest_client.py | 14 ++++++-------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/bashhub/bh.py b/bashhub/bh.py index ec0bc7c..99e70a7 100755 --- a/bashhub/bh.py +++ b/bashhub/bh.py @@ -8,6 +8,7 @@ import cli.app import os import io +import traceback from model import MinCommand from bashhub_globals import * @@ -78,6 +79,7 @@ def main(): try: bh.run() except Exception as e: + formatted = traceback.format_exc(e) print("Oops, look like an exception occured: " + str(e)) sys.exit(1) except KeyboardInterrupt: diff --git a/bashhub/rest_client.py b/bashhub/rest_client.py index 8bb4654..5eae5be 100644 --- a/bashhub/rest_client.py +++ b/bashhub/rest_client.py @@ -70,7 +70,7 @@ def login_user(login_form): print("Looks like there's a connection error. Please try again later") return None except HTTPError as error: - if response.status_code == 409 or 401: + if response.status_code in (409, 401): print(response.text) else: print(error) @@ -158,11 +158,10 @@ def patch_system(system_patch, mac): r.raise_for_status() return r.status_code except Exception as error: - if r.status_code == 403 or 401: + if r.status_code in (403, 401): print("Permissons Issue. Run bashhub setup to re-login.") return None - def search(limit=None, path=None, query=None, system_name=None, unique=None): payload = dict() @@ -190,12 +189,11 @@ def search(limit=None, path=None, query=None, system_name=None, unique=None): except ConnectionError as error: print "Sorry, looks like there's a connection error. Please try again later" except Exception as error: - if r.status_code == 403 or 401: + if r.status_code in (403, 401): print("Permissons Issue. Run bashhub setup to re-login.") + print("Sorry, an error occurred communicating with Bashhub. Response Code: " + str(r.status_code)) return [] - - def save_command(command): url = BH_URL + "/api/v1/command" @@ -205,7 +203,7 @@ def save_command(command): print "Sorry, looks like there's a connection error" pass except Exception as error: - if r.status_code == 403 or 401: + if r.status_code in (403, 401): print("Permissons Issue. Run bashhub setup to re-login.") @@ -219,7 +217,7 @@ def get_status_view(process_id, start_time): status_view_json = json.dumps(r.json()) return StatusView.from_JSON(status_view_json) except Exception as error: - if r.status_code == 403 or 401: + if r.status_code in (403, 401): print("Permissons Issue. Run bashhub setup to re-login.") else: print("Sorry, looks like there's a connection error: " + str(error)) From e995cfa786edae82a14ef0dc2df2ffdbc589de1a Mon Sep 17 00:00:00 2001 From: Ryan Caloras Date: Fri, 22 Apr 2016 08:58:14 -0400 Subject: [PATCH 03/10] Updated to bash-preexec 0.3.0 - Fixes #27 --- bashhub/shell/deps/bash-preexec.sh | 113 +++++++++++++++++++++-------- 1 file changed, 84 insertions(+), 29 deletions(-) diff --git a/bashhub/shell/deps/bash-preexec.sh b/bashhub/shell/deps/bash-preexec.sh index 9446797..e4ddbc6 100644 --- a/bashhub/shell/deps/bash-preexec.sh +++ b/bashhub/shell/deps/bash-preexec.sh @@ -5,13 +5,13 @@ # # # 'preexec' functions are executed before each interactive command is -# executed, with the interactive command as its argument. The 'precmd' +# executed, with the interactive command as its argument. The 'precmd' # function is executed before each prompt is displayed. # # Author: Ryan Caloras (ryan@bashhub.com) # Forked from Original Author: Glyph Lefkowitz # -# V0.2.3 +# V0.3.0 # # General Usage: @@ -40,6 +40,13 @@ if [[ "$__bp_imported" == "defined" ]]; then fi __bp_imported="defined" +# Should be available to each precmd and preexec +# functions, should they want it. +__bp_last_command_ret_value="$?" + +# Command to set our preexec trap. It's invoked once via +# PROMPT_COMMAND and then removed. +__bp_trap_install_string="trap '__bp_preexec_invoke_exec' DEBUG;" # Remove ignorespace and or replace ignoreboth from HISTCONTROL # so we can accurately invoke preexec with a command from our @@ -81,7 +88,7 @@ __bp_interactive_mode() { __bp_precmd_invoke_cmd() { # Should be available to each precmd function, should it want it. - local ret_value="$?" + __bp_last_ret_value="$?" # For every function defined in our function array. Invoke it. local precmd_function @@ -90,7 +97,7 @@ __bp_precmd_invoke_cmd() { # Only execute this function if it actually exists. # Test existence of functions with: declare -[Ff] if type -t "$precmd_function" 1>/dev/null; then - __bp_set_ret_value $ret_value + __bp_set_ret_value $__bp_last_ret_value $precmd_function fi done @@ -130,14 +137,19 @@ __bp_in_prompt_command() { # interactively, and invoke 'preexec' if so. __bp_preexec_invoke_exec() { - if [[ -n "$COMP_LINE" ]] - then - # We're in the middle of a completer. This obviously can't be + # Checks if the file descriptor is not standard out (i.e. '1') + # __bp_delay_install checks if we're in test. Needed for bats to run. + # Prevents preexec from being invoked for functions in PS1 + if [[ ! -t 1 && -z "$__bp_delay_install" ]]; then + return + fi + + if [[ -n "$COMP_LINE" ]]; then + # We're in the middle of a completer. This obviously can't be # an interactively issued command. return fi - if [[ -z "$__bp_preexec_interactive_mode" ]] - then + if [[ -z "$__bp_preexec_interactive_mode" ]]; then # We're doing something related to displaying the prompt. Let the # prompt set the title instead of me. return @@ -147,8 +159,7 @@ __bp_preexec_invoke_exec() { # In other words, if you have a subshell like # (sleep 1; sleep 2) # You want to see the 'sleep 2' as a set_command_title as well. - if [[ 0 -eq "$BASH_SUBSHELL" ]] - then + if [[ 0 -eq "$BASH_SUBSHELL" ]]; then __bp_preexec_interactive_mode="" fi fi @@ -156,7 +167,6 @@ __bp_preexec_invoke_exec() { if __bp_in_prompt_command "$BASH_COMMAND"; then # If we're executing something inside our prompt_command then we don't # want to call preexec. Bash prior to 3.1 can't detect this at all :/ - __bp_preexec_interactive_mode="" return fi @@ -180,19 +190,42 @@ __bp_preexec_invoke_exec() { # Only execute each function if it actually exists. # Test existence of function with: declare -[fF] if type -t "$preexec_function" 1>/dev/null; then + __bp_set_ret_value $__bp_last_ret_value $preexec_function "$this_command" fi done } -# Execute this to set up preexec and precmd execution. -__bp_preexec_and_precmd_install() { +# Returns PROMPT_COMMAND with a semicolon appended +# if it doesn't already have one. +__bp_prompt_command_with_semi_colon() { - # Make sure this is bash that's running this and return otherwise. - if [[ -z "$BASH_VERSION" ]]; then - return 1; + # Trim our existing PROMPT_COMMAND + local trimmed + trimmed=$(__bp_trim_whitespace "$PROMPT_COMMAND") + + # Take our existing prompt command and append a semicolon to it + # if it doesn't already have one. + local existing_prompt_command + if [[ -n "$trimmed" ]]; then + existing_prompt_command=${trimmed%${trimmed##*[![:space:]]}} + existing_prompt_command=${existing_prompt_command%;} + existing_prompt_command=${existing_prompt_command/%/;} + else + existing_prompt_command="" fi + echo -n "$existing_prompt_command" +} + +__bp_install() { + + # Remove setting our trap from PROMPT_COMMAND + PROMPT_COMMAND="${PROMPT_COMMAND//$__bp_trap_install_string}" + + # Remove this function from our PROMPT_COMMAND + PROMPT_COMMAND="${PROMPT_COMMAND//__bp_install;}" + # Exit if we already have this installed. if [[ "$PROMPT_COMMAND" == *"__bp_precmd_invoke_cmd"* ]]; then return 1; @@ -201,19 +234,16 @@ __bp_preexec_and_precmd_install() { # Adjust our HISTCONTROL Variable if needed. __bp_adjust_histcontrol - # Take our existing prompt command and append a semicolon to it - # if it doesn't already have one. - local existing_prompt_command + # Set so debug trap will work be invoked in subshells. + set -o functrace > /dev/null 2>&1 + shopt -s extdebug > /dev/null 2>&1 - if [[ -n "$PROMPT_COMMAND" ]]; then - existing_prompt_command=${PROMPT_COMMAND%${PROMPT_COMMAND##*[![:space:]]}} - existing_prompt_command=${existing_prompt_command%;} - existing_prompt_command=${existing_prompt_command/%/;} - else - existing_prompt_command="" - fi - # Finally install our traps. + local existing_prompt_command + existing_prompt_command=$(__bp_prompt_command_with_semi_colon) + + # Install our hooks in PROMPT_COMMAND to allow our trap to know when we've + # actually entered something. PROMPT_COMMAND="__bp_precmd_invoke_cmd; ${existing_prompt_command} __bp_interactive_mode;" trap '__bp_preexec_invoke_exec' DEBUG; @@ -221,9 +251,34 @@ __bp_preexec_and_precmd_install() { # of definition. precmd_functions+=(precmd) preexec_functions+=(preexec) + + # Since this is in PROMPT_COMMAND, invoke any precmd functions we have defined. + __bp_precmd_invoke_cmd + # Put us in interactive mode for our first command. + __bp_interactive_mode +} + +# Sets our trap and __bp_install as part of our PROMPT_COMMAND to install +# after our session has started. This allows bash-preexec to be inlucded +# at any point in our bash profile. Ideally we could set our trap inside +# __bp_install, but if a trap already exists it'll only set locally to +# the function. +__bp_install_after_session_init() { + + # Make sure this is bash that's running this and return otherwise. + if [[ -z "$BASH_VERSION" ]]; then + return 1; + fi + + local existing_prompt_command + existing_prompt_command=$(__bp_prompt_command_with_semi_colon) + + # Add our installation to be done last via our PROMPT_COMMAND. These are + # removed by __bp_install when it's invoked so it only runs once. + PROMPT_COMMAND="${existing_prompt_command} $__bp_trap_install_string __bp_install;" } # Run our install so long as we're not delaying it. if [[ -z "$__bp_delay_install" ]]; then - __bp_preexec_and_precmd_install + __bp_install_after_session_init fi; From 7e0f42701289a2dda337ce13f3e22a378d727fca Mon Sep 17 00:00:00 2001 From: Ryan Caloras Date: Fri, 22 Apr 2016 22:13:27 -0400 Subject: [PATCH 04/10] Change help message --- bashhub/bh.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bashhub/bh.py b/bashhub/bh.py index 99e70a7..0a4aed1 100755 --- a/bashhub/bh.py +++ b/bashhub/bh.py @@ -17,7 +17,7 @@ @cli.app.CommandLineApp def bh(app): - """Parse command line arguments and call our REST API""" + """Bashhub Search""" limit = app.params.number query = app.params.query system_name = BH_SYSTEM_NAME if app.params.system else None From ba88663afa78cad434bff6af9ffadc219a37a939 Mon Sep 17 00:00:00 2001 From: Ryan Caloras Date: Mon, 25 Apr 2016 17:30:31 -0400 Subject: [PATCH 05/10] Renamed trim function to match naming scheme --- bashhub/shell/deps/lib-bashhub.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bashhub/shell/deps/lib-bashhub.sh b/bashhub/shell/deps/lib-bashhub.sh index ced7df2..5a3917f 100644 --- a/bashhub/shell/deps/lib-bashhub.sh +++ b/bashhub/shell/deps/lib-bashhub.sh @@ -77,7 +77,7 @@ __bh_precmd() { __bh_process_command() { local bh_command - bh_command=$(BH_TRIM_WHITESPACE "$1") + bh_command=$(__bh_trim_whitespace "$1") # Sanity empty check if [[ -z "$bh_command" ]]; then @@ -106,7 +106,7 @@ __bh_process_command() { "$process_id" "$process_start" "$exit_status"&) } -BH_TRIM_WHITESPACE() { +__bh_trim_whitespace() { local var=$@ var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters From 8eada7a3ae8af26ea378e95f742b976983843ec0 Mon Sep 17 00:00:00 2001 From: Ryan Caloras Date: Mon, 25 Apr 2016 17:35:25 -0400 Subject: [PATCH 06/10] Don't add 'function' to function declarations --- bashhub/shell/bashhub.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bashhub/shell/bashhub.zsh b/bashhub/shell/bashhub.zsh index ad073f4..906f582 100644 --- a/bashhub/shell/bashhub.zsh +++ b/bashhub/shell/bashhub.zsh @@ -55,7 +55,7 @@ __bh_hook_bashhub() { fi } -function __bh_zsh_precmd() { +__bh_zsh_precmd() { if [[ -e $BH_HOME_DIRECTORY/response.bh ]]; then local COMMAND="`head -n 1 $BH_HOME_DIRECTORY/response.bh`" rm $BH_HOME_DIRECTORY/response.bh From 5da56937a26001b4e8cc5444ac5b28db67a2da11 Mon Sep 17 00:00:00 2001 From: Ryan Caloras Date: Mon, 25 Apr 2016 17:35:48 -0400 Subject: [PATCH 07/10] Small bugfix, was checking wrong array. --- bashhub/shell/bashhub.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bashhub/shell/bashhub.sh b/bashhub/shell/bashhub.sh index 59f93b8..688a729 100644 --- a/bashhub/shell/bashhub.sh +++ b/bashhub/shell/bashhub.sh @@ -45,7 +45,7 @@ __bh_hook_bashhub() { preexec_functions+=(__bh_preexec) fi - if ! contains_element __bh_precmd "${preexec_functions[@]}"; then + if ! contains_element __bh_precmd "${precmd_functions[@]}"; then precmd_functions+=(__bh_precmd) precmd_functions+=(__bh_bash_precmd) fi From cb1470a2d32cc8c3b61a135409b31975a4b84363 Mon Sep 17 00:00:00 2001 From: Ryan Caloras Date: Mon, 25 Apr 2016 16:13:06 -0400 Subject: [PATCH 08/10] Added bashhub installation check - Runs on session start to make sure our environment looks correct and that we have a config file. --- bashhub/shell/deps/lib-bashhub.sh | 29 +++++++++++++++++++++++++ tests/shell/lib-bashhub.bats | 36 +++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/bashhub/shell/deps/lib-bashhub.sh b/bashhub/shell/deps/lib-bashhub.sh index 5a3917f..6f0488c 100644 --- a/bashhub/shell/deps/lib-bashhub.sh +++ b/bashhub/shell/deps/lib-bashhub.sh @@ -106,6 +106,35 @@ __bh_process_command() { "$process_id" "$process_start" "$exit_status"&) } +# Small function to check our Bashhub installation. +# It's added to our precmd functions. On its initial run +# it removes itself from the precmd function array. +# This means it runs exactly once. +__bh_check_bashhub_installation() { + local ret + ret=0 + if [[ -n "$BASH_VERSION" && "$(trap)" != *"__bp_preexec_invoke_exec"* ]]; then + echo "Bashhub's preexec hook is being overriden and is not saving commands. Please resolve what may be holding the DEBUG trap." + ret=1 + elif [[ ! -f "$BH_HOME_DIRECTORY/config" ]]; then + echo "Missing Bashhub config file. Please run 'bashhub setup' to generate one." + ret=2 + elif ! grep -Fq "access_token" "$BH_HOME_DIRECTORY/config"; then + echo "Missing Bashhub access token. Please run 'bashhub setup' to re-login." + ret=3 + fi + + # Remove from precmd_functions so it only runs once when the session starts. + local delete + delete=(__bh_check_bashhub_installation) + precmd_functions=( "${precmd_functions[@]/$delete}" ) + + return $ret +} + +# Check our bashhub installation when the session starts. +precmd_functions+=(__bh_check_bashhub_installation) + __bh_trim_whitespace() { local var=$@ var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters diff --git a/tests/shell/lib-bashhub.bats b/tests/shell/lib-bashhub.bats index 577e498..c396fb5 100644 --- a/tests/shell/lib-bashhub.bats +++ b/tests/shell/lib-bashhub.bats @@ -27,6 +27,42 @@ teardown() { } +@test "__bh_check_bashhub_installation should find issues and run only once." { + + # Bash but no trap. + run '__bh_check_bashhub_installation' + [[ $status == 1 ]] + + # To skip trap check + unset BASH_VERSION + run '__bh_check_bashhub_installation' + [[ $status == 3 ]] + + + # Check that we succeed on the check and remove from precmd_functions + # That way it only runs exactly once. + echo "access_token=12345" >> "$BATS_TMPDIR/config" + precmd_functions+=(__bh_check_bashhub_installation __bh_something_else) + __bh_check_bashhub_installation + [[ $? == 0 ]] + run contains_element "__bh_check_bashhub_installation" "${precmd_functions[@]}" + [[ $status == 1 ]] + run contains_element "__bh_something_else" "${precmd_functions[@]}" + [[ $status == 0 ]] + +} + +@test "__bh_check_bashhub_installation should find we're missing a config" { + + # Bash and trap. + trap() { echo "__bp_preexec_invoke_exec"; } + + # No config file + rm "$BATS_TMPDIR/config" + run '__bh_check_bashhub_installation' + [[ $status == 2 ]] +} + @test "__bh_precmd should check if our home directory exists" { BH_HOME_DIRECTORY="$non-existent" __BH_SAVE_COMMAND="something to save" From d65770a8c766769e7098337cb6e474ff5d708b28 Mon Sep 17 00:00:00 2001 From: Ryan Caloras Date: Sat, 30 Apr 2016 14:55:44 -0400 Subject: [PATCH 09/10] Check that we have a system_name in our config --- bashhub/shell/deps/lib-bashhub.sh | 3 +++ tests/shell/lib-bashhub.bats | 6 +++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/bashhub/shell/deps/lib-bashhub.sh b/bashhub/shell/deps/lib-bashhub.sh index 6f0488c..6264783 100644 --- a/bashhub/shell/deps/lib-bashhub.sh +++ b/bashhub/shell/deps/lib-bashhub.sh @@ -122,6 +122,9 @@ __bh_check_bashhub_installation() { elif ! grep -Fq "access_token" "$BH_HOME_DIRECTORY/config"; then echo "Missing Bashhub access token. Please run 'bashhub setup' to re-login." ret=3 + elif ! grep -Fq "system_name" "$BH_HOME_DIRECTORY/config"; then + echo "Missing system name. Please run 'bashhub setup' to re-login." + ret=4 fi # Remove from precmd_functions so it only runs once when the session starts. diff --git a/tests/shell/lib-bashhub.bats b/tests/shell/lib-bashhub.bats index c396fb5..1e9cd58 100644 --- a/tests/shell/lib-bashhub.bats +++ b/tests/shell/lib-bashhub.bats @@ -38,10 +38,14 @@ teardown() { run '__bh_check_bashhub_installation' [[ $status == 3 ]] + # Check that we have a system name + echo "access_token=12345" >> "$BATS_TMPDIR/config" + run '__bh_check_bashhub_installation' + [[ $status == 4 ]] # Check that we succeed on the check and remove from precmd_functions # That way it only runs exactly once. - echo "access_token=12345" >> "$BATS_TMPDIR/config" + echo "system_name=test-system" >> "$BATS_TMPDIR/config" precmd_functions+=(__bh_check_bashhub_installation __bh_something_else) __bh_check_bashhub_installation [[ $? == 0 ]] From 489e81bf88d9cc3f5af4ac11b87f40545d1b5613 Mon Sep 17 00:00:00 2001 From: Ryan Caloras Date: Sat, 30 Apr 2016 15:17:29 -0400 Subject: [PATCH 10/10] Prepare for release of 1.0.2 --- bashhub/version.py | 2 +- install-bashhub.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bashhub/version.py b/bashhub/version.py index 0dbb755..63cde58 100644 --- a/bashhub/version.py +++ b/bashhub/version.py @@ -1 +1 @@ -__version__ = '1.0.2-dev' +__version__ = '1.0.2' diff --git a/install-bashhub.sh b/install-bashhub.sh index dfb2cd6..298e022 100755 --- a/install-bashhub.sh +++ b/install-bashhub.sh @@ -41,7 +41,7 @@ zshprofile=~/.zshrc # Optional parameter to specify a github branch # to pull from. -github_branch=${1:-'1.0.1'} +github_branch=${1:-'1.0.2'} install_bashhub() { check_dependencies