*: add --version flag to commands

Since stolonctl has different subcommands, also a `version` subcommand is added
over the --version flag.
This commit is contained in:
Simone Gotti 2017-12-04 17:41:25 +01:00
parent 872b35b9a3
commit 0d461c4d9b
7 changed files with 85 additions and 18 deletions

13
build
View File

@ -43,6 +43,9 @@ mkdir -p ${BINDIR}
export GO15VENDOREXPERIMENT=1
VERSION=$(${BASEDIR}/scripts/git-version.sh)
LD_FLAGS="-s -X ${REPO_PATH}/cmd.Version=$VERSION"
# for static compilation we need to compile with cgo disabled (CGO_ENABLED=0),
# this will trigger a rebuild of all the packages and also the go std library
# (using a different install suffix called `cgo`, will use this since it's the
@ -73,23 +76,23 @@ if [ -z $use_go_install ]; then
echo "or manually rebuild stdlib executing the command 'CGO_ENABLED=0 go install -a -installsuffix cgo std' from a user with write access to ${go_root_dir}/pkg"
for cmd in sentinel proxy; do
CGO_ENABLED=0 go build -a -installsuffix cgo -ldflags '-s' -o ${BINDIR}/stolon-${cmd} ${REPO_PATH}/cmd/${cmd}
CGO_ENABLED=0 go build -a -installsuffix cgo -ldflags "$LD_FLAGS" -o ${BINDIR}/stolon-${cmd} ${REPO_PATH}/cmd/${cmd}
done
CGO_ENABLED=0 go build -a -installsuffix cgo -ldflags '-s' -o ${BINDIR}/stolonctl ${REPO_PATH}/cmd/stolonctl
CGO_ENABLED=0 go build -a -installsuffix cgo -ldflags "$LD_FLAGS" -o ${BINDIR}/stolonctl ${REPO_PATH}/cmd/stolonctl
else
for cmd in sentinel proxy; do
CGO_ENABLED=0 go install -installsuffix cgo -ldflags '-s' ${REPO_PATH}/cmd/${cmd}
CGO_ENABLED=0 go install -installsuffix cgo -ldflags "$LD_FLAGS" ${REPO_PATH}/cmd/${cmd}
rm -f ${BINDIR}/stolon-${cmd}
cp ${GOPATH}/bin/${cmd} ${BINDIR}/stolon-${cmd}
done
CGO_ENABLED=0 go install -installsuffix cgo -ldflags '-s' ${REPO_PATH}/cmd/stolonctl
CGO_ENABLED=0 go install -installsuffix cgo -ldflags "$LD_FLAGS" ${REPO_PATH}/cmd/stolonctl
rm -f ${BINDIR}/stolonctl
cp ${GOPATH}/bin/stolonctl ${BINDIR}/
fi
# stolon-keeper cannot be statically built since it needs to get its current
# running user and this is not available with cgo disabled
go install ${REPO_PATH}/cmd/keeper
go install -ldflags "$LD_FLAGS" ${REPO_PATH}/cmd/keeper
rm -f ${BINDIR}/stolon-keeper
cp ${GOPATH}/bin/keeper ${BINDIR}/stolon-keeper

View File

@ -28,6 +28,7 @@ import (
"syscall"
"time"
"github.com/sorintlab/stolon/cmd"
"github.com/sorintlab/stolon/common"
"github.com/sorintlab/stolon/pkg/cluster"
"github.com/sorintlab/stolon/pkg/flagutil"
@ -46,8 +47,9 @@ import (
var log = slog.S()
var cmdKeeper = &cobra.Command{
Use: "stolon-keeper",
Run: keeper,
Use: "stolon-keeper",
Run: keeper,
Version: cmd.Version,
}
type KeeperLocalState struct {

View File

@ -23,6 +23,7 @@ import (
"sync"
"time"
"github.com/sorintlab/stolon/cmd"
"github.com/sorintlab/stolon/common"
"github.com/sorintlab/stolon/pkg/cluster"
"github.com/sorintlab/stolon/pkg/flagutil"
@ -39,8 +40,9 @@ import (
var log = slog.S()
var cmdProxy = &cobra.Command{
Use: "stolon-proxy",
Run: proxy,
Use: "stolon-proxy",
Run: proxy,
Version: cmd.Version,
}
type config struct {

View File

@ -29,6 +29,7 @@ import (
"syscall"
"time"
"github.com/sorintlab/stolon/cmd"
"github.com/sorintlab/stolon/common"
"github.com/sorintlab/stolon/pkg/cluster"
"github.com/sorintlab/stolon/pkg/flagutil"
@ -53,8 +54,9 @@ const (
)
var cmdSentinel = &cobra.Command{
Use: "stolon-sentinel",
Run: sentinel,
Use: "stolon-sentinel",
Run: sentinel,
Version: cmd.Version,
}
type config struct {

View File

@ -21,6 +21,7 @@ import (
"path/filepath"
"strings"
"github.com/sorintlab/stolon/cmd"
"github.com/sorintlab/stolon/common"
"github.com/sorintlab/stolon/pkg/cluster"
"github.com/sorintlab/stolon/pkg/flagutil"
@ -35,16 +36,21 @@ const (
)
var cmdStolonCtl = &cobra.Command{
Use: "stolonctl",
Short: "stolon command line client",
Use: "stolonctl",
Short: "stolon command line client",
Version: cmd.Version,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if cfg.clusterName == "" {
die("cluster name required")
}
if cfg.storeBackend == "" {
die("store backend type required")
if cmd.Name() != "stolonctl" && cmd.Name() != "version" {
if cfg.clusterName == "" {
die("cluster name required")
}
if cfg.storeBackend == "" {
die("store backend type required")
}
}
},
// just defined to make --version work
Run: func(c *cobra.Command, args []string) { c.Help() },
}
type config struct {
@ -69,6 +75,20 @@ func init() {
cmdStolonCtl.PersistentFlags().StringVar(&cfg.clusterName, "cluster-name", "", "cluster name")
}
var cmdVersion = &cobra.Command{
Use: "version",
Run: versionCommand,
Short: "Display the version",
}
func init() {
cmdStolonCtl.AddCommand(cmdVersion)
}
func versionCommand(c *cobra.Command, args []string) {
stdout("stolonctl version %s", cmd.Version)
}
func main() {
flagutil.SetFlagsFromEnv(cmdStolonCtl.PersistentFlags(), "STOLONCTL")

17
cmd/version.go Normal file
View File

@ -0,0 +1,17 @@
// Copyright 2017 Sorint.lab
//
// 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
var Version = "No version defined at build time"

21
scripts/git-version.sh Executable file
View File

@ -0,0 +1,21 @@
#!/bin/sh -e
# parse the current git commit hash
COMMIT=`git rev-parse HEAD`
# check if the current commit has a matching tag
TAG=$(git describe --exact-match --abbrev=0 --tags ${COMMIT} 2> /dev/null || true)
# use the matching tag as the version, if available
if [ -z "$TAG" ]; then
VERSION=$COMMIT
else
VERSION=$TAG
fi
# check for changed files (not untracked files)
if [ -n "$(git diff --shortstat 2> /dev/null | tail -n1)" ]; then
VERSION="${VERSION}-dirty"
fi
echo $VERSION