1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
#!/bin/bash
#
#
# This script generates stub scripts for every Go "main" package in this
# repository. These stubs execute the corresponding main package via "go run".
#
set -euo pipefail
usage() {
echo "$(basename $0) [-c] -- create or clean Go main stub scripts"
exit 1
}
bindir="$(dirname $0)"
root="$(dirname "${bindir}")"
clean() {
grep -l "^# go-main-stubs stub script" "${bindir}"/* | grep -v bin/go-main-stubs | xargs rm || true
}
while getopts ":hc" arg; do
case $arg in
c)
echo "Cleaning old stubs"
clean
exit 0
;;
h | *)
usage
exit 0
;;
esac
done
clean
echo "Creating Go main stubs in ${bindir}"
echo -n .
for main in $(cd "${root}" && go list -f '{{if eq "main" .Name}}{{.ImportPath}}{{end}}' ./...); do
stub_script="${bindir}/$(basename $main)"
cat << EOF > ${stub_script}
#!/bin/bash
# go-main-stubs stub script
exec "\$(dirname \$0)/go" run $main "\$@"
EOF
chmod +x "${stub_script}"
echo -n .
done
echo
|