From 218359a3ab9ad6bdf4460da15b1576503e435f45 Mon Sep 17 00:00:00 2001 From: Laurent Defert Date: Fri, 8 Jan 2021 16:14:36 +0100 Subject: [PATCH] cmd: version command --- .gitlab-ci.yml | 8 ++++---- build/ci/build_debian.sh | 4 +++- cmd/version.go | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 cmd/version.go diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index fc56160..bd5a55e 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -9,8 +9,8 @@ build-binary-buster: - ./build/ci/build_debian.sh after_script: - gzip gopherCap - rules: - - if: '$CI_BUILD_TAG =~ /^build-.*/' + only: + - tags build-binary-ubuntu-2004: image: ubuntu:20.04 @@ -23,5 +23,5 @@ build-binary-ubuntu-2004: - ./build/ci/build_debian.sh after_script: - gzip gopherCap - rules: - - if: '$CI_BUILD_TAG =~ /^build-.*/' + only: + - tags diff --git a/build/ci/build_debian.sh b/build/ci/build_debian.sh index 802a7bb..07612a1 100755 --- a/build/ci/build_debian.sh +++ b/build/ci/build_debian.sh @@ -5,11 +5,13 @@ GO_VERSION=${GO_VERSION:-1.15.6} GOROOT_BASE=${GOROOT_BASE:-/opt} GOROOT=$GOROOT_BASE/go GOPATH=${GOPATH:-/var/go} +GOPHERCAP_VERSION=${CI_COMMIT_TAG:-git $CI_COMMIT_SHORT_SHA} apt-get update && apt-get install -y libpcap-dev wget build-essential wget -O /tmp/golang.tgz https://golang.org/dl/go${GO_VERSION}.linux-amd64.tar.gz tar -xzf /tmp/golang.tgz -C $GOROOT_BASE mkdir $GOPATH $GOROOT/bin/go get -u ./ -$GOROOT/bin/go build -o ./gopherCap ./ + +$GOROOT/bin/go build -ldflags="-X 'gopherCap/cmd.Version=$GOPHERCAP_VERSION'" -o ./gopherCap ./ ./gopherCap --help diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 0000000..6372248 --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,37 @@ +/* +Copyright © 2020 Stamus Networks oss@stamus-networks.com + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +var Version = "development" + +// versionCmd represents the version command +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Shows gopherCap version.", + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("gopherCap", Version) + }, +} + +func init() { + rootCmd.AddCommand(versionCmd) +}