Skip to content

Commit

Permalink
cmd: make tilt binary compatible with goreleaser
Browse files Browse the repository at this point in the history
  • Loading branch information
nicks committed Oct 24, 2018
1 parent 535d348 commit 06e2186
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 22 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@
*.generated.yaml

.synclet-dev-image-tag
.#*
.#*

# goreleaser binaries
dist
23 changes: 23 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
builds:
- env:
- CGO_ENABLED=0
main: ./cmd/tilt/main.go
goos:
- linux
archive:
name_template: "{{ .ProjectName }}.{{ .Version }}.{{ .Os }}.{{ .Arch }}"
replacements:
darwin: mac
linux: linux
386: i386
amd64: x86_64
checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "{{ .Tag }}-next"
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,6 @@ synclet-release:
docker build -t $(SYNCLET_IMAGE):$(TAG) -f synclet/Dockerfile .
docker push $(SYNCLET_IMAGE):$(TAG)
sed -i 's/var SyncletTag = ".*"/var SyncletTag = "$(TAG)"/' internal/synclet/sidecar/sidecar.go

release:
goreleaser --rm-dist
9 changes: 9 additions & 0 deletions cmd/tilt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ import (
"github.com/windmilleng/tilt/internal/cli"
)

// Magic variables set by goreleaser
var version string
var commit string
var date string

func main() {
cli.SetBuildInfo(cli.BuildInfo{
Version: version,
Date: date,
})
cli.Execute()
}
63 changes: 63 additions & 0 deletions internal/cli/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package cli

import (
"fmt"
"os"
"strings"
)

type BuildInfo struct {
Version string
Date string
}

func (e BuildInfo) empty() bool {
return e == BuildInfo{}
}

var globalBuildInfo BuildInfo

func SetBuildInfo(info BuildInfo) {
globalBuildInfo = info
}

func buildStamp() string {
info := globalBuildInfo
if info.empty() {
info = defaultBuildInfo()
}
version := info.Version
date := info.Date
timeIndex := strings.Index(date, "T")
if timeIndex != -1 {
date = date[0:timeIndex]
}
return fmt.Sprintf("v%s, built %s", version, date)
}

// Returns a build datestamp in the format 2018-08-30
func defaultBuildDate() string {
// TODO(nick): Add a mechanism to encode the datestamp in the binary with
// ldflags. This currently only works if you are building your own
// binaries. It won't work once we're distributing pre-built binaries.
path, err := os.Executable()
if err != nil {
return "[unknown]"
}

info, err := os.Stat(path)
if err != nil {
return "[unknown]"
}

modTime := info.ModTime()
return modTime.Format("2006-01-02")
}

// Returns a build datestamp in the format 2018-08-30
func defaultBuildInfo() BuildInfo {
return BuildInfo{
Date: defaultBuildDate(),
Version: "0.0.0-dev",
}
}
20 changes: 1 addition & 19 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func Execute() {
addCommand(rootCmd, &downCmd{})
addCommand(rootCmd, &demoCmd{})
addCommand(rootCmd, &hudCmd{})
addCommand(rootCmd, &versionCmd{})
rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "Enable debug logging")
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Enable verbose logging")

Expand Down Expand Up @@ -123,22 +124,3 @@ func addCommand(parent *cobra.Command, child tiltCmd) {

parent.AddCommand(cobraChild)
}

// Returns a build datestamp in the format 2018-08-30
func buildDateStamp() string {
// TODO(nick): Add a mechanism to encode the datestamp in the binary with
// ldflags. This currently only works if you are building your own
// binaries. It won't work once we're distributing pre-built binaries.
path, err := os.Executable()
if err != nil {
return "[unknown]"
}

info, err := os.Stat(path)
if err != nil {
return "[unknown]"
}

modTime := info.ModTime()
return modTime.Format("2006-01-02")
}
2 changes: 1 addition & 1 deletion internal/cli/hud.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (c *hudCmd) run(ctx context.Context, args []string) error {
span.SetTag(k, v)
}

logOutput(fmt.Sprintf("Starting the HUD (built %s)…\n", buildDateStamp()))
logOutput(fmt.Sprintf("Starting the HUD (%s)…\n", buildStamp()))

err := client.ConnectHud(ctx)
if err == context.Canceled {
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (c *upCmd) run(ctx context.Context, args []string) error {
span.SetTag(k, v)
}

logOutput(fmt.Sprintf("Starting Tilt (built %s)…\n", buildDateStamp()))
logOutput(fmt.Sprintf("Starting Tilt (%s)…\n", buildStamp()))

if trace {
traceID, err := tracer.TraceID(ctx)
Expand Down
24 changes: 24 additions & 0 deletions internal/cli/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cli

import (
"context"
"fmt"

"github.com/spf13/cobra"
)

type versionCmd struct {
}

func (c *versionCmd) register() *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: "current Tilt version",
}
return cmd
}

func (c *versionCmd) run(ctx context.Context, args []string) error {
fmt.Println(buildStamp())
return nil
}

0 comments on commit 06e2186

Please sign in to comment.