summaryrefslogtreecommitdiff
path: root/Magefile.go
blob: 4ec244df7ea8cac7233b8caa2428dfee1ccab80a (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

// Magefile provides build targets for the geheim project.
// Targets: Build, Test, Vet, Install, Uninstall, Clean
package main

import (
	"errors"
	"fmt"
	"os"

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

const (
	binary  = "./bin/geheim"
	mainPkg = "./cmd/geheim"
)

// Build compiles the binary to ./bin/geheim.
func Build() error {
	mg.Deps(createBinDir)
	fmt.Println("Building", binary)
	return sh.RunV("go", "build", "-o", binary, mainPkg)
}

// Test runs all tests in the module.
func Test() error {
	fmt.Println("Running tests")
	return sh.RunV("go", "test", "./...")
}

// Vet runs go vet on all packages.
func Vet() error {
	fmt.Println("Vetting")
	return sh.RunV("go", "vet", "./...")
}

// Install builds the binary, copies it to ~/.local/bin/geheim, and sets executable permissions.
func Install() error {
	mg.Deps(Build)
	home, err := os.UserHomeDir()
	if err != nil {
		return fmt.Errorf("cannot determine home directory: %w", err)
	}
	dest := home + "/.local/bin/geheim"
	fmt.Println("Installing to", dest)
	if err := sh.Copy(dest, binary); err != nil {
		return err
	}
	return os.Chmod(dest, 0755)
}

// Uninstall removes the installed binary from ~/.local/bin/geheim.
// It is idempotent: if the binary is not installed, it succeeds silently.
func Uninstall() error {
	home, err := os.UserHomeDir()
	if err != nil {
		return fmt.Errorf("cannot determine home directory: %w", err)
	}
	dest := home + "/.local/bin/geheim"
	fmt.Println("Uninstalling", dest)
	if err := os.Remove(dest); err != nil && !errors.Is(err, os.ErrNotExist) {
		return err
	}
	return nil
}

// Clean removes the ./bin directory.
func Clean() error {
	fmt.Println("Cleaning", "./bin")
	return os.RemoveAll("./bin")
}

// createBinDir ensures ./bin exists before the build step writes the binary.
func createBinDir() error {
	return os.MkdirAll("./bin", 0755)
}