-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix circular import by moving injected build meta from flyctl to buil…
…dinfo
- Loading branch information
1 parent
bf3c713
commit 1f35171
Showing
17 changed files
with
261 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
Oops, something went wrong.