diff --git a/go.mod b/go.mod index 2fd2fde..6a43b6f 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/taigrr/systemctl -go 1.21 +go 1.26 diff --git a/structs.go b/structs.go index 0a0458a..e0d4ca8 100644 --- a/structs.go +++ b/structs.go @@ -1,5 +1,7 @@ package systemctl +import "strings" + type Options struct { UserMode bool } @@ -11,3 +13,30 @@ type Unit struct { Sub string Description string } + +// UnitTypes contains all valid systemd unit type suffixes. +var UnitTypes = []string{ + "automount", + "device", + "mount", + "path", + "scope", + "service", + "slice", + "snapshot", + "socket", + "swap", + "target", + "timer", +} + +// HasValidUnitSuffix checks whether the given unit name ends with a valid +// systemd unit type suffix (e.g. ".service", ".timer"). +func HasValidUnitSuffix(unit string) bool { + for _, t := range UnitTypes { + if strings.HasSuffix(unit, "."+t) { + return true + } + } + return false +} diff --git a/util.go b/util.go index 7dc8a2e..75f4d7a 100644 --- a/util.go +++ b/util.go @@ -11,6 +11,9 @@ import ( var systemctl string +// killed is the exit code returned when a process is terminated by SIGINT. +const killed = 130 + func init() { path, _ := exec.LookPath("systemctl") systemctl = path @@ -37,6 +40,10 @@ func execute(ctx context.Context, args []string) (string, string, int, error) { warnings = stderr.String() code = cmd.ProcessState.ExitCode() + if code == killed { + return output, warnings, code, ErrExecTimeout + } + customErr := filterErr(warnings) if customErr != nil { err = customErr