summaryrefslogtreecommitdiff
path: root/Magefile.go
blob: 119f0b8fbfbc69f8c3037df241bfb8ea2cb9338f (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
90
91
92
93
94
95
96
//go:build mage

package main

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

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

const (
	binaryName = "loadbars"
	shareName  = "loadbars"
)

// Default sets the default target (build).
func Default() { mg.Deps(Build) }

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

// Test runs the test suite.
func Test() error {
	return sh.RunV("go", "test", "./...")
}

// Install builds and installs the binary, script, and NOTICE under DESTDIR.
// Usage: DESTDIR=/tmp/install mage install  (or  mage install  for DESTDIR empty)
func Install() error {
	mg.Deps(Build)
	dest := getDestDir()
	binDir := filepath.Join(dest, "usr", "bin")
	shareDir := filepath.Join(dest, "usr", "share", shareName)
	scriptsDir := filepath.Join(shareDir, "scripts")
	if err := os.MkdirAll(binDir, 0755); err != nil {
		return fmt.Errorf("mkdir %s: %w", binDir, err)
	}
	if err := os.MkdirAll(scriptsDir, 0755); err != nil {
		return fmt.Errorf("mkdir %s: %w", scriptsDir, err)
	}
	if err := sh.Copy(filepath.Join(binDir, binaryName), binaryName); err != nil {
		return fmt.Errorf("copy binary: %w", err)
	}
	if err := sh.Copy(filepath.Join(scriptsDir, "loadbars-remote.sh"), "scripts/loadbars-remote.sh"); err != nil {
		return fmt.Errorf("copy script: %w", err)
	}
	if err := sh.Copy(filepath.Join(shareDir, "NOTICE"), "NOTICE"); err != nil {
		return fmt.Errorf("copy NOTICE: %w", err)
	}
	fmt.Printf("Installed to %s (binary: %s/usr/bin/%s)\n", dest, dest, binaryName)
	return nil
}

// Uninstall removes files installed by Install from DESTDIR.
func Uninstall() error {
	return deinstall()
}

// Deinstall is an alias for Uninstall.
func Deinstall() error {
	return deinstall()
}

func deinstall() error {
	dest := getDestDir()
	if dest == "" {
		return fmt.Errorf("DESTDIR must be set to uninstall (e.g. DESTDIR=/tmp/install mage uninstall)")
	}
	paths := []string{
		filepath.Join(dest, "usr", "bin", binaryName),
		filepath.Join(dest, "usr", "share", shareName, "scripts", "loadbars-remote.sh"),
		filepath.Join(dest, "usr", "share", shareName, "NOTICE"),
	}
	for _, p := range paths {
		if err := os.Remove(p); err != nil && !os.IsNotExist(err) {
			return fmt.Errorf("remove %s: %w", p, err)
		}
	}
	// Remove empty dirs
	_ = os.Remove(filepath.Join(dest, "usr", "share", shareName, "scripts"))
	_ = os.Remove(filepath.Join(dest, "usr", "share", shareName))
	fmt.Printf("Uninstalled from %s\n", dest)
	return nil
}

func getDestDir() string {
	if d := os.Getenv("DESTDIR"); d != "" {
		return filepath.Clean(d)
	}
	return ""
}