Last active
October 1, 2017 23:26
-
-
Save cstockton/6933ae5dadcb07354159300ed6e9b085 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # _some_ of my env vars used for the workspace.sh above. | |
| #### | |
| # Defaults for all machines | |
| export ONE=/one | |
| export ONE_SEC=/sec | |
| export ONE_ENV="$(hostname)" | |
| # Workspace persistence @ loading | |
| export ONE_WS=/ws | |
| export ONE_WS_ENV="${ONE_ENV}" | |
| export ONE_WS_HOME="${ONE_WS}/${ONE_WS_ENV}" | |
| # Load the saved environment if it exists. | |
| test -r "${ONE_WS}/save.env" && \ | |
| source "${ONE_WS}/save.env" | |
| # Languages | |
| export GOTOOLS="${ONE}/ws/gotools" | |
| export GOROOT="${ONE}/lang/go" | |
| export GOROOT_BOOTSTRAP="${ONE}/lang/go" | |
| export NODE_PATH="${ONE}/lang/node/node_modules" | |
| # IDE defaults | |
| export ATOM_HOME="${ONE}/ide/atom" | |
| # Paths | |
| export PATH="/usr/sbin:/usr/bin:/sbin:/bin" | |
| export PATH="${PATH}:/usr/local/sbin:/usr/local/bin" | |
| export PATH="${PATH}:${ONE}/bin" | |
| export PATH="${PATH}:${GOROOT}/bin" | |
| export PATH="${PATH}:${GOTOOLS}/bin" | |
| export PATH="${PATH}:${ONE}/lang/node/bin" | |
| export PATH="${PATH}:${ONE}/lang/protoc/bin" | |
| export PATH="${PATH}:${HOME}/bin" | |
| export PATH="${PATH}:${HOME}/.local/bin" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # dot/bash/aliases/lang.go.sh | |
| function gowatch { gowatch "$@" $(gopkg); } | |
| function gopkg { go list 2>/dev/null; } | |
| function gopkgident { basename $(gopkg); } | |
| function gosym { go tool nm -size "$@" $(gopkgident); } | |
| function gosymx { go tool nm -size -sort address $(gopkgident); } | |
| function gosyml { go tool nm -size -sort size $(gopkgident); } | |
| function golex { go build -gcflags="-x"; } | |
| function gols { find . -maxdepth 1 -type f -name '*.go'; } | |
| function gosrc { find . -maxdepth 1 -type f -name '*.go' | grep -v '_test.go$'; } | |
| function gotests { find . -maxdepth 1 -type f -name '*_test.go'; } | |
| function gomains { grep -l '^package main' $(_gosrc) | awk -F: '{print $1}'; } | |
| function goesc3 { _a="$@"; _a=('-m=3' "${_a[@]}"); goesc "${_a[@]}"; } | |
| function goesc4 { _a="$@"; _a=('-m=4' "${_a[@]}"); goesc "${_a[@]}"; } | |
| function gocovh { _gocov -html; } | |
| function gocovfn { _gocov -func; } | |
| function goasm { gogc -S; } | |
| # passes gc flags, but uses stdout for grepping | |
| function gogc { | |
| test $# -eq 0 && { | |
| go build -gcflags '--help' | |
| echo >&2 "gogc: no arguments given, printed help" | |
| return 1; | |
| } | |
| go build -gcflags "$@" 2>&1 | |
| } | |
| # print current pkg assembly from obj file to stdout | |
| function goobj { | |
| _build_dir=$(_go_build_dir) | |
| _pkg="$(gopkg)" | |
| _obj="${_build_dir}/${_pkg}.a" | |
| test -f "${_obj}" || { | |
| echo >&2 "goasm: WORK dir did not contain object file '${_obj}'" | |
| return 1; | |
| } | |
| go tool objdump "$@" "${_obj}" | |
| _go_build_dir_rm "${_build_dir}" | |
| } | |
| # all pkgs starting from each given arg; or cwd if args -eq 0 | |
| function gopkgs { | |
| [ $# -eq 0 ] && _gopkg_cwd || for _arg in "$@"; do | |
| (cd "${_arg}" && _gopkg_cwd) | |
| done | |
| } | |
| # prints a go tmp build dir | |
| function _go_build_dir { | |
| _work_var=$(go build -work 2>&1) | |
| _build_dir=$(eval "${_work_var}"; echo "${WORK}") | |
| _go_build_dir_chk "${_build_dir}" || { return 1; } | |
| echo "${_build_dir}" | |
| } | |
| # safe removal of build dir (no rm -rf "/$herpderp") | |
| function _go_build_dir_rm { | |
| _go_build_dir_chk "${_build_dir}" || { return 1; } | |
| _rmobj="$(find "${1}" -type f -name '*.a')" | |
| test -n "${_rmobj}" || { | |
| echo >&2 "_go_build_dir_rm: unexpected empty obj list" | |
| return 1; | |
| } | |
| _rm_dirs=$(cd "${1}" && find . -type d -empty | sed 's|^\./||') | |
| test -n "${_rmobj}" || { | |
| echo >&2 "_rm_dirs: unexpected empty dir list" | |
| return 1; | |
| } | |
| # removes obj files | |
| rm $_rmobj | |
| # safely removes empty parents of empty children | |
| (cd "${1}" && rmdir -p $_rm_dirs) | |
| # removes the build dir only if empty (should be due to above two cmds) | |
| rmdir "${1}" | |
| } | |
| # check a build dir var | |
| function _go_build_dir_chk { | |
| test -d "${1}" || { | |
| echo >&2 "_go_build_dir_chk: WORK dir was not a directory '${1}'" | |
| return 1; | |
| } | |
| basename "${1}" | grep -q "^go-build" || { | |
| echo >&2 "_go_build_dir_chk: WORK dir name did not begin with go-build '${1}'" | |
| return 1; | |
| } | |
| test "." = "$(basename "${1}")" && { | |
| echo >&2 "_go_build_dir_chk: WORK dir had no parent '${1}'" | |
| return 1; | |
| } | |
| return 0 | |
| } | |
| # all pkgs starting from cwd | |
| function _gopkg_cwd { | |
| # determine sane base path for when I am in a <repo>/pkg dir | |
| _cur_pkg=$(go list 2>/dev/null) && { | |
| echo $_cur_pkg | |
| } || { | |
| _cur_pkg="${PWD#$GOPATH/src/}" | |
| } | |
| find . -mindepth 2 -type f -name '*.go' -exec grep -l '^package ' {} \; \ | |
| | sort | sed 's|^\./||' | while read _file; | |
| do | |
| echo "$_cur_pkg/$(dirname "${_file}")" | |
| done | sort | uniq | |
| } | |
| # gocov runs go test with coverage and a tmp file. passes | |
| # any extra args to go test. | |
| function _gocov { | |
| _gocov_tmp=$(mktemp -p . -t tmp.gocov.XXXXXXXXXX) | |
| go test -coverprofile="${_gocov_tmp}" | |
| test $# -gt 0 && { | |
| _strip_cwd="$(gopkg)" | |
| go tool cover $1="${_gocov_tmp}" | while read _line; do | |
| echo "${_line#$_strip_cwd}" | |
| done | |
| } | |
| rm "$_gocov_tmp" | |
| } | |
| # goesc - run go build or test with escape analysis, i.e.: | |
| # | |
| # goesc # go build -gcflags "-m=2 -l" 2>&1 | |
| # goesc build # go build -gcflags "-m=2 -l" 2>&1 | |
| # goesc run # go run -gcflags "-m=2 -l" 2>&1 | |
| # goesc build -m 4 # go build -gcflags "-m=4 -l" 2>&1 | |
| # goesc -m=4 test # go test -gcflags "-m=4 -l" 2>&1 | |
| # | |
| function goesc { | |
| _args=() | |
| _cmd="" | |
| _m=2 # default is 2, shows reasons for escapes | |
| while (( "$#" )); do | |
| _arg="$1" | |
| case $_arg in | |
| -m) | |
| shift | |
| _m=$1 | |
| shift | |
| ;; | |
| -m=*) | |
| _m="$(echo "${1}" | cut -d'=' -f2)" | |
| shift | |
| ;; | |
| test) | |
| _cmd="test" | |
| shift | |
| ;; | |
| run) | |
| _cmd="run" | |
| shift | |
| ;; | |
| run) | |
| _cmd="build" | |
| shift | |
| ;; | |
| *) | |
| _args+=("$1") | |
| shift | |
| ;; | |
| esac | |
| done | |
| # assign m if none given or too large | |
| test $_m -ge 1 && test $_m -le 4 || _m=2 | |
| # default cmd is build | |
| test -n "${_cmd}" || _cmd="build" | |
| go "${_cmd}" -gcflags "-m=${_m} -l" "${_args[@]}" 2>&1 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pathpush () { pathdel "${1}"; export PATH="$PATH:$1"; } | |
| pathunshift () { pathdel "${1}"; export PATH="$1:$PATH"; } | |
| pathdel () { export PATH=$(echo -n "${PATH}" | awk -v RS=':' -v ORS=':' '$0 != "'$1'"' | sed 's/:$//'); } | |
| pathclean () { export PATH=$(echo "$PATH" | awk -v RS=':' -v ORS=":" '!a[$1]++' | sed 's/[:]$//g'); } | |
| # workspaces func, simply takes a arg if it exists and sources it rooted by my | |
| # $ONE_WS root to activate it, cd's to ONE_WS_HOME or the WS root with no arg. | |
| function ws { | |
| if [ $# -eq 0 ]; then | |
| for _path in "${ONE_WS_HOME}" "${ONE_WS}/${ONE_WS_ENV}" "${ONE_WS}"; | |
| do | |
| test -d "${_path}" && cd "${_path}" && return 0 || { | |
| echo >&2 "invalid path: ${_path}"; | |
| continue | |
| } | |
| done | |
| echo >&2 "no valid workspace paths, bad environment config"; | |
| return 1; | |
| fi | |
| # test for /ws/<wsname> | |
| _ws_dir="${ONE_WS}/${1}" | |
| test -d "${_ws_dir}" || { | |
| echo >&2 "workspace dir does not exist: ${_ws_dir}"; | |
| return 1; | |
| } | |
| # test for /ws/<wsname>/dot/env | |
| _ws_env="${_ws_dir}/dot/env" | |
| test -r "${_ws_env}" || { | |
| echo >&2 "workspace activiation file does not exist: ${_ws_env}"; | |
| return 1; | |
| } | |
| # workspace is valid, activate it | |
| echo "activating worksapce '${1}' using ${_ws_env}" | |
| # remove old bin dir, if any | |
| pathdel "${ONE_WS}/${ONE_WS_ENV}/bin" | |
| # setup the ENV, HOME | |
| export ONE_WS_ENV="${1}" | |
| export ONE_WS_HOME="${ONE_WS}/${ONE_WS_ENV}" | |
| # add bin dir to path | |
| test -d "${ONE_WS}/${ONE_WS_ENV}/bin" && \ | |
| pathpush "${ONE_WS}/${ONE_WS_ENV}/bin" | |
| # source the workspace specific env file which will do various setup to dev | |
| # in that environment / language / app. | |
| source "${_ws_env}" | |
| # save this env so future terminals use it by default. | |
| echo "export ONE_WS_ENV='${ONE_WS_ENV}'" > "${ONE_WS}/save.env" | |
| # ws # call ws with no arg to cd to home | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment