Skip to content

Commit

Permalink
fix circular import by moving injected build meta from flyctl to buil…
Browse files Browse the repository at this point in the history
…dinfo
  • Loading branch information
michaeldwan committed Aug 20, 2021
1 parent bf3c713 commit 1f35171
Show file tree
Hide file tree
Showing 17 changed files with 261 additions and 100 deletions.
17 changes: 8 additions & 9 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ builds:
- goos: linux
goarch: 386
ldflags:
- -X github.com/superfly/flyctl/flyctl.BuildDate={{ .Date }}
- -X github.com/superfly/flyctl/flyctl.Version={{ .Version }}
- -X github.com/superfly/flyctl/flyctl.Commit={{ .ShortCommit }}
- -X github.com/superfly/flyctl/flyctl.Environment=production
- -X github.com/superfly/flyctl/internal/buildinfo.environment=production
- -X github.com/superfly/flyctl/internal/buildinfo.buildDate={{ .Date }}
- -X github.com/superfly/flyctl/internal/buildinfo.version={{ .Version }}
- -X github.com/superfly/flyctl/internal/buildinfo.commit={{ .ShortCommit }}
- id: windows
env:
- CGO_ENABLED=0
Expand All @@ -29,11 +29,10 @@ builds:
- goos: windows
goarch: 386
ldflags:
- -X github.com/superfly/flyctl/flyctl.BuildDate={{ .Date }}
- -X github.com/superfly/flyctl/flyctl.Version={{ .Version }}
- -X github.com/superfly/flyctl/flyctl.Commit={{ .ShortCommit }}
- -X github.com/superfly/flyctl/flyctl.Environment=production

- -X github.com/superfly/flyctl/internal/buildinfo.environment=production
- -X github.com/superfly/flyctl/internal/buildinfo.buildDate={{ .Date }}
- -X github.com/superfly/flyctl/internal/buildinfo.version={{ .Version }}
- -X github.com/superfly/flyctl/internal/buildinfo.commit={{ .ShortCommit }}

archives:
- id: windows
Expand Down
5 changes: 2 additions & 3 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"strings"

"github.com/machinebox/graphql"
"github.com/superfly/flyctl/flyname"
)

var baseURL string
Expand All @@ -37,14 +36,14 @@ type Client struct {
}

