Skip to content

Commit

Permalink
Merge branch '1.0.2-dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
rcaloras committed Apr 30, 2016
2 parents 05a49ef + 489e81b commit 6df5456
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 44 deletions.
4 changes: 3 additions & 1 deletion bashhub/bh.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import cli.app
import os
import io
import traceback

from model import MinCommand
from bashhub_globals import *
Expand All @@ -16,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
Expand Down Expand Up @@ -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:
Expand Down
14 changes: 6 additions & 8 deletions bashhub/rest_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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"

Expand All @@ -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.")


Expand All @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion bashhub/shell/bashhub.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion bashhub/shell/bashhub.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
113 changes: 84 additions & 29 deletions bashhub/shell/deps/bash-preexec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 ([email protected])
# Forked from Original Author: Glyph Lefkowitz
#
# V0.2.3
# V0.3.0
#

# General Usage:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -147,16 +159,14 @@ __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

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
Expand All @@ -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;
Expand All @@ -201,29 +234,51 @@ __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;

# Add two functions to our arrays for convenience
# 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;
36 changes: 34 additions & 2 deletions bashhub/shell/deps/lib-bashhub.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -106,7 +106,39 @@ __bh_process_command() {
"$process_id" "$process_start" "$exit_status"&)
}

BH_TRIM_WHITESPACE() {
# 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
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.
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
var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters
Expand Down
2 changes: 1 addition & 1 deletion bashhub/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.0.1'
__version__ = '1.0.2'
2 changes: 1 addition & 1 deletion install-bashhub.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 6df5456

Please sign in to comment.