From d7570f5d90b867d734570d179af0e3d920b6fb20 Mon Sep 17 00:00:00 2001 From: hzy Date: Sat, 22 Oct 2022 15:37:59 +0800 Subject: [PATCH] Support `version` command --- buildAllPlatforms.sh | 4 ++++ main.go | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/buildAllPlatforms.sh b/buildAllPlatforms.sh index bc034be..2f1db10 100644 --- a/buildAllPlatforms.sh +++ b/buildAllPlatforms.sh @@ -4,6 +4,10 @@ OUTPUT="bin/" PROGRAM="mnh" LDFLAGS="-s -w" +ver="$(git describe --tags --match "v*" --dirty="" 2>/dev/null || git log -1 --pretty=format:"v0.0.0-%h" 2>/dev/null || echo "v0.0.0")" +[ -n "$(git status --porcelain |& grep -Ev '^\?\?')" ] && ver="$ver-$(date +"%Y%M%d-%H%m%S")" +LDFLAGS="$LDFLAGS -X main.version=$ver" + mkdir -p "$OUTPUT" rm -f "$OUTPUT/${PROGRAM}-"* diff --git a/main.go b/main.go index c87518d..276042d 100644 --- a/main.go +++ b/main.go @@ -20,6 +20,8 @@ import ( "github.com/hzyitc/mnh/routerForward" ) +var version = "v0.0.0" + var rootCmd = &cobra.Command{ Use: "mnh", Short: "A NAT hole punching tool that allows peers directly connect to your NATed server without client.", @@ -27,6 +29,13 @@ var rootCmd = &cobra.Command{ "mnh client will produce an ip:port pair for your NATed server which can be used for public access.", } +var versionCmd = &cobra.Command{ + Use: "version", + Run: func(cmd *cobra.Command, args []string) { + print(version) + }, +} + var tcpCmd = &cobra.Command{ Use: "tcp", Run: func(cmd *cobra.Command, args []string) { @@ -118,6 +127,7 @@ func runHook(event string, errmsg string, port string, addr string) { } func main() { + rootCmd.AddCommand(versionCmd) tcpCmdRegister(rootCmd) udpCmdRegister(rootCmd)