From ddac6f183d85399ae139e01638eef4a8a8175888 Mon Sep 17 00:00:00 2001 From: Finn Date: Sat, 30 Jan 2021 22:21:18 -0800 Subject: [PATCH] Fix version command --- Makefile | 2 +- cmd/signaldctl/cmd/version.go | 38 +++++++++++++++++++++++++++++--- cmd/signaldctl/common/version.go | 8 +++++++ debian/rules | 8 +++++++ 4 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 cmd/signaldctl/common/version.go diff --git a/Makefile b/Makefile index b2cb0d1..4c7e089 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ signaldctl: signald/client-protocol - go build -o signaldctl ./cmd/signaldctl + go build -ldflags '-X gitlab.com/signald/signald-go/cmd/signaldctl/common.Version=$(shell git describe) -X gitlab.com/signald/signald-go/cmd/signaldctl/common.Branch=$(shell git rev-parse --abbrev-ref HEAD) -X gitlab.com/signald/signald-go/cmd/signaldctl/common.Commit=$(shell git rev-parse HEAD)' -o signaldctl ./cmd/signaldctl protocol.json: signald --dump-protocol | jq . > protocol.json diff --git a/cmd/signaldctl/cmd/version.go b/cmd/signaldctl/cmd/version.go index 3793a94..f020491 100644 --- a/cmd/signaldctl/cmd/version.go +++ b/cmd/signaldctl/cmd/version.go @@ -20,7 +20,9 @@ import ( "log" "os" + "github.com/jedib0t/go-pretty/v6/table" "github.com/spf13/cobra" + "gopkg.in/yaml.v2" "gitlab.com/signald/signald-go/cmd/signaldctl/common" "gitlab.com/signald/signald-go/signald/client-protocol/v1" @@ -38,9 +40,39 @@ var versionCmd = &cobra.Command{ if err != nil { log.Fatal(err) } - err = json.NewEncoder(os.Stdout).Encode(response) - if err != nil { - log.Fatal("error encoding output ", err) + + output := []v1.JsonVersionMessage{ + response, + { + Name: common.Name, + Branch: common.Branch, + Commit: common.Commit, + Version: common.Version, + }, + } + switch common.OutputFormat { + case "json": + err := json.NewEncoder(os.Stdout).Encode(output) + if err != nil { + log.Fatal(err, "error encoding response to stdout") + } + break + case "yaml": + err := yaml.NewEncoder(os.Stdout).Encode(output) + if err != nil { + log.Fatal(err, "error encoding response to stdout") + } + break + case "table": + t := table.NewWriter() + t.SetOutputMirror(os.Stdout) + t.AppendHeader(table.Row{"Name", "Version", "Branch", "Commit"}) + t.AppendRow(table.Row{common.Name, common.Version, common.Branch, common.Commit}) + t.AppendRow(table.Row{response.Name, response.Version, response.Branch, response.Commit}) + t.Render() + break + default: + log.Fatal("Unsupported output format") } }, } diff --git a/cmd/signaldctl/common/version.go b/cmd/signaldctl/common/version.go new file mode 100644 index 0000000..37d9236 --- /dev/null +++ b/cmd/signaldctl/common/version.go @@ -0,0 +1,8 @@ +package common + +var ( + Name = "signaldctl" + Branch string + Commit string + Version string +) diff --git a/debian/rules b/debian/rules index 8b5c1a7..f2b9d74 100755 --- a/debian/rules +++ b/debian/rules @@ -2,8 +2,16 @@ export DH_GOLANG_EXCLUDES := tools/ +PKG := gitlab.com/signald/signald-go +GO_LDFLAGS += -X $(PKG)/cmd/signaldctl/common.Version=$(DEB_VERSION) +GO_LDFLAGS += -X $(PKG)/cmd/signaldctl/common.Branch=$(shell git rev-parse HEAD) +GO_LDFLAGS += -X $(PKG)/cmd/signaldctl/common.Commit=$(CI_BUILD_REF) + override_dh_auto_install: dh_auto_install -- --no-source %: dh $@ --buildsystem=golang --with=golang + +override_dh_auto_build: + dh_auto_build -- -ldflags '$(GO_LDFLAGS)' \ No newline at end of file