Created
November 29, 2018 09:36
-
-
Save iOliverNguyen/8a2be23407e50870f6841d5d1e112a63 to your computer and use it in GitHub Desktop.
get.sh is a tool for retrieving environment information for scripting with go modules
This file contains 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
#!/usr/bin/env bash | |
this=$0 | |
prog=$(basename $0) | |
current=$prog | |
quickusage="Run '$prog help' for usage." | |
quickusage() { | |
echo "" | |
echo "$current: unknown command" | |
echo "$quickusage" | |
exit 126 | |
} | |
expect() { | |
local inlineusage=$1 | |
shift | |
if [[ -z "$1" ]]; then | |
echo "Usage: $current $inlineusage" | |
echo "$quickusage" | |
exit 126 | |
fi | |
current="$current $1" | |
} | |
cmd_help() { | |
usage="$prog is a tool for retrieving environment information for scripting with go modules. | |
Usage: | |
$prog <command> [arguments] | |
The commands are: | |
gopath print the current gopath or default to ~/go | |
mod | |
srcpath <import> print the location of 'import' from GOPATH/pkg/mod | |
" | |
echo "$usage" | |
} | |
cmd_gopath() { | |
if [[ -n "$GOPATH" ]]; then | |
echo $GOPATH | |
else | |
echo ~/go | |
fi | |
} | |
cmd_mod() { | |
set -eo pipefail | |
subcmd=$1 ; expect "srcpath [arguments]" $@ ; shift | |
case $subcmd in | |
srcpath) | |
import=$1 ; expect "<import>" $@ ; shift | |
srcpath=$(go list -m $import) | |
echo $($this gopath)/pkg/mod/${srcpath/ /@} | |
;; | |
*) | |
quickusage | |
esac | |
} | |
command=$1 ; expect "<command>" $@ ; shift | |
case $command in | |
"-h" | "--help") | |
cmd_help | |
;; | |
*) | |
cmd_${command} $@ | |
if [[ $? == 127 ]]; then quickusage ; fi | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment