Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix GET /version for zed built by "go install" #4371

Merged
merged 1 commit into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ builds:
env:
- CGO_ENABLED=0
ldflags:
- -s -X github.com/brimdata/zed/cli.Version={{ .Tag }}
- -s -X github.com/brimdata/zed/cli.version={{ .Tag }}
goarch:
- amd64
- arm64
Expand All @@ -19,7 +19,7 @@ builds:
env:
- CGO_ENABLED=0
ldflags:
- -s -X github.com/brimdata/zed/cli.Version={{ .Tag }}
- -s -X github.com/brimdata/zed/cli.version={{ .Tag }}
goarch:
- amd64
- arm64
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export GO111MODULE=on

VERSION = $(shell git describe --tags --dirty --always)
LDFLAGS = -s -X github.com/brimdata/zed/cli.Version=$(VERSION)
LDFLAGS = -s -X github.com/brimdata/zed/cli.version=$(VERSION)
BUILD_COMMANDS = ./cmd/zed ./cmd/zq

# This enables a shortcut to run a single test from the ./ztests suite, e.g.:
Expand Down
14 changes: 1 addition & 13 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@ import (
"os"
"os/signal"
"runtime"
"runtime/debug"
"runtime/pprof"
"syscall"
)

// Version is set via the Go linker. See Makefile.
var Version string

type Flags struct {
showVersion bool
cpuprofile string
Expand All @@ -36,15 +32,7 @@ type Initializer interface {

func (f *Flags) Init(all ...Initializer) (context.Context, context.CancelFunc, error) {
if f.showVersion {
if Version == "" {
Version = "unknown"
if info, ok := debug.ReadBuildInfo(); ok {
// This will be "(devel)" for binaries not built
// by "go install PACKAGE@VERSION".
Version = info.Main.Version
}
}
fmt.Printf("Version: %s\n", Version)
fmt.Printf("Version: %s\n", Version())
os.Exit(0)
}
var err error
Expand Down
23 changes: 23 additions & 0 deletions cli/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cli

import "runtime/debug"

// version can be set by the linker.
var version string

// Version returns a version string. If the version variable in this package
// was set to a non-empty string by the linker, Version returns that.
// Otherwise, if build information is available via [debug.ReadBuildInfo],
// Version returns [debug.Buildinfo].Main.Version. Otherwise, Version returns
// "unknown".
func Version() string {
if version != "" {
return version
}
if info, ok := debug.ReadBuildInfo(); ok {
// This will be "(devel)" for binaries not built by
// "go install PACKAGE@VERSION".
return info.Main.Version
}
return "unknown"
}
2 changes: 1 addition & 1 deletion cmd/zed/serve/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type Command struct {
func New(parent charm.Command, f *flag.FlagSet) (charm.Command, error) {
c := &Command{Command: parent.(*root.Command)}
c.conf.Auth.SetFlags(f)
c.conf.Version = cli.Version
c.conf.Version = cli.Version()
c.logflags.SetFlags(f)
f.IntVar(&c.brimfd, "brimfd", -1, "pipe read fd passed by brim to signal brim closure")
f.Func("cors.origin", "CORS allowed origin (may be repeated)", func(s string) error {
Expand Down