From 2107e4f1c523208e6b42d7c0d2db2844bd5bd840 Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Fri, 20 Apr 2018 16:24:44 -0400 Subject: [PATCH 1/4] took a better approach to this problem --- buffalo/cmd/build/templates/main.go.tmpl | 24 ++++++++++---- buffalo/cmd/generate.go | 1 - buffalo/cmd/generate/docker.go | 3 +- buffalo/cmd/generate/generate.go | 4 --- buffalo/cmd/info.go | 3 +- buffalo/cmd/new.go | 4 +-- buffalo/cmd/update.go | 4 +-- buffalo/cmd/updater/runner.go | 3 +- buffalo/cmd/updater/updater.go | 4 --- buffalo/cmd/version.go | 28 ----------------- generators/docker/generator.go | 3 +- generators/newapp/generator.go | 1 - generators/newapp/new.go | 1 - grifts/release.go | 2 +- handler.go | 3 -- internal/vbuffalo/build.go | 4 +-- runtime/build.go | 40 ++++++++++++++++++++++++ runtime/runtime.go | 1 + runtime/version.go | 5 +++ 19 files changed, 79 insertions(+), 59 deletions(-) delete mode 100644 buffalo/cmd/generate/generate.go delete mode 100644 buffalo/cmd/version.go create mode 100644 runtime/build.go create mode 100644 runtime/runtime.go create mode 100644 runtime/version.go diff --git a/buffalo/cmd/build/templates/main.go.tmpl b/buffalo/cmd/build/templates/main.go.tmpl index 587e7aba1..b2456e81c 100644 --- a/buffalo/cmd/build/templates/main.go.tmpl +++ b/buffalo/cmd/build/templates/main.go.tmpl @@ -4,13 +4,15 @@ 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) { %> - "github.com/gobuffalo/envy" "github.com/gobuffalo/packr" "github.com/gobuffalo/pop" "<%= opts.ModelsPkg %>" @@ -20,12 +22,22 @@ 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() { - envy.MustSet("BUILD_VERSION", BuildVersion) - envy.MustSet("BUILD_TIME", BuildTime) args := os.Args if len(args) == 1 { originalMain() @@ -56,7 +68,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 deleted file mode 100644 index 49d4b2165..000000000 --- a/buffalo/cmd/version.go +++ /dev/null @@ -1,28 +0,0 @@ -package cmd - -import ( - "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) -} - -var versionCmd = &cobra.Command{ - Use: "version", - 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) - }, - // needed to override the root level pre-run func - PersistentPreRunE: func(c *cobra.Command, args []string) error { - return nil - }, -} 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..d040ad16f 100644 --- a/generators/newapp/generator.go +++ b/generators/newapp/generator.go @@ -20,7 +20,6 @@ var ErrNotInGoPath = errors.New("currently not in a $GOPATH") // Generator is the representation of a new Buffalo application type Generator struct { meta.App - Version string `json:"version"` Force bool `json:"force"` Verbose bool `json:"verbose"` DBType string `json:"db_type"` diff --git a/generators/newapp/new.go b/generators/newapp/new.go index 0a5ed5b81..9fb4b3312 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/handler.go b/handler.go index b18bdebd6..4ff9c5099 100644 --- a/handler.go +++ b/handler.go @@ -3,7 +3,6 @@ package buffalo import ( "net/http" - "github.com/gobuffalo/envy" "github.com/gobuffalo/x/httpx" gcontext "github.com/gorilla/context" "github.com/gorilla/mux" @@ -50,8 +49,6 @@ func (a *App) newContext(info RouteInfo, res http.ResponseWriter, req *http.Requ "current_path": req.URL.Path, "contentType": ct, "method": req.Method, - "BUILD_VERSION": envy.Get("BUILD_VERSION", "unknown"), - "BUILD_TIME": envy.Get("BUILD_TIME", "unknown"), } for _, route := range a.Routes() { 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" From 3e3535ae5e74c99edb9aeb1b1538f0dacc32d667 Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Fri, 20 Apr 2018 16:29:23 -0400 Subject: [PATCH 2/4] added back the accidentally deleted version command --- buffalo/cmd/version.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 buffalo/cmd/version.go diff --git a/buffalo/cmd/version.go b/buffalo/cmd/version.go new file mode 100644 index 000000000..54146c9e5 --- /dev/null +++ b/buffalo/cmd/version.go @@ -0,0 +1,25 @@ +package cmd + +import ( + "github.com/gobuffalo/buffalo/runtime" + "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +func init() { + decorate("version", versionCmd) + RootCmd.AddCommand(versionCmd) +} + +var versionCmd = &cobra.Command{ + Use: "version", + 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", runtime.Version) + }, + // needed to override the root level pre-run func + PersistentPreRunE: func(c *cobra.Command, args []string) error { + return nil + }, +} From 22c4d30773482c1859c59aaf9b689ed92c321c5d Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Fri, 20 Apr 2018 16:48:10 -0400 Subject: [PATCH 3/4] fixed broken tests --- generators/newapp/generator.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/generators/newapp/generator.go b/generators/newapp/generator.go index d040ad16f..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" @@ -20,6 +21,7 @@ var ErrNotInGoPath = errors.New("currently not in a $GOPATH") // Generator is the representation of a new Buffalo application type Generator struct { meta.App + Version string `json:"version"` Force bool `json:"force"` Verbose bool `json:"verbose"` DBType string `json:"db_type"` @@ -41,6 +43,7 @@ func New(name string) (Generator, error) { CIProvider: "none", AsWeb: true, Docker: "multi", + Version: runtime.Version, } g.Name = inflect.Name(name) From 0068e23ec5e05a6eaf653d0bc1ffe1ed71690ab8 Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Thu, 26 Apr 2018 16:07:58 -0400 Subject: [PATCH 4/4] fixed panic when cancelling --- buffalo/cmd/build/templates/main.go.tmpl | 1 + 1 file changed, 1 insertion(+) diff --git a/buffalo/cmd/build/templates/main.go.tmpl b/buffalo/cmd/build/templates/main.go.tmpl index b2456e81c..24e6e3555 100644 --- a/buffalo/cmd/build/templates/main.go.tmpl +++ b/buffalo/cmd/build/templates/main.go.tmpl @@ -41,6 +41,7 @@ func main() { args := os.Args if len(args) == 1 { originalMain() + return } c := args[1] switch c {