Skip to content

Commit

Permalink
Build meta (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
eculver authored May 2, 2017
1 parent bdb802a commit ddd5dbc
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 7 deletions.
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,19 @@ fmt:
$(GOIMPORTS) -w $(ALL_SRC) ; \
fi

CLI_BUILD_VERSION=$(shell cat VERSION.txt)
CLI_BUILD_TIMESTAMP=$(shell date -u '+%Y-%m-%d_%I:%M:%S%p')
CLI_BUILD_REF=$(shell git rev-parse --short HEAD)
CLI_LINKER_FLAGS="-X main.version=$(CLI_BUILD_VERSION) -X main.timestamp=$(CLI_BUILD_TIMESTAMP) -X main.githash=$(CLI_BUILD_REF)"

.PHONY: cli
cli:
$(ECHO_V)go install ./cmd/dosa
$(ECHO_V)go build -ldflags $(CLI_LINKER_FLAGS) -o $$GOPATH/bin/dosa ./cmd/dosa
ifdef target
ifeq ($(target), Darwin)
$(ECHO_V)GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 go build -o ./out/cli/darwin/dosa ./cmd/dosa
$(ECHO_V)GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 go build -ldflags $(CLI_LINKER_FLAGS) -o ./out/cli/darwin/dosa ./cmd/dosa
else ifeq ($(target), Linux)
$(ECHO_V)GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o ./out/cli/linux/dosa ./cmd/dosa
$(ECHO_V)GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags $(CLI_LINKER_FLAGS) -o ./out/cli/linux/dosa ./cmd/dosa
endif
endif

Expand All @@ -111,3 +116,4 @@ mocks/connector.go:

.PHONY: mocks
mocks: mocks/client.go mocks/connector.go
python ./script/license-headers.py -t LICENSE.txt -d mocks
1 change: 1 addition & 0 deletions VERSION.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v2.0.0-rc1
43 changes: 41 additions & 2 deletions cmd/dosa/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,31 @@ type exiter func(int)

var exit = os.Exit

// these are overridden at build-time w/ the -ldflags -X option
var (
version = "0.0.0"
githash = "master"
timestamp = "now"
)

// BuildInfo reports information about the binary build environment
type BuildInfo struct {
Version string
Githash string
Timestamp string
}

// String satisfies Stringer interface
func (b BuildInfo) String() string {
return fmt.Sprintf("Version:\t%s\nGit Commit:\t%s\nUTC Build Time:\t%s", version, githash, timestamp)
}

// Execute is ran for the version subcommand
func (b BuildInfo) Execute(args []string) error {
fmt.Printf("%s\n", b)
return nil
}

// GlobalOptions are options for all subcommands
type GlobalOptions struct {
Host string `long:"host" default:"127.0.0.1" description:"The hostname or IP for the gateway."`
Expand All @@ -42,18 +67,25 @@ type GlobalOptions struct {
CallerName string `long:"caller" default:"dosacli-$USER" description:"Caller will override the default caller name (which is dosacli-$USER)."`
Timeout timeFlag `long:"timeout" default:"60s" description:"The timeout for gateway requests. E.g., 100ms, 0.5s, 1s. If no unit is specified, milliseconds are assumed."`
Connector string `hidden:"true" long:"connector" default:"yarpc" description:"Name of connector to use"`
Version bool `long:"version" description:"Display version info"`
}

var options GlobalOptions
var (
options GlobalOptions
buildInfo BuildInfo
)

// OptionsParser holds the global parser

func main() {
buildInfo := &BuildInfo{}
OptionsParser := flags.NewParser(&options, flags.PassAfterNonOption|flags.HelpFlag)
OptionsParser.ShortDescription = "DOSA CLI - The command-line tool for your DOSA client"
OptionsParser.LongDescription = `
dosa manages your schema both in production and development scopes`
c, _ := OptionsParser.AddCommand("scope", "commands to manage scope", "create, drop, or truncate development scopes", &ScopeOptions{})
c, _ := OptionsParser.AddCommand("version", "display build info", "display build info", &BuildInfo{})

c, _ = OptionsParser.AddCommand("scope", "commands to manage scope", "create, drop, or truncate development scopes", &ScopeOptions{})
_, _ = c.AddCommand("create", "Create scope", "creates a new scope", &ScopeCreate{})
_, _ = c.AddCommand("drop", "Drop scope", "drops a scope", &ScopeDrop{})
_, _ = c.AddCommand("truncate", "Truncate scope", "truncates a scope", &ScopeTruncate{})
Expand All @@ -65,6 +97,13 @@ dosa manages your schema both in production and development scopes`
_, _ = c.AddCommand("status", "Check schema status", "Check application status of schema", &SchemaStatus{})

_, err := OptionsParser.Parse()

if options.Version {
fmt.Fprintf(os.Stdout, "%s\n", buildInfo.String())
options.Version = false // for tests, we leak state between runs
exit(0)
}

if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
exit(1)
Expand Down
26 changes: 24 additions & 2 deletions cmd/dosa/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestNoSubcommand(t *testing.T) {
}
os.Args = []string{"dosa"}
main()
assert.Contains(t, c.stop(true), "schema or scope")
assert.Contains(t, c.stop(true), "schema, scope or version")
}

func TestMissingSubcommands(t *testing.T) {
Expand All @@ -50,7 +50,7 @@ func TestHostOptionButNothingElse(t *testing.T) {
exit = func(r int) {}
os.Args = []string{"dosa", "--host", "10.10.10.10"}
main()
assert.Contains(t, c.stop(true), "schema or scope")
assert.Contains(t, c.stop(true), "schema, scope or version")
}

// this test uses a trailing dot in the hostname to avoid multiple DNS lookups
Expand Down Expand Up @@ -86,6 +86,28 @@ func TestInvalidTransport(t *testing.T) {
assert.Contains(t, output, "invalid transport")
}

func TestVersionFlag(t *testing.T) {
c := StartCapture()
exit = func(r int) {}
os.Args = []string{"dosa", "--version"}
main()
output := c.stop(false)
assert.Contains(t, output, "Version:")
assert.Contains(t, output, "Git Commit:")
assert.Contains(t, output, "UTC Build Time:")
}

func TestVersionCommand(t *testing.T) {
c := StartCapture()
exit = func(r int) {}
os.Args = []string{"dosa", "version"}
main()
output := c.stop(false)
assert.Contains(t, output, "Version:")
assert.Contains(t, output, "Git Commit:")
assert.Contains(t, output, "UTC Build Time:")
}

/* TODO: implement these integration test cases
// expect: top-level usage, transport=tchannel
Expand Down
1 change: 1 addition & 0 deletions mocks/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ package mocks

import (
context "context"

gomock "github.com/golang/mock/gomock"
dosa "github.com/uber-go/dosa"
)
Expand Down
1 change: 1 addition & 0 deletions mocks/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ package mocks

import (
context "context"

gomock "github.com/golang/mock/gomock"
dosa "github.com/uber-go/dosa"
)
Expand Down
36 changes: 36 additions & 0 deletions mocks/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

// Package mocks is the Mocks generated by mockgen..
//
// These are the commands used to generate the mocks found here:
//
// mocks/client.go:
//
// mockgen -package mocks github.com/uber-go/dosa Client,AdminClient > mocks/client.go
//
// mocks/connector.go:
//
// mockgen -package mocks github.com/uber-go/dosa Connector > mocks/connector.go
//
// OR just run make mocks
//
//
package mocks

0 comments on commit ddd5dbc

Please sign in to comment.