Make IsEnabled match the documentation
This commit is contained in:
parent
3fcd88d966
commit
6b8241ed7c
@ -16,6 +16,8 @@ var (
|
|||||||
// Running as superuser or adding the correct PolicyKit definitions can fix this
|
// Running as superuser or adding the correct PolicyKit definitions can fix this
|
||||||
// See https://wiki.debian.org/PolicyKit for more information
|
// See https://wiki.debian.org/PolicyKit for more information
|
||||||
ErrInsufficientPermissions = errors.New("insufficient permissions")
|
ErrInsufficientPermissions = errors.New("insufficient permissions")
|
||||||
|
// Selected unit file resides outside of the unit file search path
|
||||||
|
ErrLinked = errors.New("unit file linked")
|
||||||
// Masked units can only be unmasked, but something else was attempted
|
// Masked units can only be unmasked, but something else was attempted
|
||||||
// Unmask the unit before enabling or disabling it
|
// Unmask the unit before enabling or disabling it
|
||||||
ErrMasked = errors.New("unit masked")
|
ErrMasked = errors.New("unit masked")
|
||||||
|
186
systemctl.go
186
systemctl.go
@ -8,22 +8,54 @@ import (
|
|||||||
"github.com/taigrr/systemctl/properties"
|
"github.com/taigrr/systemctl/properties"
|
||||||
)
|
)
|
||||||
|
|
||||||
func IsFailed(ctx context.Context, unit string, opts Options) (bool, error) {
|
// Reload systemd manager configuration.
|
||||||
var args = []string{"is-failed", "--system", unit}
|
//
|
||||||
|
// This will rerun all generators (see systemd. generator(7)), reload all unit
|
||||||
|
// files, and recreate the entire dependency tree. While the daemon is being
|
||||||
|
// reloaded, all sockets systemd listens on behalf of user configuration will
|
||||||
|
// stay accessible.
|
||||||
|
func DaemonReload(ctx context.Context, opts Options) error {
|
||||||
|
var args = []string{"daemon-reload", "--system"}
|
||||||
if opts.UserMode {
|
if opts.UserMode {
|
||||||
args[1] = "--user"
|
args[1] = "--user"
|
||||||
}
|
}
|
||||||
stdout, _, _, err := execute(ctx, args)
|
_, _, _, err := execute(ctx, args)
|
||||||
if matched, _ := regexp.MatchString(`inactive`, stdout); matched {
|
return err
|
||||||
return false, nil
|
|
||||||
} else if matched, _ := regexp.MatchString(`active`, stdout); matched {
|
|
||||||
return false, nil
|
|
||||||
} else if matched, _ := regexp.MatchString(`failed`, stdout); matched {
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
return false, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Disables one or more units.
|
||||||
|
//
|
||||||
|
// This removes all symlinks to the unit files backing the specified units from
|
||||||
|
// the unit configuration directory, and hence undoes any changes made by
|
||||||
|
// enable or link.
|
||||||
|
func Disable(ctx context.Context, unit string, opts Options) error {
|
||||||
|
var args = []string{"disable", "--system", unit}
|
||||||
|
if opts.UserMode {
|
||||||
|
args[1] = "--user"
|
||||||
|
}
|
||||||
|
_, _, _, err := execute(ctx, args)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enable one or more units or unit instances.
|
||||||
|
//
|
||||||
|
// This will create a set of symlinks, as encoded in the [Install] sections of
|
||||||
|
// the indicated unit files. After the symlinks have been created, the system
|
||||||
|
// manager configuration is reloaded (in a way equivalent to daemon-reload),
|
||||||
|
// in order to ensure the changes are taken into account immediately.
|
||||||
|
func Enable(ctx context.Context, unit string, opts Options) error {
|
||||||
|
var args = []string{"enable", "--system", unit}
|
||||||
|
if opts.UserMode {
|
||||||
|
args[1] = "--user"
|
||||||
|
}
|
||||||
|
_, _, _, err := execute(ctx, args)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check whether any of the specified units are active (i.e. running).
|
||||||
|
//
|
||||||
|
// Returns true if the unit is active, false if inactive or failed.
|
||||||
|
// Also returns false in an error case.
|
||||||
func IsActive(ctx context.Context, unit string, opts Options) (bool, error) {
|
func IsActive(ctx context.Context, unit string, opts Options) (bool, error) {
|
||||||
var args = []string{"is-active", "--system", unit}
|
var args = []string{"is-active", "--system", unit}
|
||||||
if opts.UserMode {
|
if opts.UserMode {
|
||||||
@ -41,30 +73,77 @@ func IsActive(ctx context.Context, unit string, opts Options) (bool, error) {
|
|||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Checks whether any of the specified unit files are enabled (as with enable).
|
||||||
|
//
|
||||||
|
// Returns true if the unit is enabled, aliased, static, indirect, generated
|
||||||
|
// or transient.
|
||||||
|
//
|
||||||
|
// Returns false if disabled. Also returns an error if linked, masked, or bad.
|
||||||
|
//
|
||||||
|
// See https://www.freedesktop.org/software/systemd/man/systemctl.html#is-enabled%20UNIT%E2%80%A6
|
||||||
|
// for more information
|
||||||
func IsEnabled(ctx context.Context, unit string, opts Options) (bool, error) {
|
func IsEnabled(ctx context.Context, unit string, opts Options) (bool, error) {
|
||||||
var args = []string{"is-enabled", "--system", unit}
|
var args = []string{"is-enabled", "--system", unit}
|
||||||
if opts.UserMode {
|
if opts.UserMode {
|
||||||
args[1] = "--user"
|
args[1] = "--user"
|
||||||
}
|
}
|
||||||
stdout, _, _, err := execute(ctx, args)
|
stdout, _, _, err := execute(ctx, args)
|
||||||
if matched, _ := regexp.MatchString(`enabled`, stdout); matched {
|
switch stdout {
|
||||||
|
case "enabled":
|
||||||
return true, nil
|
return true, nil
|
||||||
} else if matched, _ := regexp.MatchString(`disabled`, stdout); matched {
|
case "enabled-runtime":
|
||||||
return false, nil
|
return true, nil
|
||||||
} else if matched, _ := regexp.MatchString(`masked`, stdout); matched {
|
case "linked":
|
||||||
|
return false, ErrLinked
|
||||||
|
case "linked-runtime":
|
||||||
|
return false, ErrLinked
|
||||||
|
case "alias":
|
||||||
|
return true, nil
|
||||||
|
case "masked":
|
||||||
|
return false, ErrMasked
|
||||||
|
case "masked-runtime":
|
||||||
|
return false, ErrMasked
|
||||||
|
case "static":
|
||||||
|
return true, nil
|
||||||
|
case "indirect":
|
||||||
|
return true, nil
|
||||||
|
case "disabled":
|
||||||
return false, nil
|
return false, nil
|
||||||
|
case "generated":
|
||||||
|
return true, nil
|
||||||
|
case "transient":
|
||||||
|
return true, nil
|
||||||
}
|
}
|
||||||
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
|
}
|
||||||
|
return false, ErrUnspecified
|
||||||
}
|
}
|
||||||
|
|
||||||
func Status(ctx context.Context, unit string, opts Options) (string, error) {
|
// Check whether any of the specified units are in a "failed" state.
|
||||||
var args = []string{"status", "--system", unit}
|
func IsFailed(ctx context.Context, unit string, opts Options) (bool, error) {
|
||||||
|
var args = []string{"is-failed", "--system", unit}
|
||||||
if opts.UserMode {
|
if opts.UserMode {
|
||||||
args[1] = "--user"
|
args[1] = "--user"
|
||||||
}
|
}
|
||||||
stdout, _, _, err := execute(ctx, args)
|
stdout, _, _, err := execute(ctx, args)
|
||||||
return stdout, err
|
if matched, _ := regexp.MatchString(`inactive`, stdout); matched {
|
||||||
|
return false, nil
|
||||||
|
} else if matched, _ := regexp.MatchString(`active`, stdout); matched {
|
||||||
|
return false, nil
|
||||||
|
} else if matched, _ := regexp.MatchString(`failed`, stdout); matched {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func Mask(ctx context.Context, unit string, opts Options) error {
|
||||||
|
var args = []string{"mask", "--system", unit}
|
||||||
|
if opts.UserMode {
|
||||||
|
args[1] = "--user"
|
||||||
|
}
|
||||||
|
_, _, _, err := execute(ctx, args)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func Restart(ctx context.Context, unit string, opts Options) error {
|
func Restart(ctx context.Context, unit string, opts Options) error {
|
||||||
@ -76,51 +155,6 @@ func Restart(ctx context.Context, unit string, opts Options) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func Start(ctx context.Context, unit string, opts Options) error {
|
|
||||||
var args = []string{"start", "--system", unit}
|
|
||||||
if opts.UserMode {
|
|
||||||
args[1] = "--user"
|
|
||||||
}
|
|
||||||
_, _, _, err := execute(ctx, args)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func Stop(ctx context.Context, unit string, opts Options) error {
|
|
||||||
var args = []string{"stop", "--system", unit}
|
|
||||||
if opts.UserMode {
|
|
||||||
args[1] = "--user"
|
|
||||||
}
|
|
||||||
_, _, _, err := execute(ctx, args)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func Enable(ctx context.Context, unit string, opts Options) error {
|
|
||||||
var args = []string{"enable", "--system", unit}
|
|
||||||
if opts.UserMode {
|
|
||||||
args[1] = "--user"
|
|
||||||
}
|
|
||||||
_, _, _, err := execute(ctx, args)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func Disable(ctx context.Context, unit string, opts Options) error {
|
|
||||||
var args = []string{"disable", "--system", unit}
|
|
||||||
if opts.UserMode {
|
|
||||||
args[1] = "--user"
|
|
||||||
}
|
|
||||||
_, _, _, err := execute(ctx, args)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func DaemonReload(ctx context.Context, opts Options) error {
|
|
||||||
var args = []string{"daemon-reload", "--system"}
|
|
||||||
if opts.UserMode {
|
|
||||||
args[1] = "--user"
|
|
||||||
}
|
|
||||||
_, _, _, err := execute(ctx, args)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func Show(ctx context.Context, unit string, property properties.Property, opts Options) (string, error) {
|
func Show(ctx context.Context, unit string, property properties.Property, opts Options) (string, error) {
|
||||||
var args = []string{"show", "--system", unit, "--property", string(property)}
|
var args = []string{"show", "--system", unit, "--property", string(property)}
|
||||||
if opts.UserMode {
|
if opts.UserMode {
|
||||||
@ -132,8 +166,26 @@ func Show(ctx context.Context, unit string, property properties.Property, opts O
|
|||||||
return stdout, err
|
return stdout, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func Mask(ctx context.Context, unit string, opts Options) error {
|
func Start(ctx context.Context, unit string, opts Options) error {
|
||||||
var args = []string{"mask", "--system", unit}
|
var args = []string{"start", "--system", unit}
|
||||||
|
if opts.UserMode {
|
||||||
|
args[1] = "--user"
|
||||||
|
}
|
||||||
|
_, _, _, err := execute(ctx, args)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func Status(ctx context.Context, unit string, opts Options) (string, error) {
|
||||||
|
var args = []string{"status", "--system", unit}
|
||||||
|
if opts.UserMode {
|
||||||
|
args[1] = "--user"
|
||||||
|
}
|
||||||
|
stdout, _, _, err := execute(ctx, args)
|
||||||
|
return stdout, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func Stop(ctx context.Context, unit string, opts Options) error {
|
||||||
|
var args = []string{"stop", "--system", unit}
|
||||||
if opts.UserMode {
|
if opts.UserMode {
|
||||||
args[1] = "--user"
|
args[1] = "--user"
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user