From 57016b06e485127cfc05eed330cef9b32d41f477 Mon Sep 17 00:00:00 2001 From: Dan Kanefsky Date: Tue, 9 Jan 2024 14:35:02 -0800 Subject: [PATCH] add version command --- Makefile | 21 +++++++++++++++++++++ cmd/flags.go | 18 ++++++++++++++++++ cmd/root.go | 13 ++++++++----- cmd/version.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 cmd/flags.go create mode 100644 cmd/version.go diff --git a/Makefile b/Makefile index 81becce..a112538 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,14 @@ +VERSION := $(shell echo $(shell git describe --tags) | sed 's/^v//') +COMMIT := $(shell git log -1 --format='%H') +DIRTY := $(shell git status --porcelain | wc -l | xargs) + +ldflags = -X github.com/strangelove-ventures/noble-cctp-relayer/cmd.Version=$(VERSION) \ + -X github.com/strangelove-ventures/noble-cctp-relayer/cmd.Commit=$(COMMIT) \ + -X github.com/strangelove-ventures/noble-cctp-relayer/cmd.Dirty=$(DIRTY) + +ldflags += $(LDFLAGS) +ldflags := $(strip $(ldflags)) + .PHONY: all format lint all: format lint @@ -17,3 +28,13 @@ lint: @echo "🤖 Running linter..." @go run $(golangci_lint_cmd) run --timeout=10m @echo "✅ Completed linting!" + + +############################################################################### +### Install ### +############################################################################### + +install: go.sum + @echo "🤖 Building noble-cctp-relayer..." + @go build -mod=readonly -ldflags '$(ldflags)' -o $(GOBIN)/noble-cctp-relayer main.go + diff --git a/cmd/flags.go b/cmd/flags.go new file mode 100644 index 0000000..5843342 --- /dev/null +++ b/cmd/flags.go @@ -0,0 +1,18 @@ +package cmd + +import ( + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +const ( + flagJSON = "json" +) + +func jsonFlag(v *viper.Viper, cmd *cobra.Command) *cobra.Command { + cmd.Flags().BoolP(flagJSON, "j", false, "returns the response in json format") + if err := v.BindPFlag(flagJSON, cmd.Flags().Lookup(flagJSON)); err != nil { + panic(err) + } + return cmd +} diff --git a/cmd/root.go b/cmd/root.go index 0d18aa6..271b2ef 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -5,16 +5,17 @@ import ( "encoding/hex" "encoding/json" "fmt" + "io" + "net/http" + "os" + "strconv" + "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" "github.com/cosmos/cosmos-sdk/types/bech32" "github.com/ethereum/go-ethereum/ethclient" "github.com/gin-gonic/gin" "github.com/strangelove-ventures/noble-cctp-relayer/cmd/ethereum" "github.com/strangelove-ventures/noble-cctp-relayer/types" - "io" - "net/http" - "os" - "strconv" "cosmossdk.io/log" "github.com/rs/zerolog" @@ -46,7 +47,9 @@ func init() { rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "config.yaml", "") rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "") - rootCmd.AddCommand(startCmd) + rootCmd.AddCommand( + startCmd, + versionCmd) cobra.OnInitialize(func() { if verbose { diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 0000000..1ead9cc --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,51 @@ +package cmd + +import ( + "fmt" + "runtime" + + "github.com/spf13/cobra" + "gopkg.in/yaml.v2" +) + +var ( + // Version defines the application version (defined at compile time) + Version = "" + Commit = "" + Dirty = "" +) + +type versionInfo struct { + Version string `json:"version" yaml:"version"` + Commit string `json:"commit" yaml:"commit"` + Go string `json:"go" yaml:"go"` +} + +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Print the relayer version info", + RunE: getVersionCmd, +} + +func getVersionCmd(cmd *cobra.Command, args []string) error { + // jsn, err := cmd.Flags().GetBool(flagJSON) + // if err != nil { + // return err + // } + + commit := Commit + if Dirty != "0" { + commit += " (dirty)" + } + + verInfo := versionInfo{ + Version: Version, + Commit: commit, + Go: fmt.Sprintf("%s %s/%s", runtime.Version(), runtime.GOOS, runtime.GOARCH), + } + + bz, err := yaml.Marshal(&verInfo) + + fmt.Fprintln(cmd.OutOrStdout(), string(bz)) + return err +}