diff --git a/go.mod b/go.mod index 9c23437..a691c5d 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/taigrr/systemctl go 1.16 + +require golang.org/x/tools v0.1.1 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..49a0a97 --- /dev/null +++ b/go.sum @@ -0,0 +1,33 @@ +github.com/yuin/goldmark v1.3.5 h1:dPmz1Snjq0kmkz159iL7S6WzdahUTHnHB5M56WFVifs= +github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo= +golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 h1:4nGaVu0QrbjT/AK2PRLuQfQuh6DJve+pELhqTdAj3x0= +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE= +golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.1 h1:wGiQel/hW0NnEkJUk8lbzkX2gFJU6PFxf1v5OlCfuOs= +golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/helpers_test.go b/helpers_test.go index caf60ce..7257829 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -24,20 +24,20 @@ func TestGetStartTime(t *testing.T) { }{ // Run these tests only as a user //try nonexistant unit in user mode as user - {"nonexistant", ErrUnitNotRunning, Options{usermode: false}, true}, + {"nonexistant", ErrUnitNotRunning, Options{UserMode: false}, true}, // try existing unit in user mode as user - {"syncthing", ErrUnitNotRunning, Options{usermode: true}, true}, + {"syncthing", ErrUnitNotRunning, Options{UserMode: true}, true}, // 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 // try nonexistant unit in system mode as system - {"nonexistant", ErrUnitNotRunning, Options{usermode: false}, false}, + {"nonexistant", ErrUnitNotRunning, Options{UserMode: false}, false}, // 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 - {"nginx", nil, Options{usermode: false}, false}, + {"nginx", nil, Options{UserMode: false}, false}, } for _, tc := range testCases { t.Run(fmt.Sprintf("%s as %s", tc.unit, userString), func(t *testing.T) { @@ -62,15 +62,15 @@ func TestGetStartTime(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) defer cancel() - startTime, err := GetStartTime(ctx, "nginx", Options{usermode: false}) + startTime, err := GetStartTime(ctx, "nginx", Options{UserMode: false}) if err != nil { t.Errorf("issue getting start time of nginx: %v", err) } - err = Restart(ctx, "nginx", Options{usermode: false}) + err = Restart(ctx, "nginx", Options{UserMode: false}) if err != nil { t.Errorf("issue restarting nginx as %s: %v", userString, err) } - newStartTime, err := GetStartTime(ctx, "nginx", Options{usermode: false}) + newStartTime, err := GetStartTime(ctx, "nginx", Options{UserMode: false}) if err != nil { t.Errorf("issue getting second start time of nginx: %v", err) } @@ -92,20 +92,20 @@ func TestGetMemoryUsage(t *testing.T) { // Run these tests only as a user //try nonexistant unit in user mode as user - {"nonexistant", ErrValueNotSet, Options{usermode: false}, true}, + {"nonexistant", ErrValueNotSet, Options{UserMode: false}, true}, // try existing unit in user mode as user - {"syncthing", ErrValueNotSet, Options{usermode: true}, true}, + {"syncthing", ErrValueNotSet, Options{UserMode: true}, true}, // 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 // try nonexistant unit in system mode as system - {"nonexistant", ErrValueNotSet, Options{usermode: false}, false}, + {"nonexistant", ErrValueNotSet, Options{UserMode: false}, false}, // 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 - {"nginx", nil, Options{usermode: false}, false}, + {"nginx", nil, Options{UserMode: false}, false}, } for _, tc := range testCases { t.Run(fmt.Sprintf("%s as %s", tc.unit, userString), func(t *testing.T) { @@ -127,11 +127,11 @@ func TestGetMemoryUsage(t *testing.T) { t.Run(fmt.Sprintf("prove memory usage values change across services"), func(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) defer cancel() - bytes, err := GetMemoryUsage(ctx, "nginx", Options{usermode: false}) + bytes, err := GetMemoryUsage(ctx, "nginx", Options{UserMode: false}) if err != nil { t.Errorf("issue getting memory usage of nginx: %v", err) } - secondBytes, err := GetMemoryUsage(ctx, "user.slice", Options{usermode: false}) + secondBytes, err := GetMemoryUsage(ctx, "user.slice", Options{UserMode: false}) if err != nil { t.Errorf("issue getting second memort usage reading of nginx: %v", err) } @@ -152,20 +152,20 @@ func TestGetPID(t *testing.T) { // Run these tests only as a user //try nonexistant unit in user mode as user - {"nonexistant", nil, Options{usermode: false}, true}, + {"nonexistant", nil, Options{UserMode: false}, true}, // try existing unit in user mode as user - {"syncthing", nil, Options{usermode: true}, true}, + {"syncthing", nil, Options{UserMode: true}, true}, // 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 // try nonexistant unit in system mode as system - {"nonexistant", nil, Options{usermode: false}, false}, + {"nonexistant", nil, Options{UserMode: false}, false}, // 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 - {"nginx", nil, Options{usermode: false}, false}, + {"nginx", nil, Options{UserMode: false}, false}, } for _, tc := range testCases { t.Run(fmt.Sprintf("%s as %s", tc.unit, userString), func(t *testing.T) { diff --git a/structs.go b/structs.go index def6794..2a48a73 100644 --- a/structs.go +++ b/structs.go @@ -1,5 +1,5 @@ package systemctl type Options struct { - usermode bool + UserMode bool } diff --git a/systemctl.go b/systemctl.go index eaa8316..7883936 100644 --- a/systemctl.go +++ b/systemctl.go @@ -10,7 +10,7 @@ import ( 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" } stdout, _, _, err := execute(ctx, args) @@ -26,7 +26,7 @@ func IsFailed(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} - if opts.usermode { + if opts.UserMode { args[1] = "--user" } stdout, _, _, err := execute(ctx, args) @@ -43,7 +43,7 @@ func IsActive(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} - if opts.usermode { + if opts.UserMode { args[1] = "--user" } stdout, _, _, err := execute(ctx, args) @@ -60,7 +60,7 @@ func IsEnabled(ctx context.Context, unit string, opts Options) (bool, error) { func Status(ctx context.Context, unit string, opts Options) (string, error) { var args = []string{"status", "--system", unit} - if opts.usermode { + if opts.UserMode { args[1] = "--user" } stdout, _, _, err := execute(ctx, args) @@ -69,7 +69,7 @@ func Status(ctx context.Context, unit string, opts Options) (string, error) { func Restart(ctx context.Context, unit string, opts Options) error { var args = []string{"restart", "--system", unit} - if opts.usermode { + if opts.UserMode { args[1] = "--user" } _, _, _, err := execute(ctx, args) @@ -78,7 +78,7 @@ func Restart(ctx context.Context, unit string, opts Options) error { func Start(ctx context.Context, unit string, opts Options) error { var args = []string{"start", "--system", unit} - if opts.usermode { + if opts.UserMode { args[1] = "--user" } _, _, _, err := execute(ctx, args) @@ -87,7 +87,7 @@ func Start(ctx context.Context, unit string, opts Options) error { 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" } _, _, _, err := execute(ctx, args) @@ -96,7 +96,7 @@ func Stop(ctx context.Context, unit string, opts Options) error { func Enable(ctx context.Context, unit string, opts Options) error { var args = []string{"enable", "--system", unit} - if opts.usermode { + if opts.UserMode { args[1] = "--user" } _, _, _, err := execute(ctx, args) @@ -105,7 +105,7 @@ func Enable(ctx context.Context, unit string, opts Options) error { func Disable(ctx context.Context, unit string, opts Options) error { var args = []string{"disable", "--system", unit} - if opts.usermode { + if opts.UserMode { args[1] = "--user" } _, _, _, err := execute(ctx, args) @@ -114,7 +114,7 @@ func Disable(ctx context.Context, unit string, opts Options) error { func DaemonReload(ctx context.Context, opts Options) error { var args = []string{"daemon-reload", "--system"} - if opts.usermode { + if opts.UserMode { args[1] = "--user" } _, _, _, err := execute(ctx, args) @@ -123,7 +123,7 @@ func DaemonReload(ctx context.Context, opts Options) error { func Show(ctx context.Context, unit string, property properties.Property, opts Options) (string, error) { var args = []string{"show", "--system", unit, "--property", string(property)} - if opts.usermode { + if opts.UserMode { args[1] = "--user" } stdout, _, _, err := execute(ctx, args) @@ -134,7 +134,7 @@ func Show(ctx context.Context, unit string, property properties.Property, opts O func Mask(ctx context.Context, unit string, opts Options) error { var args = []string{"mask", "--system", unit} - if opts.usermode { + if opts.UserMode { args[1] = "--user" } _, _, _, err := execute(ctx, args) @@ -143,7 +143,7 @@ func Mask(ctx context.Context, unit string, opts Options) error { func Unmask(ctx context.Context, unit string, opts Options) error { var args = []string{"unmask", "--system", unit} - if opts.usermode { + if opts.UserMode { args[1] = "--user" } _, _, _, err := execute(ctx, args) diff --git a/systemctl_test.go b/systemctl_test.go index 629122e..9be16bf 100644 --- a/systemctl_test.go +++ b/systemctl_test.go @@ -43,22 +43,22 @@ func TestEnable(t *testing.T) { // Run these tests only as a user //try nonexistant unit in user mode as user - {"nonexistant", ErrDoesNotExist, Options{usermode: true}, true}, + {"nonexistant", ErrDoesNotExist, Options{UserMode: true}, true}, // try existing unit in user mode as user - {"syncthing", nil, Options{usermode: true}, true}, + {"syncthing", nil, Options{UserMode: true}, true}, // try nonexisting unit in system mode as user - {"nonexistant", ErrInsufficientPermissions, Options{usermode: false}, true}, + {"nonexistant", ErrInsufficientPermissions, Options{UserMode: false}, true}, // try existing unit in system mode as user - {"nginx", ErrInsufficientPermissions, Options{usermode: false}, true}, + {"nginx", ErrInsufficientPermissions, Options{UserMode: false}, true}, // Run these tests only as a superuser // try nonexistant unit in system mode as system - {"nonexistant", ErrDoesNotExist, Options{usermode: false}, false}, + {"nonexistant", ErrDoesNotExist, Options{UserMode: false}, false}, // 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 - {"nginx", nil, Options{usermode: false}, false}, + {"nginx", nil, Options{UserMode: false}, false}, } for _, tc := range testCases { t.Run(fmt.Sprintf("%s as %s", tc.unit, userString), func(t *testing.T) { @@ -82,18 +82,18 @@ func TestEnable(t *testing.T) { } unit := "nginx" ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) - err := Mask(ctx, unit, Options{usermode: false}) + err := Mask(ctx, unit, Options{UserMode: false}) defer cancel() if err != nil { - Unmask(ctx, unit, Options{usermode: false}) + Unmask(ctx, unit, Options{UserMode: false}) t.Errorf("Unable to mask %s", unit) } - err = Enable(ctx, unit, Options{usermode: false}) + err = Enable(ctx, unit, Options{UserMode: false}) if err != ErrMasked { - Unmask(ctx, unit, Options{usermode: false}) + Unmask(ctx, unit, Options{UserMode: false}) t.Errorf("error is %v, but should have been %v", err, ErrMasked) } - err = Unmask(ctx, unit, Options{usermode: false}) + err = Unmask(ctx, unit, Options{UserMode: false}) if err != nil { t.Errorf("Unable to unmask %s", unit) } @@ -108,25 +108,29 @@ func TestDisable(t *testing.T) { opts Options runAsUser bool }{ - // Run these tests only as a user + /* Run these tests only as a user */ //try nonexistant unit in user mode as user - {"nonexistant", ErrDoesNotExist, Options{usermode: true}, true}, + {"nonexistant", ErrDoesNotExist, Options{UserMode: true}, true}, // try existing unit in user mode as user - {"syncthing", nil, Options{usermode: true}, true}, + {"syncthing", nil, Options{UserMode: true}, true}, // try nonexisting unit in system mode as user - {"nonexistant", ErrInsufficientPermissions, Options{usermode: false}, true}, + {"nonexistant", ErrInsufficientPermissions, Options{UserMode: false}, true}, // try existing unit in system mode as user - {"nginx", ErrInsufficientPermissions, Options{usermode: false}, true}, + {"nginx", ErrInsufficientPermissions, Options{UserMode: false}, true}, - // Run these tests only as a superuser + /* End user tests*/ + + /* Run these tests only as a superuser */ // try nonexistant unit in system mode as system - {"nonexistant", ErrDoesNotExist, Options{usermode: false}, false}, + {"nonexistant", ErrDoesNotExist, Options{UserMode: false}, false}, // 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 - {"nginx", nil, Options{usermode: false}, false}, + {"nginx", nil, Options{UserMode: false}, false}, + + /* End superuser tests*/ } for _, tc := range testCases { t.Run(fmt.Sprintf("%s as %s", tc.unit, userString), func(t *testing.T) { @@ -149,18 +153,18 @@ func TestDisable(t *testing.T) { } unit := "nginx" ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) - err := Mask(ctx, unit, Options{usermode: false}) + err := Mask(ctx, unit, Options{UserMode: false}) defer cancel() if err != nil { - Unmask(ctx, unit, Options{usermode: false}) + Unmask(ctx, unit, Options{UserMode: false}) t.Errorf("Unable to mask %s", unit) } - err = Disable(ctx, unit, Options{usermode: false}) + err = Disable(ctx, unit, Options{UserMode: false}) if err != ErrMasked { - Unmask(ctx, unit, Options{usermode: false}) + Unmask(ctx, unit, Options{UserMode: false}) t.Errorf("error is %v, but should have been %v", err, ErrMasked) } - err = Unmask(ctx, unit, Options{usermode: false}) + err = Unmask(ctx, unit, Options{UserMode: false}) if err != nil { t.Errorf("Unable to unmask %s", unit) } @@ -175,7 +179,7 @@ func TestShow(t *testing.T) { } unit := "nginx" opts := Options{ - usermode: false, + UserMode: false, } for _, x := range properties.Properties { t.Run(fmt.Sprintf("show property %s", string(x)), func(t *testing.T) {