adds option structs, ready for Show implementation
This commit is contained in:
parent
7bedb452e4
commit
8a7c865b5f
@ -46,9 +46,11 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
userMode := false
|
|
||||||
// Equivalent to `systemctl enable nginx` with a 10 second timeout
|
// Equivalent to `systemctl enable nginx` with a 10 second timeout
|
||||||
err := systemctl.Enable(ctx, "nginx", userMode)
|
opts := Options{
|
||||||
|
usermode: false,
|
||||||
|
}
|
||||||
|
err := Enable(ctx, unit, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("unable to enable unit %s: %v", "nginx", err)
|
log.Fatalf("unable to enable unit %s: %v", "nginx", err)
|
||||||
}
|
}
|
||||||
|
5
structs.go
Normal file
5
structs.go
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package systemctl
|
||||||
|
|
||||||
|
type Options struct {
|
||||||
|
usermode bool
|
||||||
|
}
|
171
systemctl.go
171
systemctl.go
@ -2,98 +2,143 @@ package systemctl
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"regexp"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO
|
func IsFailed(ctx context.Context, unit string, opts Options) (bool, error) {
|
||||||
func IsFailed(ctx context.Context, unit string, usermode bool) (bool, error) {
|
var args = []string{"is-failed", "--system", unit}
|
||||||
|
if opts.usermode {
|
||||||
return false, nil
|
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, opts Options) (bool, error) {
|
||||||
func IsActive(ctx context.Context, unit string, usermode bool) (bool, error) {
|
var args = []string{"is-active", "--system", unit}
|
||||||
return false, nil
|
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 IsEnabled(ctx context.Context, unit string, opts Options) (bool, error) {
|
||||||
func Status(ctx context.Context, unit string, usermode bool) (bool, error) {
|
var args = []string{"is-enabled", "--system", unit}
|
||||||
return false, nil
|
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 Status(ctx context.Context, unit string, opts Options) (string, error) {
|
||||||
func Restart(ctx context.Context, unit string, usermode bool) error {
|
var args = []string{"status", "--system", unit}
|
||||||
return nil
|
if opts.usermode {
|
||||||
|
args[1] = "--user"
|
||||||
|
}
|
||||||
|
stdout, _, _, err := execute(ctx, args)
|
||||||
|
return stdout, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
func Restart(ctx context.Context, unit string, opts Options) error {
|
||||||
func Start(ctx context.Context, unit string, usermode bool) error {
|
var args = []string{"restart", "--system", unit}
|
||||||
return nil
|
if opts.usermode {
|
||||||
|
args[1] = "--user"
|
||||||
|
}
|
||||||
|
_, _, _, err := execute(ctx, args)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
func Start(ctx context.Context, unit string, opts Options) error {
|
||||||
func Stop(ctx context.Context, unit string, usermode bool) error {
|
var args = []string{"start", "--system", unit}
|
||||||
return nil
|
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}
|
var args = []string{"enable", "--system", unit}
|
||||||
if usermode {
|
if opts.usermode {
|
||||||
args[1] = "--user"
|
args[1] = "--user"
|
||||||
}
|
}
|
||||||
_, stderr, code, err := execute(ctx, args)
|
_, _, _, err := execute(ctx, args)
|
||||||
customErr := filterErr(stderr)
|
return err
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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}
|
var args = []string{"disable", "--system", unit}
|
||||||
if usermode {
|
if opts.usermode {
|
||||||
args[1] = "--user"
|
args[1] = "--user"
|
||||||
}
|
}
|
||||||
_, stderr, code, err := execute(ctx, args)
|
_, _, _, err := execute(ctx, args)
|
||||||
customErr := filterErr(stderr)
|
return err
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
func DaemonReload(ctx context.Context, opts Options) error {
|
||||||
func IsEnabled(ctx context.Context, unit string, usermode bool) (bool, error) {
|
var args = []string{"daemon-reload", "--system"}
|
||||||
return false, nil
|
if opts.usermode {
|
||||||
}
|
args[1] = "--user"
|
||||||
|
}
|
||||||
// TODO
|
_, _, _, err := execute(ctx, args)
|
||||||
func DaemonReload(ctx context.Context, unit string, usermode bool) error {
|
return err
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO
|
//TODO
|
||||||
func Show(ctx context.Context, unit string, property string, usermode bool) (string, error) {
|
func Show(ctx context.Context, unit string, property string, opts Options) (string, error) {
|
||||||
return "", nil
|
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, opts Options) error {
|
||||||
func Mask(ctx context.Context, unit string, usermode bool) error {
|
var args = []string{"mask", "--system", unit}
|
||||||
return nil
|
if opts.usermode {
|
||||||
|
args[1] = "--user"
|
||||||
|
}
|
||||||
|
_, _, _, err := execute(ctx, args)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func Unmask(ctx context.Context, unit string, usermode bool) error {
|
func Unmask(ctx context.Context, unit string, opts Options) error {
|
||||||
return nil
|
var args = []string{"unmask", "--system", unit}
|
||||||
|
if opts.usermode {
|
||||||
|
args[1] = "--user"
|
||||||
|
}
|
||||||
|
_, _, _, err := execute(ctx, args)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,10 @@ func TestEnableNonexistant(t *testing.T) {
|
|||||||
unit := "nonexistant"
|
unit := "nonexistant"
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
err := Enable(ctx, unit, true)
|
opts := Options{
|
||||||
|
usermode: true,
|
||||||
|
}
|
||||||
|
err := Enable(ctx, unit, opts)
|
||||||
if err != ErrDoesNotExist {
|
if err != ErrDoesNotExist {
|
||||||
t.Errorf("error is %v, but should have been %v", err, ErrDoesNotExist)
|
t.Errorf("error is %v, but should have been %v", err, ErrDoesNotExist)
|
||||||
}
|
}
|
||||||
@ -23,7 +26,10 @@ func TestEnableNoPermissions(t *testing.T) {
|
|||||||
unit := "nginx"
|
unit := "nginx"
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
err := Enable(ctx, unit, false)
|
opts := Options{
|
||||||
|
usermode: false,
|
||||||
|
}
|
||||||
|
err := Enable(ctx, unit, opts)
|
||||||
if err != ErrInsufficientPermissions {
|
if err != ErrInsufficientPermissions {
|
||||||
t.Errorf("error is %v, but should have been %v", err, ErrInsufficientPermissions)
|
t.Errorf("error is %v, but should have been %v", err, ErrInsufficientPermissions)
|
||||||
}
|
}
|
||||||
@ -37,7 +43,10 @@ func TestEnableSuccess(t *testing.T) {
|
|||||||
unit := "syncthing"
|
unit := "syncthing"
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
err := Enable(ctx, unit, true)
|
opts := Options{
|
||||||
|
usermode: true,
|
||||||
|
}
|
||||||
|
err := Enable(ctx, unit, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("error is %v, but should have been %v", err, nil)
|
t.Errorf("error is %v, but should have been %v", err, nil)
|
||||||
}
|
}
|
||||||
|
26
util.go
26
util.go
@ -3,6 +3,7 @@ package systemctl
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"regexp"
|
"regexp"
|
||||||
)
|
)
|
||||||
@ -37,27 +38,32 @@ 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()
|
||||||
|
|
||||||
|
customErr := filterErr(warnings)
|
||||||
|
if customErr != nil {
|
||||||
|
err = customErr
|
||||||
|
}
|
||||||
|
if code != 0 && err == nil {
|
||||||
|
err = fmt.Errorf("received error code %d for stderr `%s`: %w", code, warnings, ErrUnspecified)
|
||||||
|
}
|
||||||
|
|
||||||
return output, warnings, code, err
|
return output, warnings, code, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func filterErr(stderr string) error {
|
func filterErr(stderr string) error {
|
||||||
matched, _ := regexp.MatchString(`does not exist`, stderr)
|
if matched, _ := regexp.MatchString(`does not exist`, stderr); matched {
|
||||||
if matched {
|
|
||||||
return ErrDoesNotExist
|
return ErrDoesNotExist
|
||||||
}
|
}
|
||||||
matched, _ = regexp.MatchString(`Interactive authentication required`, stderr)
|
if matched, _ := regexp.MatchString(`No such file or directory`, stderr); matched {
|
||||||
if matched {
|
return ErrDoesNotExist
|
||||||
|
}
|
||||||
|
if matched, _ := regexp.MatchString(`Interactive authentication required`, stderr); matched {
|
||||||
return ErrInsufficientPermissions
|
return ErrInsufficientPermissions
|
||||||
}
|
}
|
||||||
matched, _ = regexp.MatchString(`Access denied`, stderr)
|
if matched, _ := regexp.MatchString(`Access denied`, stderr); matched {
|
||||||
if matched {
|
|
||||||
return ErrInsufficientPermissions
|
return ErrInsufficientPermissions
|
||||||
}
|
}
|
||||||
|
if matched, _ := regexp.MatchString(`Failed`, stderr); matched {
|
||||||
matched, _ = regexp.MatchString(`Failed`, stderr)
|
|
||||||
if matched {
|
|
||||||
return ErrUnspecified
|
return ErrUnspecified
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user