diff --git a/buffalo/cmd/build/templates/main.go.tmpl b/buffalo/cmd/build/templates/main.go.tmpl index 8964d1f0f..24e6e3555 100644 --- a/buffalo/cmd/build/templates/main.go.tmpl +++ b/buffalo/cmd/build/templates/main.go.tmpl @@ -4,9 +4,12 @@ import ( "fmt" "log" "os" + "time" + "strings" "github.com/markbates/grift/grift" "github.com/gobuffalo/buffalo/buffalo/cmd" + "github.com/gobuffalo/buffalo/runtime" _ "<%= opts.PackagePkg %>/a" _ "<%= opts.ActionsPkg %>" <%= if (opts.WithPop) { %> @@ -19,13 +22,26 @@ import ( <% } %> ) -var BuildVersion = "unknown" -var BuildTime = "unknown" +var BuildVersion = "" +var BuildTime = "" + +func init() { + BuildTime = strings.Trim(BuildTime, "\"") + t, err := time.Parse("2006-01-02T15:04:05-07:00", BuildTime) + if err != nil { + fmt.Println(err) + } + runtime.SetBuild(runtime.BuildInfo{ + Version: BuildVersion, + Time: t, + }) +} func main() { args := os.Args if len(args) == 1 { originalMain() + return } c := args[1] switch c { @@ -53,7 +69,7 @@ func main() { } func printVersion() { - fmt.Printf("<%= opts.Name.Title() %> version %s (%s)\n", BuildVersion, BuildTime) + fmt.Printf("<%= opts.Name.Title() %> version %s\n", runtime.Build()) } <%= if (opts.WithPop) { %> diff --git a/buffalo/cmd/generate.go b/buffalo/cmd/generate.go index b676aa572..ad4cde56a 100644 --- a/buffalo/cmd/generate.go +++ b/buffalo/cmd/generate.go @@ -12,7 +12,6 @@ var generateCmd = &cobra.Command{ } func init() { - generate.Version = Version generateCmd.AddCommand(generate.ResourceCmd) generateCmd.AddCommand(generate.ActionCmd) generateCmd.AddCommand(generate.DockerCmd) diff --git a/buffalo/cmd/generate/docker.go b/buffalo/cmd/generate/docker.go index 7bae45489..f069b169c 100644 --- a/buffalo/cmd/generate/docker.go +++ b/buffalo/cmd/generate/docker.go @@ -2,6 +2,7 @@ package generate import ( "github.com/gobuffalo/buffalo/generators/docker" + "github.com/gobuffalo/buffalo/runtime" "github.com/gobuffalo/makr" "github.com/spf13/cobra" ) @@ -13,7 +14,7 @@ var DockerCmd = &cobra.Command{ Use: "docker", Short: "Generates a Dockerfile", RunE: func(cmd *cobra.Command, args []string) error { - dockerOptions.Version = Version + dockerOptions.Version = runtime.Version return dockerOptions.Run(".", makr.Data{}) }, } diff --git a/buffalo/cmd/generate/generate.go b/buffalo/cmd/generate/generate.go deleted file mode 100644 index 3772bb004..000000000 --- a/buffalo/cmd/generate/generate.go +++ /dev/null @@ -1,4 +0,0 @@ -package generate - -// Version is set by the cmd package, to all the generate package to have this information withouth cyclical deps -var Version string diff --git a/buffalo/cmd/info.go b/buffalo/cmd/info.go index 27d06fbc5..37ce7656b 100644 --- a/buffalo/cmd/info.go +++ b/buffalo/cmd/info.go @@ -7,6 +7,7 @@ import ( "reflect" "github.com/gobuffalo/buffalo/meta" + "github.com/gobuffalo/buffalo/runtime" "github.com/gobuffalo/envy" "github.com/pkg/errors" "github.com/spf13/cobra" @@ -19,7 +20,7 @@ var infoCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) error { bb := os.Stdout - bb.WriteString(fmt.Sprintf("### Buffalo Version\n%s\n", Version)) + bb.WriteString(fmt.Sprintf("### Buffalo Version\n%s\n", runtime.Version)) bb.WriteString("\n### App Information\n") app := meta.New(".") diff --git a/buffalo/cmd/new.go b/buffalo/cmd/new.go index b1b1c0fb3..7af558780 100644 --- a/buffalo/cmd/new.go +++ b/buffalo/cmd/new.go @@ -6,6 +6,7 @@ import ( "os/exec" "os/user" "path/filepath" + "runtime" "strings" "github.com/markbates/inflect" @@ -40,7 +41,6 @@ var newCmd = &cobra.Command{ } app.Name = inflect.Name(args[0]) - app.Version = Version if app.Name == "." { app.Name = inflect.Name(filepath.Base(app.Root)) @@ -69,7 +69,7 @@ var newCmd = &cobra.Command{ } data := makr.Data{ - "version": Version, + "version": runtime.Version, } if err := app.Run(app.Root, data); err != nil { return errors.WithStack(err) diff --git a/buffalo/cmd/update.go b/buffalo/cmd/update.go index 09f43ca1d..ce0f8f5d3 100644 --- a/buffalo/cmd/update.go +++ b/buffalo/cmd/update.go @@ -4,15 +4,15 @@ import ( "fmt" "github.com/gobuffalo/buffalo/buffalo/cmd/updater" + "github.com/gobuffalo/buffalo/runtime" "github.com/spf13/cobra" ) // updateCmd represents the info command var updateCmd = &cobra.Command{ Use: "update", - Short: fmt.Sprintf("will attempt to upgrade a Buffalo application to version %s", Version), + Short: fmt.Sprintf("will attempt to upgrade a Buffalo application to version %s", runtime.Version), RunE: func(cmd *cobra.Command, args []string) error { - updater.Version = Version return updater.Run() }, } diff --git a/buffalo/cmd/updater/runner.go b/buffalo/cmd/updater/runner.go index b80edcaa2..2ba3fb673 100644 --- a/buffalo/cmd/updater/runner.go +++ b/buffalo/cmd/updater/runner.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/gobuffalo/buffalo/meta" + "github.com/gobuffalo/buffalo/runtime" "github.com/pkg/errors" ) @@ -18,7 +19,7 @@ type Runner struct { // Run all compatible checks func Run() error { - fmt.Printf("! This updater will attempt to update your application to Buffalo version: %s\n", Version) + fmt.Printf("! This updater will attempt to update your application to Buffalo version: %s\n", runtime.Version) if !ask("Do you wish to continue?") { fmt.Println("~~~ cancelling update ~~~") return nil diff --git a/buffalo/cmd/updater/updater.go b/buffalo/cmd/updater/updater.go index 2d8b62b87..2a868447f 100644 --- a/buffalo/cmd/updater/updater.go +++ b/buffalo/cmd/updater/updater.go @@ -7,10 +7,6 @@ import ( "strings" ) -// Version is the current Buffalo version. It is set here by the cmd package. -// This is due to circular dependencies -var Version string - var replace = map[string]string{ "github.com/markbates/pop": "github.com/gobuffalo/pop", "github.com/markbates/validate": "github.com/gobuffalo/validate", diff --git a/buffalo/cmd/version.go b/buffalo/cmd/version.go index 49d4b2165..54146c9e5 100644 --- a/buffalo/cmd/version.go +++ b/buffalo/cmd/version.go @@ -1,14 +1,11 @@ package cmd import ( + "github.com/gobuffalo/buffalo/runtime" "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) -// Version is the current version of the buffalo binary -// const Version = "v0.11.0" -const Version = "development" - func init() { decorate("version", versionCmd) RootCmd.AddCommand(versionCmd) @@ -19,7 +16,7 @@ var versionCmd = &cobra.Command{ Short: "Print the version number of buffalo", Long: `All software has versions. This is buffalo's.`, Run: func(c *cobra.Command, args []string) { - logrus.Infof("Buffalo version is: %s\n", Version) + logrus.Infof("Buffalo version is: %s\n", runtime.Version) }, // needed to override the root level pre-run func PersistentPreRunE: func(c *cobra.Command, args []string) error { diff --git a/generators/docker/generator.go b/generators/docker/generator.go index 631d14342..63b2fff90 100644 --- a/generators/docker/generator.go +++ b/generators/docker/generator.go @@ -2,6 +2,7 @@ package docker import ( "github.com/gobuffalo/buffalo/meta" + "github.com/gobuffalo/buffalo/runtime" ) // Generator for generating a new docker file @@ -16,7 +17,7 @@ type Generator struct { func New() Generator { o := Generator{ App: meta.New("."), - Version: "latest", + Version: runtime.Version, Style: "multi", } o.AsWeb = o.App.WithWebpack diff --git a/generators/newapp/generator.go b/generators/newapp/generator.go index 5271611f0..64f43d72d 100644 --- a/generators/newapp/generator.go +++ b/generators/newapp/generator.go @@ -8,6 +8,7 @@ import ( "strings" "github.com/gobuffalo/buffalo/meta" + "github.com/gobuffalo/buffalo/runtime" "github.com/gobuffalo/envy" "github.com/gobuffalo/pop" "github.com/markbates/inflect" @@ -42,6 +43,7 @@ func New(name string) (Generator, error) { CIProvider: "none", AsWeb: true, Docker: "multi", + Version: runtime.Version, } g.Name = inflect.Name(name) diff --git a/generators/newapp/new.go b/generators/newapp/new.go index 0bf2a665e..f7c899dd0 100644 --- a/generators/newapp/new.go +++ b/generators/newapp/new.go @@ -124,7 +124,6 @@ func (a Generator) setupDocker(root string, data makr.Data) error { o := docker.New() o.App = a.App - o.Version = a.Version if err := o.Run(root, data); err != nil { return errors.WithStack(err) } diff --git a/grifts/release.go b/grifts/release.go index c64af29d3..dfde92660 100644 --- a/grifts/release.go +++ b/grifts/release.go @@ -109,7 +109,7 @@ func findVersion() (string, error) { if err != nil { return "", err } - vfile, err := ioutil.ReadFile(filepath.Join(pwd, "buffalo/cmd/version.go")) + vfile, err := ioutil.ReadFile(filepath.Join(pwd, "runtime/version.go")) if err != nil { return "", err } diff --git a/internal/vbuffalo/build.go b/internal/vbuffalo/build.go index e479e45cd..8ec6b2a48 100644 --- a/internal/vbuffalo/build.go +++ b/internal/vbuffalo/build.go @@ -3,7 +3,7 @@ package vbuffalo import ( "os" - "github.com/gobuffalo/buffalo/buffalo/cmd" + "github.com/gobuffalo/buffalo/runtime" "github.com/gobuffalo/plush" "github.com/pkg/errors" ) @@ -17,7 +17,7 @@ func writeMain() error { s, err := plush.Render(mainTemplate, plush.NewContextWith(map[string]interface{}{ "app": app, "cmdPkg": cmdPkg, - "version": cmd.Version, + "version": runtime.Version, })) if err != nil { return errors.WithStack(err) diff --git a/runtime/build.go b/runtime/build.go new file mode 100644 index 000000000..546c8a1d0 --- /dev/null +++ b/runtime/build.go @@ -0,0 +1,40 @@ +package runtime + +import ( + "fmt" + "sync" + "time" +) + +// BuildInfo holds information about the build +type BuildInfo struct { + Version string + Time time.Time +} + +// String implements fmt.String +func (b BuildInfo) String() string { + return fmt.Sprintf("%s (%s)", b.Version, b.Time) +} + +var build = BuildInfo{ + Version: "", + Time: time.Time{}, +} + +// Build returns the information about the current build +// of the application. In development mode this will almost +// always run zero values for BuildInfo. +func Build() BuildInfo { + return build +} + +var so sync.Once + +// SetBuild allows the setting of build information only once. +// This is typically managed by the binary built by `buffalo build`. +func SetBuild(b BuildInfo) { + so.Do(func() { + build = b + }) +} diff --git a/runtime/runtime.go b/runtime/runtime.go new file mode 100644 index 000000000..7ccdf5f69 --- /dev/null +++ b/runtime/runtime.go @@ -0,0 +1 @@ +package runtime diff --git a/runtime/version.go b/runtime/version.go new file mode 100644 index 000000000..c99fa786c --- /dev/null +++ b/runtime/version.go @@ -0,0 +1,5 @@ +package runtime + +// Version is the current version of the buffalo binary +// const Version = "v0.11.0" +const Version = "development"