Clean up errors and messages

This commit is contained in:
Tai Groot 2021-05-16 16:17:12 -07:00
parent 7325dda66d
commit 3fcd88d966
Signed by: taigrr
GPG Key ID: D00C269A87614812
3 changed files with 29 additions and 24 deletions

View File

@ -5,26 +5,31 @@ import (
) )
var ( var (
// ErrSystemctlNotInstalled means that upon trying to manually locate systemctl in the user's path, // $DBUS_SESSION_BUS_ADDRESS and $XDG_RUNTIME_DIR were not defined
// it was not found. If this error occurs, the library isn't entirely useful. // This usually is the result of running in usermode as root
ErrNotInstalled = errors.New("systemd binary was not found") ErrBusFailure = errors.New("bus connection failure")
// The unit specified doesn't exist or can't be found
// ErrExecTimeout means that the provided context was done before the command finished execution. ErrDoesNotExist = errors.New("unit does not exist")
// The provided context was cancelled before the command finished execution
ErrExecTimeout = errors.New("command timed out") ErrExecTimeout = errors.New("command timed out")
// The executable was invoked without enough permissions to run the selected command
// Running as superuser or adding the correct PolicyKit definitions can fix this
// See https://wiki.debian.org/PolicyKit for more information
ErrInsufficientPermissions = errors.New("insufficient permissions")
// Masked units can only be unmasked, but something else was attempted
// Unmask the unit before enabling or disabling it
ErrMasked = errors.New("unit masked")
// If this error occurs, the library isn't entirely useful, as it causes a panic
// Make sure systemctl is in the PATH before calling again
ErrNotInstalled = errors.New("systemctl not in $PATH")
// A unit was expected to be running but was found inactive
// This can happen when calling GetStartTime on a dead unit, for example
ErrUnitNotActive = errors.New("unit not active")
// An expected value is unavailable, but the unit may be running
// This can happen when calling GetMemoryUsage on systemd itself, for example
ErrValueNotSet = errors.New("value not set")
// ErrInsufficientPermissions means the calling executable was invoked without enough permissions to run the selected command // Something in the stderr output contains the word `Failed`, but it is not a known case
ErrInsufficientPermissions = errors.New("insufficient permissions for action") // This is a catch-all, and if it's ever seen in the wild, please submit a PR
ErrUnspecified = errors.New("Unknown error, please submit an issue at github.com/taigrr/systemctl")
// ErrDoesNotExist means the unit specified doesn't exist or can't be found
ErrDoesNotExist = errors.New("Unit does not exist")
// ErrUnspecified means something in the stderr output contains the word `Failed`, but not a known case
ErrUnspecified = errors.New("Unknown error")
// ErrUnitNotRunning means a function which requires a unit to be run (such as GetStartTime) was run against an inactive unit
ErrUnitNotRunning = errors.New("Unit isn't running")
// ErrBusFailure means $DBUS_SESSION_BUS_ADDRESS and $XDG_RUNTIME_DIR were not defined
ErrBusFailure = errors.New("Failure to connect to bus, did you run in usermode as root?")
// ErrValueNotSet means an expected value is unavailable, but the unit may be running
ErrValueNotSet = errors.New("Value not set or unavailable")
// ErrMasked means a unit cannot be enabled because it is masked
ErrMasked = errors.New("Unit is masked")
) )

View File

@ -19,7 +19,7 @@ func GetStartTime(ctx context.Context, unit string, opts Options) (time.Time, er
} }
// ExecMainStartTimestamp returns an empty string if the unit is not running // ExecMainStartTimestamp returns an empty string if the unit is not running
if value == "" { if value == "" {
return time.Unix(0, 0), ErrUnitNotRunning return time.Unix(0, 0), ErrUnitNotActive
} }
return time.Parse(dateFormat, value) return time.Parse(dateFormat, value)
} }

View File

@ -24,16 +24,16 @@ func TestGetStartTime(t *testing.T) {
}{ }{
// Run these tests only as a user // Run these tests only as a user
//try nonexistant unit in user mode as user //try nonexistant unit in user mode as user
{"nonexistant", ErrUnitNotRunning, Options{UserMode: false}, true}, {"nonexistant", ErrUnitNotActive, Options{UserMode: false}, true},
// try existing unit in user mode as user // try existing unit in user mode as user
{"syncthing", ErrUnitNotRunning, Options{UserMode: true}, true}, {"syncthing", ErrUnitNotActive, Options{UserMode: true}, true},
// try existing unit in system mode as user // try existing unit in system mode as user
{"nginx", nil, Options{UserMode: false}, true}, {"nginx", nil, Options{UserMode: false}, true},
// Run these tests only as a superuser // Run these tests only as a superuser
// try nonexistant unit in system mode as system // try nonexistant unit in system mode as system
{"nonexistant", ErrUnitNotRunning, Options{UserMode: false}, false}, {"nonexistant", ErrUnitNotActive, Options{UserMode: false}, false},
// try existing unit in system mode as system // try existing unit in system mode as system
{"nginx", ErrBusFailure, Options{UserMode: true}, false}, {"nginx", ErrBusFailure, Options{UserMode: true}, false},
// try existing unit in system mode as system // try existing unit in system mode as system