summaryrefslogtreecommitdiff
path: root/internal/resource/pkg/pkg.go
blob: 72f098c2ab23dc9bf1d5e88da0aacef7b6dc4e14 (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
package pkg

import (
	"errors"
	"os"
)

type Ensure int

const (
	PkgPresent Ensure = iota
	PkgAbsent
	PkgLatest
)

type applyFunc func(name string, ensure Ensure) error

func Present(name string, ensure Ensure) error {
	bin, err := detect()
	if err != nil {
		return err
	}

	var applyFunc applyFunc

	switch bin {
	case "dnf":
		applyFunc = applyDNF
	}

	return applyFunc(name, ensure)
}

func detect() (string, error) {
	switch {
	case exists("/etc/fedora-release"):
		fallthrough
	case exists("/etc/rocky-release"):
		return "dnf", nil
	}
	return "", errors.New("unable to detect package manager!")
}

func exists(path string) bool {
	_, err := os.Stat(path)
	return err == nil
}