Skip to content

Commit

Permalink
Add ./scripts/generate-go & all files it generates
Browse files Browse the repository at this point in the history
  • Loading branch information
meeDamian committed Feb 2, 2020
1 parent a67777d commit e29b6d0
Show file tree
Hide file tree
Showing 73 changed files with 29,925 additions and 2,304 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/lncm/lnd-rpc
go 1.13

require (
github.com/golang/protobuf v1.3.2
github.com/golang/protobuf v1.3.3
google.golang.org/genproto v0.0.0-20200122232147-0452cf42e150
google.golang.org/grpc v1.26.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfb
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
Expand Down
171 changes: 171 additions & 0 deletions scripts/generate-go
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
#!/usr/bin/env sh

set -e

#
##
#

show_help() {
cat << EOF >&2
generate-go v1.0.0
Compile all .proto definitions into importable .go files
Usage: generate-go [options] LND_VERSION
Where LND_VERSION is in a form: [v]MAJOR.MINOR.PATCH (ex: v0.9.0), or "all" to generate for all versions
Options:
-h, --help, help Show this help message
-o, --output Save generated files to a specified dir (created, if doesn't exist)
Examples:
./generate-go all
./generate-go -o /tmp/last/ v0.9.0
github: github.com/lncm/lnd-rpc/
EOF
}

# If no arguments passed, show help and exit
if [ "$#" -le 0 ]; then
show_help
exit 1
fi

# Define logging utility fns as early as possible
log() {
# shellcheck disable=SC2059
>&2 printf "$*\n"
}
log_pad() {
log "\t$*"
}
log_arrow() {
log_pad "-> $*"
}
log_err() {
log
log_pad "ERR: $*"
log
}
log_ok() {
log_arrow "${*:-ok}" # if nothing passed, write "ok"
}
log_err_input() {
log_err "$*"
show_help
}


# DIR is scoped globally, because it can be used anywhere
DIR=


check_dependencies() {
log "Checking dependencies…"
for cmd in "$@"; do
if ! command -v "${cmd}" >/dev/null 2>&1; then
log_err "This script requires \"${cmd}\" to be installed"
exit 1
fi
done
log_ok
}


# Take .proto file, and generate a .go file out of it
generate_go_for_tag() {
tag=$1

log "Generating .go for ${tag#./}"
for proto in "${tag}"/*/*.proto; do
log_arrow "${proto#./}"
protoc --go_out=plugins=grpc,paths=source_relative:${DIR} -I. -I"${tag}" "${proto}"
done

log_ok "done"
}


#
## MAIN; This is where the logic of execution begins
#
while :; do
opt="$1"

# Handle fmt: `--output=/tmp/` or `-o=/tmp/`; Split `opt` on `=` into `opt` & `value`
# NOTE: prepended with `=`, because if `-d` isn't found, `cut` ignores `-f`, and returns the whole string regardless…
value="$(echo "=${opt}" | cut -d= -f3)"
if [ -n "${value}" ]; then
opt="$(echo "=${opt}" | cut -d= -f2)"
fi

case "${opt}" in
-h|--help|help)
show_help
exit 0
;;

-o|--output)
if [ -z "${value}" ] && [ "$2" = "${2#-}" ]; then
value="$2"
shift
fi

if [ -z "${value}" ]; then
log_err_input "${opt} requires a non-empty argument"
exit 1
fi

DIR="${value}"
;;

--) shift ; break ;;

-?*)
log_err_input "Unknown option: $1"
exit 1
;;

*) break
esac

shift
done


# If DIR is already set, use it
# If not, use `pwd`
DIR="${DIR:-$(pwd)}"

if [ -z "$1" ]; then
log_err_input 'Lnd version has to be provided (ex: v0.9.0), or "all"'
exit 1
fi

TAG="$1"
shift

if [ "${TAG}" != "all" ]; then
# Make sure version always starts with v
TAG="v${TAG#v}"

# Remove possible`-beta` suffix
TAG="$(echo "${TAG}" | cut -d- -f1)"

else
TAG=
fi

check_dependencies protoc
mkdir -p "${DIR}"

# Use TAG, if provided, otherwise loop through all ./v*/ directories
for dir in ./${TAG:-v**}; do
generate_go_for_tag "${dir}"
done
Loading

0 comments on commit e29b6d0

Please sign in to comment.