summaryrefslogtreecommitdiff
path: root/install/install.go
blob: cef11f0824ffb571bced31effb2a25ccc09ac8c6 (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
package install

import (
	"os"
	"path/filepath"
)

type installer interface {
	Install(cmd, bin string) error
	Uninstall(cmd, bin string) error
}

func Install(cmd string, asRoot bool) error {
	bin, err := getBinaryPath()
	if err != nil {
		return err
	}
	return getInstaller(asRoot).Install(cmd, bin)
}

func Uninstall(cmd string, asRoot bool) error {
	bin, err := getBinaryPath()
	if err != nil {
		return err
	}
	return getInstaller(asRoot).Uninstall(cmd, bin)
}

func getInstaller(asRoot bool) installer {
	if asRoot {
		return root{}
	} else {
		return home{}
	}
}

func getBinaryPath() (string, error) {
	bin, err := os.Executable()
	if err != nil {
		return "", err
	}
	return filepath.Abs(bin)
}