From 90b92781d7c09ae79f6d624d666c48b35c751777 Mon Sep 17 00:00:00 2001 From: Jordan Knott Date: Tue, 29 Dec 2020 19:37:14 -0600 Subject: [PATCH] refactor(Magefile): add build info in backend:build through ldflags --- go.mod | 2 +- go.sum | 2 ++ internal/commands/commands.go | 18 ++++++------- internal/utils/version.go | 21 +++++++++++++++ magefile.go | 49 ++++++++++++++++++++++++++++++++++- 5 files changed, 80 insertions(+), 12 deletions(-) create mode 100644 internal/utils/version.go diff --git a/go.mod b/go.mod index ff9db77..c7a4d33 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( github.com/jmoiron/sqlx v1.2.0 github.com/lib/pq v1.3.0 github.com/lithammer/fuzzysearch v1.1.0 - github.com/magefile/mage v1.9.0 + github.com/magefile/mage v1.11.0 github.com/matcornic/hermes/v2 v2.1.0 github.com/pelletier/go-toml v1.8.0 // indirect github.com/pkg/errors v0.9.1 diff --git a/go.sum b/go.sum index f46e412..d0a463b 100644 --- a/go.sum +++ b/go.sum @@ -383,6 +383,8 @@ github.com/lithammer/fuzzysearch v1.1.0/go.mod h1:Bqx4wo8lTOFcJr3ckpY6HA9lEIOO0H github.com/logrusorgru/aurora v0.0.0-20200102142835-e9ef32dff381/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4= github.com/magefile/mage v1.9.0 h1:t3AU2wNwehMCW97vuqQLtw6puppWXHO+O2MHo5a50XE= github.com/magefile/mage v1.9.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= +github.com/magefile/mage v1.11.0 h1:C/55Ywp9BpgVVclD3lRnSYCwXTYxmSppIgLeDYlNuls= +github.com/magefile/mage v1.11.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/markbates/oncer v0.0.0-20181203154359-bf2de49a0be2/go.mod h1:Ld9puTsIW75CHf65OeIOkyKbteujpZVXDpWK6YGZbxE= diff --git a/internal/commands/commands.go b/internal/commands/commands.go index b7cf7b0..be45364 100644 --- a/internal/commands/commands.go +++ b/internal/commands/commands.go @@ -5,6 +5,7 @@ import ( "net/http" "strings" + "github.com/jordanknott/taskcafe/internal/utils" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -12,22 +13,19 @@ import ( const mainDescription = `Taskcafé is an open soure project management system written in Golang & React.` -var ( - version = "dev" - commit = "none" - date = "unknown" -) - -var versionTemplate = fmt.Sprintf(`Version: %s +func VersionTemplate() string { + info := utils.Version() + return fmt.Sprintf(`Version: %s Commit: %s -Built: %s`, version, commit, date+"\n") +Built: %s`, info.Version, info.CommitHash, info.BuildDate+"\n") +} var cfgFile string var rootCmd = &cobra.Command{ Use: "taskcafe", Long: mainDescription, - Version: version, + Version: VersionTemplate(), } var migration http.FileSystem @@ -86,7 +84,7 @@ func Execute() { viper.SetDefault("queue.broker", "amqp://guest:guest@localhost:5672/") viper.SetDefault("queue.store", "memcache://localhost:11211") - rootCmd.SetVersionTemplate(versionTemplate) + rootCmd.SetVersionTemplate(VersionTemplate()) rootCmd.AddCommand(newWebCmd(), newMigrateCmd(), newTokenCmd(), newWorkerCmd(), newResetPasswordCmd()) rootCmd.Execute() } diff --git a/internal/utils/version.go b/internal/utils/version.go new file mode 100644 index 0000000..b603576 --- /dev/null +++ b/internal/utils/version.go @@ -0,0 +1,21 @@ +package utils + +var ( + version = "dev" + commitHash = "none" + buildDate = "unknown" +) + +type Info struct { + Version string + CommitHash string + BuildDate string +} + +func Version() Info { + return Info{ + Version: version, + CommitHash: commitHash, + BuildDate: buildDate, + } +} diff --git a/magefile.go b/magefile.go index 24845db..bda4469 100644 --- a/magefile.go +++ b/magefile.go @@ -8,12 +8,24 @@ import ( "net/http" "os" "strings" + "time" "github.com/magefile/mage/mg" "github.com/magefile/mage/sh" "github.com/shurcooL/vfsgen" ) +const ( + packageName = "github.com/jordanknott/taskcafe" +) + +var ldflags = "-X $PACKAGE/internal/utils.commitHash=$COMMIT_HASH -X $PACKAGE/internal/utils.buildDate=$BUILD_DATE -X $PACKAGE/internal/utils.version=$VERSION" + +func runWith(env map[string]string, cmd string, inArgs ...interface{}) error { + s := argsToStrings(inArgs...) + return sh.RunWith(env, cmd, s...) +} + // Aliases is a list of short names for often used commands var Aliases = map[string]interface{}{ "s": Backend.Schema, @@ -85,10 +97,25 @@ func (Backend) GenFrontend() error { return nil } +func flagEnv() map[string]string { + hash, _ := sh.Output("git", "rev-parse", "--short", "HEAD") + fmt.Println("[ignore] fatal: no tag matches") + tag, err := sh.Output("git", "describe", "--exact-match", "--tags") + if err != nil { + tag = "nightly" + } + return map[string]string{ + "PACKAGE": packageName, + "COMMIT_HASH": hash, + "BUILD_DATE": time.Now().Format("2006-01-02T15:04:05Z0700"), + "VERSION": tag, + } +} + // Build the Go api service func (Backend) Build() error { fmt.Println("compiling binary dist/taskcafe") - return sh.Run("go", "build", "-tags", "prod", "-o", "dist/taskcafe", "cmd/taskcafe/main.go") + return runWith(flagEnv(), "go", "build", "-ldflags", ldflags, "-tags", "prod", "-o", "dist/taskcafe", "cmd/taskcafe/main.go") } // Schema merges GraphQL schema files into single schema & runs gqlgen @@ -145,3 +172,23 @@ func (Docker) Up() error { func (Docker) Migrate() error { return sh.RunV("docker-compose", "-p", "taskcafe", "-f", "docker-compose.yml", "-f", "docker-compose.migrate.yml", "run", "--rm", "migrate") } + +func argsToStrings(v ...interface{}) []string { + var args []string + for _, arg := range v { + switch v := arg.(type) { + case string: + if v != "" { + args = append(args, v) + } + case []string: + if v != nil { + args = append(args, v...) + } + default: + panic("invalid type") + } + } + + return args +}