Add documentation and enable example
This commit is contained in:
parent
6b8241ed7c
commit
1ce6ec028a
@ -1,3 +1,4 @@
|
|||||||
|
[](https://pkg.go.dev/github.com/taigrr/systemctl)
|
||||||
# systemctl
|
# systemctl
|
||||||
|
|
||||||
This library aims at providing idiomatic `systemctl` bindings for go developers, in order to make it easier to write system tooling using golang.
|
This library aims at providing idiomatic `systemctl` bindings for go developers, in order to make it easier to write system tooling using golang.
|
||||||
|
15
systemctl.go
15
systemctl.go
@ -137,6 +137,8 @@ func IsFailed(ctx context.Context, unit string, opts Options) (bool, error) {
|
|||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Mask one or more units, as specified on the command line. This will link
|
||||||
|
// these unit files to /dev/null, making it impossible to start them.
|
||||||
func Mask(ctx context.Context, unit string, opts Options) error {
|
func Mask(ctx context.Context, unit string, opts Options) error {
|
||||||
var args = []string{"mask", "--system", unit}
|
var args = []string{"mask", "--system", unit}
|
||||||
if opts.UserMode {
|
if opts.UserMode {
|
||||||
@ -146,6 +148,8 @@ func Mask(ctx context.Context, unit string, opts Options) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Stop and then start one or more units specified on the command line.
|
||||||
|
// If the units are not running yet, they will be started.
|
||||||
func Restart(ctx context.Context, unit string, opts Options) error {
|
func Restart(ctx context.Context, unit string, opts Options) error {
|
||||||
var args = []string{"restart", "--system", unit}
|
var args = []string{"restart", "--system", unit}
|
||||||
if opts.UserMode {
|
if opts.UserMode {
|
||||||
@ -155,6 +159,8 @@ func Restart(ctx context.Context, unit string, opts Options) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Show a selected property of a unit. Accepted properties are predefined in the
|
||||||
|
// properties subpackage to guarantee properties are valid and assist code-completion.
|
||||||
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 {
|
||||||
@ -166,6 +172,7 @@ func Show(ctx context.Context, unit string, property properties.Property, opts O
|
|||||||
return stdout, err
|
return stdout, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Start (activate) a given unit
|
||||||
func Start(ctx context.Context, unit string, opts Options) error {
|
func Start(ctx context.Context, unit string, opts Options) error {
|
||||||
var args = []string{"start", "--system", unit}
|
var args = []string{"start", "--system", unit}
|
||||||
if opts.UserMode {
|
if opts.UserMode {
|
||||||
@ -175,6 +182,11 @@ func Start(ctx context.Context, unit string, opts Options) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get back the status string which would be returned by running
|
||||||
|
// `systemctl status [unit]`.
|
||||||
|
//
|
||||||
|
// Generally, it makes more sense to programatically retrieve the properties
|
||||||
|
// using Show, but this command is provided for the sake of completeness
|
||||||
func Status(ctx context.Context, unit string, opts Options) (string, error) {
|
func Status(ctx context.Context, unit string, opts Options) (string, error) {
|
||||||
var args = []string{"status", "--system", unit}
|
var args = []string{"status", "--system", unit}
|
||||||
if opts.UserMode {
|
if opts.UserMode {
|
||||||
@ -184,6 +196,7 @@ func Status(ctx context.Context, unit string, opts Options) (string, error) {
|
|||||||
return stdout, err
|
return stdout, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Stop (deactivate) a given unit
|
||||||
func Stop(ctx context.Context, unit string, opts Options) error {
|
func Stop(ctx context.Context, unit string, opts Options) error {
|
||||||
var args = []string{"stop", "--system", unit}
|
var args = []string{"stop", "--system", unit}
|
||||||
if opts.UserMode {
|
if opts.UserMode {
|
||||||
@ -193,6 +206,8 @@ func Stop(ctx context.Context, unit string, opts Options) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Unmask one or more unit files, as specified on the command line.
|
||||||
|
// This will undo the effect of Mask.
|
||||||
func Unmask(ctx context.Context, unit string, opts Options) error {
|
func Unmask(ctx context.Context, unit string, opts Options) error {
|
||||||
var args = []string{"unmask", "--system", unit}
|
var args = []string{"unmask", "--system", unit}
|
||||||
if opts.UserMode {
|
if opts.UserMode {
|
||||||
|
@ -82,8 +82,8 @@ func TestEnable(t *testing.T) {
|
|||||||
}
|
}
|
||||||
unit := "nginx"
|
unit := "nginx"
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
err := Mask(ctx, unit, Options{UserMode: false})
|
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
err := Mask(ctx, unit, Options{UserMode: false})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Unmask(ctx, unit, Options{UserMode: false})
|
Unmask(ctx, unit, Options{UserMode: false})
|
||||||
t.Errorf("Unable to mask %s", unit)
|
t.Errorf("Unable to mask %s", unit)
|
||||||
@ -100,6 +100,27 @@ func TestEnable(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
func ExampleEnable() {
|
||||||
|
unit := "syncthing"
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
err := Enable(ctx, unit, Options{UserMode: true})
|
||||||
|
switch err {
|
||||||
|
case ErrMasked:
|
||||||
|
fmt.Printf("%s is masked, unmask it before enabling\n", unit)
|
||||||
|
case ErrDoesNotExist:
|
||||||
|
fmt.Printf("%s does not exist\n", unit)
|
||||||
|
case ErrInsufficientPermissions:
|
||||||
|
fmt.Printf("permission to enable %s denied\n", unit)
|
||||||
|
case ErrBusFailure:
|
||||||
|
fmt.Printf("Cannot communicate with the bus\n")
|
||||||
|
case nil:
|
||||||
|
fmt.Printf("%s enabled successfully\n", unit)
|
||||||
|
default:
|
||||||
|
fmt.Printf("Error: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestDisable(t *testing.T) {
|
func TestDisable(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user