|
| 1 | +// +build mage |
| 2 | + |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "bufio" |
| 7 | + "crypto/sha1" |
| 8 | + "encoding/hex" |
| 9 | + "fmt" |
| 10 | + "github.com/magefile/mage/mg" |
| 11 | + "github.com/magefile/mage/sh" |
| 12 | + "io" |
| 13 | + "io/ioutil" |
| 14 | + "os" |
| 15 | + "path/filepath" |
| 16 | + "runtime" |
| 17 | + "strings" |
| 18 | +) |
| 19 | + |
| 20 | +// Default target to run when none is specified |
| 21 | +// If not set, running mage will list available targets |
| 22 | +// var Default = Build |
| 23 | + |
| 24 | +// tidy code |
| 25 | +func Fmt() error { |
| 26 | + packages := strings.Split("cmd", " ") |
| 27 | + files, _ := filepath.Glob("*.go") |
| 28 | + packages = append(packages, files...) |
| 29 | + return sh.Run("gofmt", append([]string{"-s", "-l", "-w"}, packages...)...) |
| 30 | +} |
| 31 | + |
| 32 | +// for local machine build |
| 33 | +func Build() error { |
| 34 | + return buildTarget(runtime.GOOS, runtime.GOARCH, nil) |
| 35 | +} |
| 36 | + |
| 37 | +// build all platform |
| 38 | +func Pack() error { |
| 39 | + buildTarget("darwin", "amd64", nil) |
| 40 | + buildTarget("darwin", "386", nil) |
| 41 | + buildTarget("freebsd", "amd64", nil) |
| 42 | + buildTarget("freebsd", "386", nil) |
| 43 | + buildTarget("linux", "amd64", nil) |
| 44 | + buildTarget("linux", "386", nil) |
| 45 | + buildTarget("linux", "arm", nil) |
| 46 | + buildTarget("linux", "arm64", nil) |
| 47 | + buildTarget("windows", "amd64", nil) |
| 48 | + buildTarget("windows", "386", nil) |
| 49 | + return genCheckSum() |
| 50 | +} |
| 51 | + |
| 52 | +// Test all packages |
| 53 | +func Test() error { |
| 54 | + err := sh.RunV("go", "test", "-v", "-coverprofile", ".cover.out", "./...") |
| 55 | + if err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + |
| 59 | + err = sh.RunV("go", "tool", "cover", "-func=.cover.out") |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + |
| 64 | + err = sh.Run("go", "tool", "cover", "-html=.cover.out", "-o", ".cover.html") |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + |
| 69 | + return sh.Rm(".cover.out") |
| 70 | +} |
| 71 | + |
| 72 | +// build to target (cross build) |
| 73 | +func buildTarget(OS, arch string, envs map[string]string) error { |
| 74 | + tag := tag() |
| 75 | + name := fmt.Sprintf("sshw-%s-%s-%s", OS, arch, tag) |
| 76 | + dir := fmt.Sprintf("dist/%s", name) |
| 77 | + target := fmt.Sprintf("%s/sshw", dir) |
| 78 | + |
| 79 | + args := make([]string, 0, 10) |
| 80 | + args = append(args, "build", "-o", target) |
| 81 | + args = append(args, "-ldflags", flags(), "cmd/sshw/main.go") |
| 82 | + |
| 83 | + fmt.Println("build", target) |
| 84 | + env := make(map[string]string) |
| 85 | + env["GOOS"] = OS |
| 86 | + env["GOARCH"] = arch |
| 87 | + env["CGO_ENABLED"] = "0" |
| 88 | + |
| 89 | + if envs != nil { |
| 90 | + for k, v := range envs { |
| 91 | + env[k] = v |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + if err := sh.RunWith(env, mg.GoCmd(), args...); err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + |
| 99 | + sh.Run("tar", "-czf", fmt.Sprintf("%s.tar.gz", dir), "-C", "dist", name) |
| 100 | + |
| 101 | + return nil |
| 102 | +} |
| 103 | + |
| 104 | +func flags() string { |
| 105 | + hash := hash() |
| 106 | + tag := tag() |
| 107 | + return fmt.Sprintf(`-s -w -X "main.Build=%s-%s" -extldflags "-static"`, tag, hash) |
| 108 | +} |
| 109 | + |
| 110 | +// tag returns the git tag for the current branch or "" if none. |
| 111 | +func tag() string { |
| 112 | + s, _ := sh.Output("bash", "-c", "git describe --abbrev=0 --tags 2> /dev/null") |
| 113 | + if s == "" { |
| 114 | + return "dev" |
| 115 | + } |
| 116 | + return s |
| 117 | +} |
| 118 | + |
| 119 | +// hash returns the git hash for the current repo or "" if none. |
| 120 | +func hash() string { |
| 121 | + hash, _ := sh.Output("git", "rev-parse", "--short", "HEAD") |
| 122 | + return hash |
| 123 | +} |
| 124 | + |
| 125 | +func mod() string { |
| 126 | + f, err := os.Open("go.mod") |
| 127 | + if err == nil { |
| 128 | + reader := bufio.NewReader(f) |
| 129 | + line, _, _ := reader.ReadLine() |
| 130 | + return strings.Replace(string(line), "module ", "", 1) |
| 131 | + } |
| 132 | + return "" |
| 133 | +} |
| 134 | + |
| 135 | +// cleanup all build files |
| 136 | +func Clean() { |
| 137 | + sh.Rm("dist") |
| 138 | + sh.Rm(".cover.html") |
| 139 | +} |
| 140 | + |
| 141 | +func genCheckSum() error { |
| 142 | + fmt.Println("generate checksum.txt file") |
| 143 | + fs, err := ioutil.ReadDir("dist") |
| 144 | + if err != nil { |
| 145 | + return err |
| 146 | + } |
| 147 | + |
| 148 | + file, err := os.OpenFile("dist/checksum.txt", os.O_CREATE|os.O_WRONLY, 0600) |
| 149 | + if err != nil { |
| 150 | + return err |
| 151 | + } |
| 152 | + defer file.Close() |
| 153 | + for _, f := range fs { |
| 154 | + if f.IsDir() { |
| 155 | + continue |
| 156 | + } |
| 157 | + |
| 158 | + if strings.HasSuffix(f.Name(), ".tar.gz") { |
| 159 | + sum, _ := fileHash(fmt.Sprintf("dist/%s", f.Name())) |
| 160 | + fmt.Println(sum, f.Name()) |
| 161 | + file.WriteString(fmt.Sprintf("%s %s\n", sum, f.Name())) |
| 162 | + } |
| 163 | + } |
| 164 | + return nil |
| 165 | +} |
| 166 | + |
| 167 | +func fileHash(filename string) (string, error) { |
| 168 | + file, err := os.Open(filename) |
| 169 | + if err != nil { |
| 170 | + return "", err |
| 171 | + } |
| 172 | + |
| 173 | + hash := sha1.New() |
| 174 | + io.Copy(hash, file) |
| 175 | + ret := hash.Sum(nil) |
| 176 | + return hex.EncodeToString(ret[:]), nil |
| 177 | +} |
0 commit comments