summaryrefslogtreecommitdiff
path: root/raveos-hyprland-theme/theme-data/DankMaterialShell/core/cmd/dms/utils.go
blob: 9459e41c4b0a3560b04ce3a81faddc9bc967ab05 (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
package main

import (
	"fmt"
	"os/exec"
	"slices"
	"strings"
)

// isReadOnlyCommand returns true if the CLI args indicate a command that is
// safe to run as root (e.g. shell completion, help).
func isReadOnlyCommand(args []string) bool {
	for _, arg := range args[1:] {
		if strings.HasPrefix(arg, "-") {
			continue
		}
		switch arg {
		case "completion", "help", "__complete":
			return true
		}
		return false
	}
	return false
}

func isArchPackageInstalled(packageName string) bool {
	cmd := exec.Command("pacman", "-Q", packageName)
	err := cmd.Run()
	return err == nil
}

type systemdServiceState struct {
	Name         string
	EnabledState string
	NeedsDisable bool
	Exists       bool
}

// checkSystemdServiceEnabled returns (state, should_disable, error) for a systemd service
func checkSystemdServiceEnabled(serviceName string) (string, bool, error) {
	cmd := exec.Command("systemctl", "is-enabled", serviceName)
	output, err := cmd.Output()

	stateStr := strings.TrimSpace(string(output))

	if err != nil {
		knownStates := []string{"disabled", "masked", "masked-runtime", "not-found", "enabled", "enabled-runtime", "static", "indirect", "alias"}
		isKnownState := slices.Contains(knownStates, stateStr)

		if !isKnownState {
			return stateStr, false, fmt.Errorf("systemctl is-enabled failed: %w (output: %s)", err, stateStr)
		}
	}

	shouldDisable := false
	switch stateStr {
	case "enabled", "enabled-runtime", "static", "indirect", "alias":
		shouldDisable = true
	case "disabled", "masked", "masked-runtime", "not-found":
		shouldDisable = false
	default:
		shouldDisable = true
	}

	return stateStr, shouldDisable, nil
}

func getSystemdServiceState(serviceName string) (*systemdServiceState, error) {
	state := &systemdServiceState{
		Name:   serviceName,
		Exists: false,
	}

	enabledState, needsDisable, err := checkSystemdServiceEnabled(serviceName)
	if err != nil {
		return nil, fmt.Errorf("failed to check enabled state: %w", err)
	}

	state.EnabledState = enabledState
	state.NeedsDisable = needsDisable

	if enabledState == "not-found" {
		state.Exists = false
		return state, nil
	}

	state.Exists = true
	return state, nil
}