-
Notifications
You must be signed in to change notification settings - Fork 0
/
magefile.go
163 lines (146 loc) · 3.74 KB
/
magefile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// +build mage
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"github.com/magefile/mage/mg" // mg contains helpful utility functions, like Deps
"github.com/magefile/mage/sh"
)
var (
// Default target to run when none is specified
// If not set, running mage will list available targets
Default = Install
appName = "jt"
installPath string
currentWorkDir string
// allow user to override go executable by running as GOEXE=xxx make ... on unix-like systems
goexe = "go"
)
// A build step that requires additional params, or platform specific steps for example
func Build() error {
fmt.Println("Building...")
cmd := exec.Command("go", "build", "-o", appName, currentWorkDir)
return cmd.Run()
}
// A custom install step if you need your bin someplace other than go/bin
func Install() error {
mg.Deps(Build)
fmt.Println("Installing...")
return os.Rename(filepath.Join(currentWorkDir, "jt"), installPath)
}
// Clean up after yourself
func Clean() {
fmt.Println("Cleaning...")
os.RemoveAll(appName)
}
// Fmt - Run gofmt linters
func Fmt() error {
err := sh.Run("goimports", "-l", "-w", "-local", "github.com/Khan", ".")
if err != nil {
fmt.Printf("ERROR: running goimports: %v\n", err)
return err
}
err = sh.Run("go", "run", "mvdan.cc/gofumpt", "-s", "-l", "-w", ".")
if err != nil {
fmt.Printf("ERROR: running gofumpt: %v\n", err)
return err
}
if commandExists("gofumpt") {
err = sh.Run(
"go",
"run",
"github.com/segmentio/golines",
//"golines",
"--shorten-comments",
"--base-formatter=gofumpt",
"-w",
".",
)
} else {
err = sh.Run(
"go",
"run",
"github.com/segmentio/golines",
"--shorten-comments",
"-w",
".",
)
}
if err != nil {
fmt.Printf("ERROR: running golines: %v\n", err)
return err
}
return nil
}
var releaseTag = regexp.MustCompile(`^v[0-9]+\.[0-9]+\.[0-9]+$`)
// Generates a new release. Expects a version tag in vx.x.x format.
// really only useful for maintainers
func Release(tag string) (err error) {
if !releaseTag.MatchString(tag) {
return errors.New(
"TAG environment variable must be in semver vx.x.x format, but was " + tag,
)
}
if err := sh.RunV("git", "tag", "-a", tag, "-m", tag); err != nil {
return err
}
if err := sh.RunV("git", "push", "origin", tag); err != nil {
return err
}
defer func() {
if err != nil {
sh.RunV("git", "tag", "--delete", "$TAG")
sh.RunV("git", "push", "--delete", "origin", "$TAG")
}
}()
return sh.RunV("goreleaser", "--rm-dist")
}
// tag returns the git tag for the current branch or "" if none.
func tag() string {
s, _ := sh.Output("git", "describe", "--tags")
return s
}
// hash returns the git hash for the current repo or "" if none.
func hash() string {
hash, _ := sh.Output("git", "rev-parse", "--short", "HEAD")
return hash
}
func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}
// this is probably overkill, but in case they are not Go developers
func init() {
if exe := os.Getenv("GOEXE"); exe != "" {
goexe = exe
}
// We want to use Go 1.11 modules even if the source lives inside GOPATH.
// The default is "auto".
os.Setenv("GO111MODULE", "on")
os.Setenv("APP", appName)
homeDir, err := os.UserHomeDir()
if err != nil {
panic(err)
}
currentWorkDir, err = os.Getwd()
if err != nil {
panic(err)
}
goBinDir := getEnv("GOBIN", fmt.Sprintf("%s/go/bin", homeDir))
os.Setenv("GOBIN", goBinDir)
// mkdir -p ${HOME}/go/bin
err = os.MkdirAll(goBinDir, 0o755)
if err != nil {
panic(err)
}
installPath = fmt.Sprintf("%s/%s", goBinDir, appName)
os.Setenv("INSTALLPATH", installPath)
os.Setenv("PATH", fmt.Sprintf("%s:%s", goBinDir, os.Getenv("PATH")))
os.Setenv("GOPRIVATE", "github.com/Khan")
}