summaryrefslogtreecommitdiff
path: root/Magefile.go
blob: 0c3c59c4fade71cdec6de2ea87a1578314c6ad15 (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
//go:build mage

package main

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

var (
	binName = "gonf"
	mod     = "codeberg.org/snonux/gonf"
)

func run(cmd string, args ...string) error {
	c := exec.Command(cmd, args...)
	c.Stdout = os.Stdout
	c.Stderr = os.Stderr
	return c.Run()
}

// Default runs the program.
func Default() error {
	return Run()
}

// Build compiles the binary.
func Build() error {
	fmt.Println("building...")
	return run("go", "build", "-o", binName, "./cmd/gonf")
}

// Run builds and runs the program.
func Run() error {
	fmt.Println("running...")
	if err := Build(); err != nil {
		return err
	}
	return run("./"+binName, "version")
}

// Test runs all unit tests.
func Test() error {
	fmt.Println("testing...")
	return run("go", "test", "-v", "./...")
}

// Lint runs go vet.
func Lint() error {
	fmt.Println("linting...")
	return run("go", "vet", "./...")
}

// Install builds and installs the binary to $GOPATH/bin.
func Install() error {
	fmt.Println("installing...")
	return run("go", "install", "./cmd/gonf")
}

// Uninstall removes the binary from $GOPATH/bin.
func Uninstall() error {
	fmt.Println("uninstalling...")
	gopath := os.Getenv("GOPATH")
	if gopath == "" {
		gopath = filepath.Join(os.Getenv("HOME"), "go")
	}
	binPath := filepath.Join(gopath, "bin", binName)
	if err := os.Remove(binPath); err != nil {
		if !os.IsNotExist(err) {
			return fmt.Errorf("remove %s: %w", binPath, err)
		}
		fmt.Println("binary not found, nothing to remove")
	} else {
		fmt.Println("removed " + binPath)
	}
	return nil
}