Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Make app version string available from inside app (v2) fixes #1035 #1037

Merged
merged 9 commits into from
Apr 26, 2018
22 changes: 19 additions & 3 deletions buffalo/cmd/build/templates/main.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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) { %>
Expand All @@ -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 {
Expand Down Expand Up @@ -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) { %>
Expand Down
1 change: 0 additions & 1 deletion buffalo/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion buffalo/cmd/generate/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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{})
},
}
Expand Down
4 changes: 0 additions & 4 deletions buffalo/cmd/generate/generate.go

This file was deleted.

3 changes: 2 additions & 1 deletion buffalo/cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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(".")
Expand Down
4 changes: 2 additions & 2 deletions buffalo/cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os/exec"
"os/user"
"path/filepath"
"runtime"
"strings"

"github.com/markbates/inflect"
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions buffalo/cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
},
}
Expand Down
3 changes: 2 additions & 1 deletion buffalo/cmd/updater/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/gobuffalo/buffalo/meta"
"github.com/gobuffalo/buffalo/runtime"
"github.com/pkg/errors"
)

Expand All @@ -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
Expand Down
4 changes: 0 additions & 4 deletions buffalo/cmd/updater/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 2 additions & 5 deletions buffalo/cmd/version.go
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion generators/docker/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package docker

import (
"github.com/gobuffalo/buffalo/meta"
"github.com/gobuffalo/buffalo/runtime"
)

// Generator for generating a new docker file
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions generators/newapp/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -42,6 +43,7 @@ func New(name string) (Generator, error) {
CIProvider: "none",
AsWeb: true,
Docker: "multi",
Version: runtime.Version,
}
g.Name = inflect.Name(name)

Expand Down
1 change: 0 additions & 1 deletion generators/newapp/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion grifts/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions internal/vbuffalo/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
Expand Down
40 changes: 40 additions & 0 deletions runtime/build.go
Original file line number Diff line number Diff line change
@@ -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
})
}
1 change: 1 addition & 0 deletions runtime/runtime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package runtime
5 changes: 5 additions & 0 deletions runtime/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package runtime

// Version is the current version of the buffalo binary
// const Version = "v0.11.0"
const Version = "development"