adds option structs, ready for Show implementation

This commit is contained in:
Tai Groot
2021-05-15 13:49:34 -07:00
parent 7bedb452e4
commit 8a7c865b5f
5 changed files with 145 additions and 78 deletions

View File

@@ -2,98 +2,143 @@ package systemctl
import (
"context"
"fmt"
"regexp"
)
// TODO
func IsFailed(ctx context.Context, unit string, usermode bool) (bool, error) {
return false, nil
func IsFailed(ctx context.Context, unit string, opts Options) (bool, error) {
var args = []string{"is-failed", "--system", unit}
if opts.usermode {
args[1] = "--user"
}
stdout, _, _, err := execute(ctx, args)
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
}
// TODO
func IsActive(ctx context.Context, unit string, usermode bool) (bool, error) {
return false, nil
func IsActive(ctx context.Context, unit string, opts Options) (bool, error) {
var args = []string{"is-active", "--system", unit}
if opts.usermode {
args[1] = "--user"
}
stdout, _, _, err := execute(ctx, args)
if matched, _ := regexp.MatchString(`inactive`, stdout); matched {
return false, nil
} else if matched, _ := regexp.MatchString(`active`, stdout); matched {
return true, nil
} else if matched, _ := regexp.MatchString(`failed`, stdout); matched {
return false, nil
}
return false, err
}
// TODO
func Status(ctx context.Context, unit string, usermode bool) (bool, error) {
return false, nil
func IsEnabled(ctx context.Context, unit string, opts Options) (bool, error) {
var args = []string{"is-enabled", "--system", unit}
if opts.usermode {
args[1] = "--user"
}
stdout, _, _, err := execute(ctx, args)
if matched, _ := regexp.MatchString(`enabled`, stdout); matched {
return true, nil
} else if matched, _ := regexp.MatchString(`disabled`, stdout); matched {
return false, nil
}
return false, err
}
// TODO
func Restart(ctx context.Context, unit string, usermode bool) error {
return nil
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
}
// TODO
func Start(ctx context.Context, unit string, usermode bool) error {
return nil
func Restart(ctx context.Context, unit string, opts Options) error {
var args = []string{"restart", "--system", unit}
if opts.usermode {
args[1] = "--user"
}
_, _, _, err := execute(ctx, args)
return err
}
// TODO
func Stop(ctx context.Context, unit string, usermode bool) error {
return nil
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 Enable(ctx context.Context, unit string, usermode bool) error {
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 usermode {
if opts.usermode {
args[1] = "--user"
}
_, stderr, code, err := execute(ctx, args)
customErr := filterErr(stderr)
if customErr != nil {
return customErr
}
if err != nil {
return err
}
if code != 0 {
return fmt.Errorf("received error code %d for stderr `%s`: %w", code, stderr, ErrUnspecified)
}
return nil
_, _, _, err := execute(ctx, args)
return err
}
func Disable(ctx context.Context, unit string, usermode bool) error {
func Disable(ctx context.Context, unit string, opts Options) error {
var args = []string{"disable", "--system", unit}
if usermode {
if opts.usermode {
args[1] = "--user"
}
_, stderr, code, err := execute(ctx, args)
customErr := filterErr(stderr)
if customErr != nil {
return customErr
}
if err != nil {
return err
}
if code != 0 {
return fmt.Errorf("received error code %d for stderr `%s`: %w", code, stderr, ErrUnspecified)
}
return nil
_, _, _, err := execute(ctx, args)
return err
}
// TODO
func IsEnabled(ctx context.Context, unit string, usermode bool) (bool, error) {
return false, nil
}
// TODO
func DaemonReload(ctx context.Context, unit string, usermode bool) error {
return nil
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
}
//TODO
func Show(ctx context.Context, unit string, property string, usermode bool) (string, error) {
return "", nil
func Show(ctx context.Context, unit string, property string, opts Options) (string, error) {
var args = []string{"show", "--system", unit}
if opts.usermode {
args[1] = "--user"
}
_, _, _, err := execute(ctx, args)
return "", err
}
//TODO
func Mask(ctx context.Context, unit string, usermode bool) error {
return nil
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 Unmask(ctx context.Context, unit string, usermode bool) error {
return nil
func Unmask(ctx context.Context, unit string, opts Options) error {
var args = []string{"unmask", "--system", unit}
if opts.usermode {
args[1] = "--user"
}
_, _, _, err := execute(ctx, args)
return err
}