aboutsummaryrefslogtreecommitdiff
path: root/mage_helpers.go
blob: 08c45268dddc7b398dd48c9f0ad69453fa5f22d7 (plain)
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
// +build mage

package main

import (
	"context"
	"log"
	"os"
	"os/exec"
	"strings"

	"github.com/jtolds/qod"
	"github.com/pkg/errors"
)

var wd string

func init() {
	lwd, err := os.Getwd()
	qod.ANE(err)

	wd = lwd
}

// must end in a slash
const pkgBase = "github.com/Xe/site/"

func output(cmd string, args ...string) (string, error) {
	c := exec.Command(cmd, args...)
	c.Env = os.Environ()
	c.Stderr = os.Stderr
	b, err := c.Output()
	if err != nil {
		return "", errors.Wrapf(err, `failed to run %v %q`, cmd, args)
	}
	return string(b), nil
}

func gitTag() (string, error) {
	s, err := output("git", "describe", "--tags")
	if err != nil {
		ee, ok := errors.Cause(err).(*exec.ExitError)
		if ok && ee.Exited() {
			// probably no git tag
			return "dev", nil
		}
		return "", err
	}

	return strings.TrimSuffix(s, "\n"), nil
}

func shouldWork(ctx context.Context, env []string, dir string, cmdName string, args ...string) {
	loc, err := exec.LookPath(cmdName)
	qod.ANE(err)

	cmd := exec.CommandContext(ctx, loc, args...)
	cmd.Dir = dir
	cmd.Env = append(env, os.Environ()...)

	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr

	log.Printf("starting process, env: %v, pwd: %s, cmd: %s, args: %v", env, dir, loc, args)
	err = cmd.Run()
	qod.ANE(err)
}

func goBuild(ctx context.Context, env []string, dir string, pkgname string) {
	shouldWork(ctx, env, dir, "go", "build", "-v", pkgBase+pkgname)
}

func goInstall(ctx context.Context, env []string, pkgname string) {
	shouldWork(ctx, nil, wd, "go", "install", pkgBase+pkgname)
}