refactor(Magefile): add build info in backend:build through ldflags

This commit is contained in:
Jordan Knott 2020-12-29 19:37:14 -06:00
parent 1bac555ebb
commit 90b92781d7
5 changed files with 80 additions and 12 deletions

2
go.mod
View File

@ -12,7 +12,7 @@ require (
github.com/jmoiron/sqlx v1.2.0 github.com/jmoiron/sqlx v1.2.0
github.com/lib/pq v1.3.0 github.com/lib/pq v1.3.0
github.com/lithammer/fuzzysearch v1.1.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/matcornic/hermes/v2 v2.1.0
github.com/pelletier/go-toml v1.8.0 // indirect github.com/pelletier/go-toml v1.8.0 // indirect
github.com/pkg/errors v0.9.1 github.com/pkg/errors v0.9.1

2
go.sum
View File

@ -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/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 h1:t3AU2wNwehMCW97vuqQLtw6puppWXHO+O2MHo5a50XE=
github.com/magefile/mage v1.9.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= 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 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= 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= github.com/markbates/oncer v0.0.0-20181203154359-bf2de49a0be2/go.mod h1:Ld9puTsIW75CHf65OeIOkyKbteujpZVXDpWK6YGZbxE=

View File

@ -5,6 +5,7 @@ import (
"net/http" "net/http"
"strings" "strings"
"github.com/jordanknott/taskcafe/internal/utils"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -12,22 +13,19 @@ import (
const mainDescription = `Taskcafé is an open soure project management const mainDescription = `Taskcafé is an open soure project management
system written in Golang & React.` system written in Golang & React.`
var ( func VersionTemplate() string {
version = "dev" info := utils.Version()
commit = "none" return fmt.Sprintf(`Version: %s
date = "unknown"
)
var versionTemplate = fmt.Sprintf(`Version: %s
Commit: %s Commit: %s
Built: %s`, version, commit, date+"\n") Built: %s`, info.Version, info.CommitHash, info.BuildDate+"\n")
}
var cfgFile string var cfgFile string
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
Use: "taskcafe", Use: "taskcafe",
Long: mainDescription, Long: mainDescription,
Version: version, Version: VersionTemplate(),
} }
var migration http.FileSystem var migration http.FileSystem
@ -86,7 +84,7 @@ func Execute() {
viper.SetDefault("queue.broker", "amqp://guest:guest@localhost:5672/") viper.SetDefault("queue.broker", "amqp://guest:guest@localhost:5672/")
viper.SetDefault("queue.store", "memcache://localhost:11211") viper.SetDefault("queue.store", "memcache://localhost:11211")
rootCmd.SetVersionTemplate(versionTemplate) rootCmd.SetVersionTemplate(VersionTemplate())
rootCmd.AddCommand(newWebCmd(), newMigrateCmd(), newTokenCmd(), newWorkerCmd(), newResetPasswordCmd()) rootCmd.AddCommand(newWebCmd(), newMigrateCmd(), newTokenCmd(), newWorkerCmd(), newResetPasswordCmd())
rootCmd.Execute() rootCmd.Execute()
} }

21
internal/utils/version.go Normal file
View File

@ -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,
}
}

View File

@ -8,12 +8,24 @@ import (
"net/http" "net/http"
"os" "os"
"strings" "strings"
"time"
"github.com/magefile/mage/mg" "github.com/magefile/mage/mg"
"github.com/magefile/mage/sh" "github.com/magefile/mage/sh"
"github.com/shurcooL/vfsgen" "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 // Aliases is a list of short names for often used commands
var Aliases = map[string]interface{}{ var Aliases = map[string]interface{}{
"s": Backend.Schema, "s": Backend.Schema,
@ -85,10 +97,25 @@ func (Backend) GenFrontend() error {
return nil 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 // Build the Go api service
func (Backend) Build() error { func (Backend) Build() error {
fmt.Println("compiling binary dist/taskcafe") 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 // Schema merges GraphQL schema files into single schema & runs gqlgen
@ -145,3 +172,23 @@ func (Docker) Up() error {
func (Docker) Migrate() error { func (Docker) Migrate() error {
return sh.RunV("docker-compose", "-p", "taskcafe", "-f", "docker-compose.yml", "-f", "docker-compose.migrate.yml", "run", "--rm", "migrate") 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
}