mirror of
https://github.com/taigrr/systemctl.git
synced 2026-03-09 00:14:38 -07:00
fix(helpers): return ErrValueNotSet for nonexistent units in GetNumRestarts (#9)
systemd returns NRestarts=0 for units with LoadState=not-found, making it indistinguishable from a genuinely loaded unit with zero restarts. GetNumRestarts now checks LoadState when NRestarts is 0 and returns ErrValueNotSet for units that don't exist, matching GetMemoryUsage behavior. Also adds unit tests for filterErr (all stderr error mapping cases) and HasValidUnitSuffix (all valid unit types + negative cases). Updates syncthing test expectation from ErrValueNotSet to nil since loaded-but-inactive units legitimately have NRestarts=0.
This commit is contained in:
18
helpers.go
18
helpers.go
@@ -32,7 +32,23 @@ func GetNumRestarts(ctx context.Context, unit string, opts Options) (int, error)
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
return strconv.Atoi(value)
|
||||
if value == "[not set]" {
|
||||
return -1, ErrValueNotSet
|
||||
}
|
||||
restarts, err := strconv.Atoi(value)
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
// systemd returns NRestarts=0 for both genuinely zero-restart units and
|
||||
// nonexistent/unloaded units. Disambiguate by checking LoadState: if the
|
||||
// unit isn't loaded, the value is meaningless.
|
||||
if restarts == 0 {
|
||||
loadState, loadErr := Show(ctx, unit, properties.LoadState, opts)
|
||||
if loadErr == nil && loadState == "not-found" {
|
||||
return -1, ErrValueNotSet
|
||||
}
|
||||
}
|
||||
return restarts, nil
|
||||
}
|
||||
|
||||
// Get current memory in bytes (`systemctl show [unit] --property MemoryCurrent`) as an int
|
||||
|
||||
Reference in New Issue
Block a user