-
Notifications
You must be signed in to change notification settings - Fork 24
/
magefile.go
119 lines (95 loc) · 3.1 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
// Copyright (c) Contributors to the Apptainer project, established as
// Apptainer a Series of LF Projects LLC.
// For website terms of use, trademark policy, privacy policy and other
// project policies see https://lfprojects.org/policies
// Copyright (c) 2021-2022, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE file distributed with the sources of this project regarding your
// rights to use or distribute this software.
//go:build mage
package main
import (
"fmt"
"os"
"strings"
"time"
"github.com/apptainer/sif/v2/internal/pkg/git"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
// Aliases defines command-line aliases exposed by Mage.
//nolint:deadcode
var Aliases = map[string]interface{}{
"build": Build.All,
"cover": Cover.All,
"install": Install.All,
"test": Test.All,
}
// env returns the environment to use when running Go commands.
func env() map[string]string {
return map[string]string{"CGO_ENABLED": "0"}
}
// ldFlags returns linker flags to pass to various Go commands.
func ldFlags() string {
vals := []string{"-s", "-w", "-X", "main.builtBy=mage"}
// Attempt to get git details.
if d, err := git.Describe("."); err == nil {
vals = append(vals, "-X", fmt.Sprintf("main.commit=%v", d.CommitHash()))
if d.IsClean() {
vals = append(vals,
"-X", fmt.Sprintf("main.date=%v", d.CommitTime().UTC().Format(time.RFC3339)),
"-X", "main.state=clean",
)
} else {
vals = append(vals,
"-X", fmt.Sprintf("main.date=%v", time.Now().UTC().Format(time.RFC3339)),
"-X", "main.state=dirty",
)
}
if v, err := d.Version(); err == nil {
vals = append(vals, "-X", fmt.Sprintf("main.version=%v", v))
} else {
fmt.Fprintf(os.Stderr, "warning: failed to get version: %v\n", err)
}
} else {
fmt.Fprintf(os.Stderr, "warning: failed to describe git HEAD: %v\n", err)
vals = append(vals, "-X", fmt.Sprintf("main.date=%v", time.Now().UTC().Format(time.RFC3339)))
}
return strings.Join(vals, " ")
}
type Build mg.Namespace
// All compiles all assets.
func (ns Build) All() {
mg.Deps(ns.Source)
}
// Source compiles all source code.
func (Build) Source() error {
return sh.RunWith(env(), mg.GoCmd(), "build", "-trimpath", "-ldflags", ldFlags(), "./...")
}
type Install mg.Namespace
// All installs all assets.
func (ns Install) All() {
mg.Deps(ns.Bin)
}
// Bin installs binary to GOBIN.
func (Install) Bin() error {
return sh.RunWith(env(), mg.GoCmd(), "install", "-trimpath", "-ldflags", ldFlags(), "./cmd/siftool")
}
type Test mg.Namespace
// All runs all tests.
func (ns Test) All() {
mg.Deps(ns.Unit)
}
// Unit runs all unit tests.
func (Test) Unit() error {
return sh.RunV(mg.GoCmd(), "test", "-race", "-cover", "./...")
}
type Cover mg.Namespace
// All runs all tests, writing coverage profile to the specified path.
func (ns Cover) All(path string) {
mg.Deps(mg.F(ns.Unit, path))
}
// Unit runs all unit tests, writing coverage profile to the specified path.
func (Cover) Unit(path string) error {
return sh.RunV(mg.GoCmd(), "test", "-race", "-coverprofile", path, "./...")
}