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

import (
	"fmt"

	"codeberg.org/snonux/gonf/internal/exec"
)

func applyDNF(p *Package) error {
	var args []string

	if p.Absent {
		args = []string{"remove", "-y", p.name}
	} else if p.latest {
		// update ensures the package is installed and updated to the latest version.
		args = []string{"update", "-y", p.name}
	} else {
		// install ensures the package is installed, but does not update it if already present.
		args = []string{"install", "-y", p.name}
	}

	stdout, stderr, exitCode, err := exec.Run("dnf", args...)
	if err != nil {
		return fmt.Errorf("failed to execute dnf: %w", err)
	}

	if exitCode != 0 {
		return fmt.Errorf("dnf failed with exit code %d: %s\n%s", exitCode, stdout, stderr)
	}

	return nil
}