mirror of
https://github.com/taigrr/systemctl.git
synced 2026-04-01 18:18:45 -07:00
refactor: use unused constants instead of removing them
- Export unitTypes as UnitTypes and add HasValidUnitSuffix helper - Use killed const (exit code 130) in execute() to detect SIGINT - Update go.mod to go 1.26
This commit is contained in:
2
go.mod
2
go.mod
@@ -1,3 +1,3 @@
|
|||||||
module github.com/taigrr/systemctl
|
module github.com/taigrr/systemctl
|
||||||
|
|
||||||
go 1.21
|
go 1.26
|
||||||
|
|||||||
29
structs.go
29
structs.go
@@ -1,5 +1,7 @@
|
|||||||
package systemctl
|
package systemctl
|
||||||
|
|
||||||
|
import "strings"
|
||||||
|
|
||||||
type Options struct {
|
type Options struct {
|
||||||
UserMode bool
|
UserMode bool
|
||||||
}
|
}
|
||||||
@@ -11,3 +13,30 @@ type Unit struct {
|
|||||||
Sub string
|
Sub string
|
||||||
Description 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
|
||||||
|
}
|
||||||
|
|||||||
7
util.go
7
util.go
@@ -11,6 +11,9 @@ import (
|
|||||||
|
|
||||||
var systemctl string
|
var systemctl string
|
||||||
|
|
||||||
|
// killed is the exit code returned when a process is terminated by SIGINT.
|
||||||
|
const killed = 130
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
path, _ := exec.LookPath("systemctl")
|
path, _ := exec.LookPath("systemctl")
|
||||||
systemctl = path
|
systemctl = path
|
||||||
@@ -37,6 +40,10 @@ func execute(ctx context.Context, args []string) (string, string, int, error) {
|
|||||||
warnings = stderr.String()
|
warnings = stderr.String()
|
||||||
code = cmd.ProcessState.ExitCode()
|
code = cmd.ProcessState.ExitCode()
|
||||||
|
|
||||||
|
if code == killed {
|
||||||
|
return output, warnings, code, ErrExecTimeout
|
||||||
|
}
|
||||||
|
|
||||||
customErr := filterErr(warnings)
|
customErr := filterErr(warnings)
|
||||||
if customErr != nil {
|
if customErr != nil {
|
||||||
err = customErr
|
err = customErr
|
||||||
|
|||||||
Reference in New Issue
Block a user