Move version vars to pilosa package

This commit is contained in:
Alan Bernstein 2017-04-21 10:14:21 -05:00
parent 985c0b4cdf
commit 83e13bec74
5 changed files with 25 additions and 24 deletions

View file

@ -6,7 +6,7 @@ VERSION := $(shell git describe --tags 2> /dev/null || echo unknown)
IDENTIFIER := $(VERSION)-$(GOOS)-$(GOARCH)
CLONE_URL=github.com/pilosa/pilosa
BUILD_TIME=`date -u +%FT%T%z`
LDFLAGS=-ldflags "-X github.com/pilosa/pilosa/cmd.Version=$(VERSION) -X github.com/pilosa/pilosa/cmd.BuildTime=$(BUILD_TIME)"
LDFLAGS=-ldflags "-X github.com/pilosa/pilosa.Version=$(VERSION) -X github.com/pilosa/pilosa.BuildTime=$(BUILD_TIME)"
default: test pilosa

View file

@ -5,22 +5,18 @@ import (
"io"
"strings"
"github.com/pilosa/pilosa"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
var (
Version string
BuildTime string
)
// TODO maybe give this an Add method which will ensure two command
// with same name aren't added
var subcommandFns = map[string]func(stdin io.Reader, stdout, stderr io.Writer) *cobra.Command{}
func NewRootCommand(stdin io.Reader, stdout, stderr io.Writer) *cobra.Command {
setupVersionBuild()
// pilosa.SetupVersionBuild() // want to remove - see version.go
rc := &cobra.Command{
Use: "pilosa",
Short: "Pilosa - A Distributed In-memory Binary Bitmap Index.",
@ -32,8 +28,8 @@ tools for administering pilosa, importing/exporting data,
backing up, and more. Complete documentation is available
at http://pilosa.com/docs
Version: ` + Version + `
Build Time: ` + BuildTime + "\n",
Version: ` + pilosa.Version + `
Build Time: ` + pilosa.BuildTime + "\n",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
v := viper.New()
err := setAllConfig(v, cmd.Flags(), "PILOSA")
@ -63,15 +59,6 @@ Build Time: ` + BuildTime + "\n",
return rc
}
func setupVersionBuild() {
if Version == "" {
Version = "v0.0.0"
}
if BuildTime == "" {
BuildTime = "not recorded"
}
}
// setAllConfig takes a FlagSet to be the definition of all configuration
// options, as well as their defaults. It then reads from the command line, the
// environment, and a config file (if specified), and applies the configuration

View file

@ -10,6 +10,7 @@ import (
"github.com/spf13/cobra"
"github.com/pilosa/pilosa"
"github.com/pilosa/pilosa/server"
)
@ -27,8 +28,7 @@ It will load existing data from the configured
directory, and start listening client connections
on the configured port.`,
RunE: func(cmd *cobra.Command, args []string) error {
Server.Server.Handler.Version = Version
fmt.Fprintf(Server.Stderr, "Pilosa %s, build time %s\n", Version, BuildTime)
fmt.Fprintf(Server.Stderr, "Pilosa %s, build time %s\n", pilosa.Version, pilosa.BuildTime)
// Start CPU profiling.
if Server.CPUProfile != "" {

View file

@ -39,9 +39,6 @@ type Handler struct {
Execute(context context.Context, db string, query *pql.Query, slices []uint64, opt *ExecOptions) ([]interface{}, error)
}
// The version to report on the /version endpoint.
Version string
// The writer for any logging.
LogOutput io.Writer
}
@ -1232,7 +1229,7 @@ func (h *Handler) handleGetVersion(w http.ResponseWriter, r *http.Request) {
if err := json.NewEncoder(w).Encode(struct {
Version string `json:"version"`
}{
Version: h.Version,
Version: Version,
}); err != nil {
h.logger().Printf("write version response error: %s", err)
}

17
version.go Normal file
View file

@ -0,0 +1,17 @@
package pilosa
var Version = "v0.0.0"
var BuildTime = "not recorded"
// ldflags works without this - removing it allows TestHandler_Version to work simply
func SetupVersionBuild() {
/*
if Version == "" {
Version = "v0.0.0"
}
if BuildTime == "" {
BuildTime = "not recorded"
}
*/
}