Skip to content

Commit 88bbecd

Browse files
authored
Merge pull request #89 from jakefhyde/update-magefiles
2 parents 308deef + 395a354 commit 88bbecd

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

magefiles/magefile.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package main
44

55
import (
66
"context"
7-
"fmt"
87
"log"
98
"os"
109
"runtime"
@@ -39,7 +38,7 @@ func Dependencies() error {
3938

4039
func Build(ctx context.Context) error {
4140
mg.Deps(Dependencies)
42-
return g.Build("main.go", fmt.Sprintf("dist/corral-%s-%s", g.OS, g.Arch))
41+
return g.Build()
4342
}
4443

4544
func Validate() error {

magetools/magetools.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,20 @@ func boolToIntString(b bool) string {
3333
return map[bool]string{true: "1", false: "0"}[b]
3434
}
3535

36-
func (g *Go) Build(target, output string) error {
36+
func (g *Go) Build() error {
3737
envs := map[string]string{"GOOS": g.OS, "GOARCH": g.Arch, "CGO_ENABLED": g.CGoEnabled, "MAGEFILE_VERBOSE": g.Verbose}
38-
return sh.RunWithV(envs, "go", "build", "-a", "-o", output, "--ldflags="+g.versionFlag(), target)
38+
return sh.RunWithV(envs, "go", "build", "-a", "-o", g.output(), "--ldflags="+g.versionFlag(), "main.go")
39+
}
40+
41+
func (g *Go) output() string {
42+
version := strings.Split(g.Version, "+")[0]
43+
prefix := fmt.Sprintf("dist/corral-%s", version)
44+
if g.OS == "windows" {
45+
return prefix + ".exe"
46+
} else if g.OS == "linux" {
47+
return prefix + "-" + g.Arch
48+
}
49+
return prefix + "-" + g.OS + "-" + g.Arch
3950
}
4051

4152
func (g *Go) Test(coverpkg string, targets ...string) error {
@@ -80,14 +91,22 @@ func (g *Go) Lint(targets ...string) error {
8091
}
8192

8293
func (g *Go) versionFlag() string {
83-
return fmt.Sprintf(`-X 'github.com/rancherlabs/corral/pkg/versionFlag.Version=%s'`, g.Version)
94+
return fmt.Sprintf(`-X 'github.com/rancherlabs/corral/pkg/version.Version=%s'`, g.Version)
8495
}
8596

97+
var ErrDirtyRepo = errors.New("encountered dirty repo")
98+
8699
func GetCommit() (string, error) {
87100
result, err := sh.Output("git", "rev-parse", "--short", "HEAD")
88101
if err != nil {
89102
return "", err
90103
}
104+
if err = IsGitClean(); err != nil {
105+
if !errors.Is(err, ErrDirtyRepo) {
106+
return "", err
107+
}
108+
result += "-dirty"
109+
}
91110
return strings.TrimSpace(result), nil
92111
}
93112

@@ -97,7 +116,7 @@ func IsGitClean() error {
97116
return err
98117
}
99118
if result != "" {
100-
return errors.New("encountered dirty repo")
119+
return ErrDirtyRepo
101120
}
102121
return nil
103122
}

0 commit comments

Comments
 (0)