summaryrefslogtreecommitdiff
path: root/Magefile.go
blob: 742ffbf70568d9cddc422a3b6632243f439a05e2 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//go:build mage

package main

// Magefile for goprecords. Targets follow the same style as other Go projects (e.g. hexai).

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/magefile/mage/mg"
	"github.com/magefile/mage/sh"
)

const binaryName = "goprecords"

// Default builds the binary.
func Default() { mg.Deps(Build) }

// Build builds the goprecords binary.
func Build() error {
	return sh.RunV("go", "build", "-o", binaryName, "./cmd/goprecords")
}

// Test runs all tests with the default race-free pass and again with the race detector.
func Test() error {
	mg.Deps(testUnit, testRace)
	return nil
}

func testUnit() error {
	return sh.RunV("go", "test", "./...")
}

// TestRace runs tests with the race detector (-race).
func TestRace() error {
	return testRace()
}

func testRace() error {
	return sh.RunV("go", "test", "-race", "./...")
}

// CoverMicroservice runs tests with coverage for daemon, authkeys, and cli, then prints per-function coverage (go tool cover -func).
func CoverMicroservice() error {
	profile := filepath.Join(os.TempDir(), "goprecords-microservice.cover")
	if err := sh.RunV("go", "test", "-coverprofile", profile, "-covermode", "count",
		"./internal/daemon/...", "./internal/authkeys/...", "./internal/cli/..."); err != nil {
		return err
	}
	return sh.RunV("go", "tool", "cover", "-func", profile)
}

// Install builds and installs the binary to GOPATH/bin.
func Install() error {
	mg.Deps(Build)
	gopath := os.Getenv("GOPATH")
	if gopath == "" {
		home, err := os.UserHomeDir()
		if err != nil {
			return fmt.Errorf("GOPATH unset and home: %w", err)
		}
		gopath = filepath.Join(home, "go")
	}
	binDir := filepath.Join(gopath, "bin")
	if err := os.MkdirAll(binDir, 0755); err != nil {
		return fmt.Errorf("mkdir %s: %w", binDir, err)
	}
	dest := filepath.Join(binDir, binaryName)
	return sh.RunV("cp", "-v", binaryName, dest)
}

// Uninstall removes the binary from GOPATH/bin.
func Uninstall() error {
	gopath := os.Getenv("GOPATH")
	if gopath == "" {
		home, err := os.UserHomeDir()
		if err != nil {
			return fmt.Errorf("GOPATH unset and home: %w", err)
		}
		gopath = filepath.Join(home, "go")
	}
	dest := filepath.Join(gopath, "bin", binaryName)
	if err := os.Remove(dest); err != nil && !os.IsNotExist(err) {
		return err
	}
	return nil
}