// NewClient - creates a new Client, takes an access token
func NewClient(accessToken string, version string) *Client {
func NewClient(accessToken string, name, version string) *Client {

httpClient, _ := newHTTPClient()

url := fmt.Sprintf("%s/graphql", baseURL)

client := graphql.NewClient(url, graphql.WithHTTPClient(httpClient))
userAgent := fmt.Sprintf("%s/%s", flyname.Name(), version)
userAgent := fmt.Sprintf("%s/%s", name, version)
return &Client{httpClient, client, accessToken, userAgent}
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
"github.com/superfly/flyctl/cmdctx"
"github.com/superfly/flyctl/docstrings"
"github.com/superfly/flyctl/flyctl"
"github.com/superfly/flyctl/flyname"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/superfly/flyctl/helpers"
"github.com/superfly/flyctl/internal/buildinfo"
"github.com/superfly/flyctl/internal/client"
"github.com/superfly/flyctl/internal/flyerr"
"github.com/superfly/flyctl/terminal"
Expand Down Expand Up @@ -297,7 +297,7 @@ func requireAppName(cmd *Command) Initializer {
},
PreRun: func(ctx *cmdctx.CmdContext) error {
if ctx.AppName == "" {
return fmt.Errorf("No app specified. Specify an app or create an app with '" + flyname.Name() + " init'")
return fmt.Errorf("No app specified. Specify an app or create an app with '" + buildinfo.Name() + " init'")
}

if ctx.AppConfig == nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"github.com/superfly/flyctl/cmdctx"
"github.com/superfly/flyctl/flyname"
"github.com/superfly/flyctl/internal/buildinfo"
"github.com/superfly/flyctl/internal/client"

"github.com/superfly/flyctl/docstrings"
Expand Down Expand Up @@ -61,7 +61,7 @@ func runInfo(ctx *cmdctx.CmdContext) error {
}

if !app.Deployed {
ctx.Status("info", `App has not been deployed yet. Try running "`+flyname.Name()+` deploy --image flyio/hellofly"`)
ctx.Status("info", `App has not been deployed yet. Try running "`+buildinfo.Name()+` deploy --image flyio/hellofly"`)
}
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions cmd/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"

"github.com/superfly/flyctl/cmdctx"
"github.com/superfly/flyctl/flyname"
"github.com/superfly/flyctl/internal/buildinfo"
"github.com/superfly/flyctl/internal/client"

"github.com/superfly/flyctl/docstrings"
Expand Down Expand Up @@ -35,7 +35,7 @@ func runOpen(ctx *cmdctx.CmdContext) error {
}

if !app.Deployed {
fmt.Println(`App has not been deployed yet. Try running "` + flyname.Name() + ` deploy --image flyio/hellofly"`)
fmt.Println(`App has not been deployed yet. Try running "` + buildinfo.Name() + ` deploy --image flyio/hellofly"`)
return nil
}

Expand Down
19 changes: 6 additions & 13 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
"context"
"fmt"
"path/filepath"
"runtime"

"github.com/spf13/cobra"
"github.com/superfly/flyctl/cmdctx"
"github.com/superfly/flyctl/flyctl"
"github.com/superfly/flyctl/flyname"
"github.com/superfly/flyctl/internal/buildinfo"
"github.com/superfly/flyctl/internal/client"
"github.com/superfly/flyctl/internal/update"

Expand Down Expand Up @@ -44,18 +43,12 @@ func runVersion(ctx *cmdctx.CmdContext) error {
update.InitState(stateFilePath, saveInstall)
}

info := buildinfo.Info()

if ctx.OutputJSON() {
type flyctlBuild struct {
Name string
Version string
Commit string
BuildDate string
OS string
Architecture string
}
ctx.WriteJSON(flyctlBuild{Name: flyname.Name(), Version: flyctl.Version, Commit: flyctl.Commit, BuildDate: flyctl.BuildDate, OS: runtime.GOOS, Architecture: runtime.GOARCH})
ctx.WriteJSON(info)
} else {
fmt.Printf("%s v%s %s/%s Commit: %s BuildDate: %s\n", flyname.Name(), flyctl.Version, runtime.GOOS, runtime.GOARCH, flyctl.Commit, flyctl.BuildDate)
fmt.Println(info)
}

return nil
Expand All @@ -68,5 +61,5 @@ func runInitState(ctx *cmdctx.CmdContext) error {

func runUpdate(ctx *cmdctx.CmdContext) error {
stateFilePath := filepath.Join(flyctl.ConfigDir(), "state.yml")
return update.PerformInPlaceUpgrade(context.TODO(), stateFilePath, flyctl.Version)
return update.PerformInPlaceUpgrade(context.TODO(), stateFilePath, buildinfo.Version())
}
6 changes: 0 additions & 6 deletions flyctl/version.go

This file was deleted.

29 changes: 0 additions & 29 deletions flyname/flyname.go

This file was deleted.

69 changes: 69 additions & 0 deletions internal/buildinfo/buildinfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package buildinfo

import (
"fmt"
"os"
"path/filepath"
"runtime"
"time"

"github.com/blang/semver"
)

var cachedName string // populated during init

func init() {
var err error
if cachedName, err = os.Executable(); err != nil {
panic(err)
}
cachedName = filepath.Base(cachedName)
}

// Name returns the name for the executable that started the current
// process.
//
// Name is safe for concurrent use.
func Name() string {
return cachedName
}

type info struct {
Name string
Version semver.Version
Commit string
BuildDate time.Time
OS string
Architecture string
Environment string
}

func (i info) String() string {
return fmt.Sprintf("%s v%s %s/%s Commit: %s BuildDate: %s",
i.Name,
i.Version,
i.OS,
i.Architecture,
i.Commit,
i.BuildDate.Format(time.RFC3339))
}

func Info() info {
return info{
Name: Name(),
Version: Version(),
Commit: Commit(),
BuildDate: BuildDate(),
OS: OS(),
Architecture: Arch(),
Environment: Environment(),
}
}

func OS() string {
return runtime.GOOS
}

func Arch() string {
return runtime.GOARCH
}
15 changes: 15 additions & 0 deletions internal/buildinfo/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package buildinfo

var environment = "development"

func Environment() string {
return environment
}

func IsDev() bool {
return environment == "development"
}

func IsRelease() bool {
return !IsDev()
}
79 changes: 79 additions & 0 deletions internal/buildinfo/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package buildinfo

import (
"time"

"github.com/blang/semver"
"github.com/superfly/flyctl/terminal"
)

var buildDate = "<date>"
var version = "<version>"
var commit = "<commit>"

var parsedVersion semver.Version
var parsedBuildDate time.Time

func init() {
loadMeta(time.Now())
}

func loadMeta(now time.Time) {
var err error

if IsDev() {
parsedBuildDate = now.UTC()

parsedVersion = semver.Version{
Pre: []semver.PRVersion{
{
VersionNum: uint64(parsedBuildDate.Unix()),
IsNum: true,
},
},
Build: []string{"dev"},
}

} else {
parsedBuildDate, err = time.Parse(time.RFC3339, buildDate)
if err != nil {
panic(err)
}
parsedBuildDate = parsedBuildDate.UTC()

parsedVersion = semver.MustParse(version)
}
}

func Commit() string {
return commit
}

func Version() semver.Version {
return parsedVersion
}

func BuildDate() time.Time {
return parsedBuildDate
}

func parseVesion(v string) semver.Version {
parsedV, err := semver.ParseTolerant(v)
if err != nil {
terminal.Warnf("error parsing version number '%s': %s\n", v, err)
return semver.Version{}
}
return parsedV
}

func IsVersionSame(otherVerison string) bool {
return parsedVersion.EQ(parseVesion(otherVerison))
}

func IsVersionOlder(otherVerison string) bool {
return parsedVersion.LT(parseVesion(otherVerison))
}

func IsVersionNewer(otherVerison string) bool {
return parsedVersion.GT(parseVesion(otherVerison))
}
34 changes: 34 additions & 0 deletions internal/buildinfo/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package buildinfo

import (
"fmt"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestProdMeta(t *testing.T) {
version = "1.2.3"
buildDate = "2020-06-05T13:32:23Z"
commit = "c8f7b8f"

loadMeta(time.Now())

assert.Equal(t, "1.2.3", Version().String())
assert.Equal(t, "2020-06-05T13:32:23Z", BuildDate().Format(time.RFC3339))
assert.Equal(t, "c8f7b8f", Commit())
}

func TestDevMeta(t *testing.T) {
version = "<version>"
buildDate = "<date>"
commit = "<commit>"

now := time.Now()
loadMeta(now)

assert.Equal(t, fmt.Sprintf("0.0.0-%d+dev", now.Unix()), Version().String())
assert.Equal(t, now, BuildDate())
assert.Equal(t, "<commit>", Commit())
}

0 comments on commit 1f35171

Please sign in to comment.