From 83e13bec747e5d4c06c85ed6b77cb491b1c3f0ce Mon Sep 17 00:00:00 2001 From: Alan Bernstein Date: Fri, 21 Apr 2017 10:14:21 -0500 Subject: [PATCH] Move version vars to pilosa package --- Makefile | 2 +- cmd/root.go | 21 ++++----------------- cmd/server.go | 4 ++-- handler.go | 5 +---- version.go | 17 +++++++++++++++++ 5 files changed, 25 insertions(+), 24 deletions(-) create mode 100644 version.go diff --git a/Makefile b/Makefile index 78c3e71f6..6641541fd 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/cmd/root.go b/cmd/root.go index 7cee0e77f..a918b0841 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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 diff --git a/cmd/server.go b/cmd/server.go index 4e5d96114..d9863c47a 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -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 != "" { diff --git a/handler.go b/handler.go index be49a032f..014afea17 100644 --- a/handler.go +++ b/handler.go @@ -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) } diff --git a/version.go b/version.go new file mode 100644 index 000000000..42129f64a --- /dev/null +++ b/version.go @@ -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" + } + */ + +}