-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add magefile, version command (fixes #2)
- Loading branch information
David Moles
committed
Feb 5, 2019
1 parent
a1e8081
commit 6642460
Showing
10 changed files
with
225 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var tag string | ||
var commitHash string | ||
var timestamp string | ||
|
||
func init() { | ||
cmd := &cobra.Command{ | ||
Use: "version", | ||
Short: "print cos version", | ||
Args: cobra.ExactArgs(0), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Println("cos " + versionStr()) | ||
}, | ||
} | ||
rootCmd.AddCommand(cmd) | ||
} | ||
|
||
func versionStr() string { | ||
// if these are blank, we were probably built with plain 'go build' or | ||
// 'go install', bypassing the ldflags in the magefile | ||
if tag == "" { | ||
return "(unknown version)" | ||
} | ||
versionStr := fmt.Sprintf("%v (%v, %v)", tag, commitHash, timestamp) | ||
return versionStr | ||
} |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
// +build mage | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"runtime" | ||
"strings" | ||
"time" | ||
|
||
"github.com/magefile/mage/mg" | ||
"github.com/magefile/mage/sh" | ||
) | ||
|
||
const ( | ||
appName = "cos" | ||
) | ||
|
||
var gocmd = mg.GoCmd() | ||
|
||
var oses = []string{"darwin", "linux", "windows"} | ||
var architectures = []string{"amd64"} | ||
|
||
// ------------------------------------------------------------ | ||
// Targets | ||
|
||
// Install installs cos in $GOPATH/bin. | ||
func Install() error { | ||
binName := appName | ||
if runtime.GOOS == "windows" { | ||
binName += ".exe" | ||
} | ||
|
||
gopath, err := sh.Output(gocmd, "env", "GOPATH") | ||
if err != nil { | ||
return fmt.Errorf("error determining GOPATH: %v", err) | ||
} | ||
binDir := filepath.Join(gopath, "bin") | ||
binPath := filepath.Join(binDir, binName) | ||
|
||
flags, err := ldFlags() | ||
if err != nil { | ||
return fmt.Errorf("error determining ldflags: %v", err) | ||
} | ||
return sh.RunV(gocmd, "build", "-o", binPath, "-ldflags", flags) | ||
} | ||
|
||
// Build builds a cos binary for the current platform. | ||
func Build() error { | ||
binName := appName | ||
if runtime.GOOS == "windows" { | ||
binName += ".exe" | ||
} | ||
flags, err := ldFlags() | ||
if err != nil { | ||
return fmt.Errorf("error determining ldflags: %v", err) | ||
} | ||
return sh.RunV(gocmd, "build", "-ldflags", flags) | ||
} | ||
|
||
// BuildAll builds a cos binary for each target platform. | ||
func BuildAll() error { | ||
for _, os_ := range oses { | ||
for _, arch := range architectures { | ||
binName := binNameFor(os_, arch) | ||
|
||
flags, err := ldFlags() | ||
if err != nil { | ||
return fmt.Errorf("error determining ldflags: %v", err) | ||
} | ||
|
||
env := map[string]string{ | ||
"GOOS": os_, | ||
"GOARCH": arch, | ||
} | ||
err = sh.RunWith(env, gocmd, "build", "-o", binName, "-ldflags", flags) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// Platforms lists target platforms for buildAll. | ||
func Platforms() { | ||
for _, os_ := range oses { | ||
for _, arch := range architectures { | ||
fmt.Printf("%s-%s\n", os_, arch) | ||
} | ||
} | ||
} | ||
|
||
// Clean removes compiled binaries from the current working directory. | ||
func Clean() error { | ||
var binRe = regexp.MustCompile("^" + appName + "(-[a-zA-Z0-9]+-[a-zA-Z0-9]+)?(.exe)?$") | ||
|
||
rmcmd := "rm" | ||
if runtime.GOOS == "windows" { | ||
rmcmd = "del" | ||
} | ||
|
||
files, err := ioutil.ReadDir("./") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, f := range files { | ||
mode := f.Mode() | ||
isPlainFile := mode.IsRegular() && mode&os.ModeSymlink == 0 | ||
isExecutable := mode&0111 != 0 | ||
if isPlainFile && isExecutable { | ||
name := f.Name() | ||
if binRe.MatchString(name) { | ||
err := sh.RunV(rmcmd, name) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// ------------------------------------------------------------ | ||
// Helper functions | ||
|
||
func ldFlags() (string, error) { | ||
commitHash, err := sh.Output("git", "rev-parse", "--short", "HEAD") | ||
if err != nil { | ||
return "", err | ||
} | ||
tag, err := sh.Output("git", "describe", "--tags") | ||
if err != nil { | ||
return "", err | ||
} | ||
timestamp := time.Now().Format(time.RFC3339) | ||
|
||
flagVals := map[string]string{ | ||
"commitHash": commitHash, | ||
"tag": tag, | ||
"timestamp": timestamp, | ||
} | ||
|
||
var flags []string | ||
for k, v := range flagVals { | ||
flag := fmt.Sprintf("-X github.com/dmolesUC3/cos/cmd.%s=%s", k, v) | ||
flags = append(flags, flag) | ||
} | ||
return strings.Join(flags, " "), nil | ||
} | ||
|
||
func binNameFor(os_ string, arch string) string { | ||
binName := appName | ||
binName = fmt.Sprintf("%s-%s-%s", binName, os_, arch) | ||
if os_ == "windows" { | ||
binName += ".exe" | ||
} | ||
return binName | ||
} |