From 98e713d0c97c33afd0ba83473dea7c9b911d68f1 Mon Sep 17 00:00:00 2001 From: Marcel Ludwig Date: Fri, 29 Jan 2021 16:49:18 +0100 Subject: [PATCH] Add version command --- Dockerfile | 2 +- command/cmd.go | 2 ++ command/version.go | 25 +++++++++++++++++++++++++ config/runtime/version.go | 3 +++ main.go | 5 +++++ 5 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 command/version.go diff --git a/Dockerfile b/Dockerfile index 1592470ed..dd7bd07d2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,7 +8,7 @@ ENV GOFLAGS="-mod=vendor" \ RUN go generate && \ CGO_ENABLED=0 go build -v \ - -ldflags "-X ${VERSION_PACKAGE}.VersionName=`git describe --tags --abbrev=0 --exact-match || git symbolic-ref -q --short HEAD` -X ${VERSION_PACKAGE}.BuildName=`git rev-parse --short HEAD`" \ + -ldflags "-X ${VERSION_PACKAGE}.VersionName=`git describe --tags --abbrev=0 --exact-match || git symbolic-ref -q --short HEAD` -X ${VERSION_PACKAGE}.BuildName=`git rev-parse --short HEAD` -X ${VERSION_PACKAGE}.BuildDate=`date +'%F'`" \ -o /couper main.go && \ ls -lh /couper diff --git a/command/cmd.go b/command/cmd.go index 1316f937e..2bca3a10e 100644 --- a/command/cmd.go +++ b/command/cmd.go @@ -18,6 +18,8 @@ func NewCommand(cmd string) Cmd { switch strings.ToLower(cmd) { case "run": return NewRun(ContextWithSignal(context.Background())) + case "version": + return NewVersion() default: return nil } diff --git a/command/version.go b/command/version.go new file mode 100644 index 000000000..a64173b26 --- /dev/null +++ b/command/version.go @@ -0,0 +1,25 @@ +package command + +import ( + "github.com/sirupsen/logrus" + + "github.com/avenga/couper/config" + "github.com/avenga/couper/config/runtime" +) + +var _ Cmd = &Version{} + +type Version struct{} + +func NewVersion() *Version { + return &Version{} +} + +func (v Version) Execute(_ Args, _ *config.CouperFile, _ *logrus.Entry) error { + println(runtime.VersionName + " " + runtime.BuildDate + " " + runtime.BuildName) + return nil +} + +func (v Version) Usage() string { + return "couper version" +} diff --git a/config/runtime/version.go b/config/runtime/version.go index b760752dd..f0daa213c 100644 --- a/config/runtime/version.go +++ b/config/runtime/version.go @@ -1,6 +1,9 @@ package runtime +import "time" + var ( + BuildDate = time.Now().Format("2006-01-02") BuildName = "dev" VersionName = "0" ) diff --git a/main.go b/main.go index ae810a57b..f455ab0d4 100644 --- a/main.go +++ b/main.go @@ -40,6 +40,11 @@ func realmain(arguments []string) int { cmd := args[0] args = args[1:] + if cmd == "version" { // global options are not required, fast exit. + _ = command.NewCommand(cmd).Execute(args, nil, nil) + return 0 + } + var filePath, logFormat string set := flag.NewFlagSet("global", flag.ContinueOnError) set.StringVar(&filePath, "f", config.DefaultFileName, "-f ./couper.hcl")