mirror of
https://github.com/taigrr/systemctl.git
synced 2026-04-09 22:11:22 -07:00
Compare commits
1 Commits
cd/update-
...
cd/filtere
| Author | SHA1 | Date | |
|---|---|---|---|
| 5518d863a8 |
4
.github/workflows/ci.yml
vendored
4
.github/workflows/ci.yml
vendored
@@ -15,7 +15,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
go-version: ["1.26", "1.26.2"]
|
||||
go-version: ["1.26"]
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
@@ -39,7 +39,7 @@ jobs:
|
||||
run: sudo go test -race -coverprofile=coverage-root.out -covermode=atomic ./...
|
||||
|
||||
- name: Upload coverage to Codecov
|
||||
if: matrix.go-version == '1.26.2'
|
||||
if: matrix.go-version == '1.26'
|
||||
uses: codecov/codecov-action@v5
|
||||
with:
|
||||
files: coverage-user.out,coverage-root.out
|
||||
|
||||
@@ -22,7 +22,6 @@ If your system isn't running (or targeting another system running) `systemctl`,
|
||||
- [x] `systemctl is-enabled`
|
||||
- [x] `systemctl is-failed`
|
||||
- [x] `systemctl mask`
|
||||
- [x] `systemctl reload`
|
||||
- [x] `systemctl restart`
|
||||
- [x] `systemctl show`
|
||||
- [x] `systemctl start`
|
||||
@@ -36,13 +35,6 @@ If your system isn't running (or targeting another system running) `systemctl`,
|
||||
- [x] Get current memory in bytes (`MemoryCurrent`) as an int
|
||||
- [x] Get the PID of the main process (`MainPID`) as an int
|
||||
- [x] Get the restart count of a unit (`NRestarts`) as an int
|
||||
- [x] List all loaded units and their states (`list-units`)
|
||||
- [x] List masked units (`list-unit-files --state=masked`)
|
||||
- [x] Get sockets associated with a service unit (`list-sockets`)
|
||||
- [x] Check if a unit is masked
|
||||
- [x] Check if a unit is running (sub-state)
|
||||
- [x] Check if systemd is the init system (`/proc/1/comm`)
|
||||
- [x] Validate unit name suffixes against known systemd unit types
|
||||
|
||||
|
||||
## Useful errors
|
||||
@@ -62,6 +54,7 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
|
||||
15
helpers.go
15
helpers.go
@@ -74,7 +74,10 @@ func GetPID(ctx context.Context, unit string, opts Options) (int, error) {
|
||||
|
||||
// GetSocketsForServiceUnit returns the socket units associated with a given service unit.
|
||||
func GetSocketsForServiceUnit(ctx context.Context, unit string, opts Options) ([]string, error) {
|
||||
args := prepareArgs("list-sockets", opts, "--all", "--no-legend", "--no-pager")
|
||||
args := []string{"list-sockets", "--all", "--no-legend", "--no-pager"}
|
||||
if opts.UserMode {
|
||||
args = append(args, "--user")
|
||||
}
|
||||
stdout, _, _, err := execute(ctx, args)
|
||||
if err != nil {
|
||||
return []string{}, err
|
||||
@@ -97,7 +100,10 @@ func GetSocketsForServiceUnit(ctx context.Context, unit string, opts Options) ([
|
||||
|
||||
// GetUnits returns a list of all loaded units and their states.
|
||||
func GetUnits(ctx context.Context, opts Options) ([]Unit, error) {
|
||||
args := prepareArgs("list-units", opts, "--all", "--no-legend", "--full", "--no-pager")
|
||||
args := []string{"list-units", "--all", "--no-legend", "--full", "--no-pager"}
|
||||
if opts.UserMode {
|
||||
args = append(args, "--user")
|
||||
}
|
||||
stdout, stderr, _, err := execute(ctx, args)
|
||||
if err != nil {
|
||||
return []Unit{}, errors.Join(err, filterErr(stderr))
|
||||
@@ -123,7 +129,10 @@ func GetUnits(ctx context.Context, opts Options) ([]Unit, error) {
|
||||
|
||||
// GetMaskedUnits returns a list of all masked unit names.
|
||||
func GetMaskedUnits(ctx context.Context, opts Options) ([]string, error) {
|
||||
args := prepareArgs("list-unit-files", opts, "--state=masked")
|
||||
args := []string{"list-unit-files", "--state=masked"}
|
||||
if opts.UserMode {
|
||||
args = append(args, "--user")
|
||||
}
|
||||
stdout, stderr, _, err := execute(ctx, args)
|
||||
if err != nil {
|
||||
return []string{}, errors.Join(err, filterErr(stderr))
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
//go:build linux
|
||||
|
||||
package systemctl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestReloadBuildsExpectedCommand(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
opts Options
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
name: "system mode",
|
||||
opts: Options{},
|
||||
want: []string{"reload", "--system", "nginx.service"},
|
||||
},
|
||||
{
|
||||
name: "user mode",
|
||||
opts: Options{UserMode: true},
|
||||
want: []string{"reload", "--user", "nginx.service"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
logFile := filepath.Join(tempDir, "args.log")
|
||||
fakeSystemctl := filepath.Join(tempDir, "systemctl")
|
||||
script := "#!/bin/sh\nprintf '%s\\n' \"$@\" > '" + logFile + "'\n"
|
||||
|
||||
if err := os.WriteFile(fakeSystemctl, []byte(script), 0o755); err != nil {
|
||||
t.Fatalf("write fake systemctl: %v", err)
|
||||
}
|
||||
|
||||
original := systemctl
|
||||
systemctl = fakeSystemctl
|
||||
t.Cleanup(func() {
|
||||
systemctl = original
|
||||
})
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
|
||||
if err := Reload(ctx, "nginx.service", tt.opts); err != nil {
|
||||
t.Fatalf("Reload returned error: %v", err)
|
||||
}
|
||||
|
||||
gotBytes, err := os.ReadFile(logFile)
|
||||
if err != nil {
|
||||
t.Fatalf("read captured args: %v", err)
|
||||
}
|
||||
got := strings.Fields(strings.TrimSpace(string(gotBytes)))
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Fatalf("Reload args = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -104,13 +104,6 @@ func Restart(ctx context.Context, unit string, opts Options, args ...string) err
|
||||
return restart(ctx, unit, opts, args...)
|
||||
}
|
||||
|
||||
// Reload one or more units if they support reload.
|
||||
//
|
||||
// Any additional arguments are passed directly to the systemctl command.
|
||||
func Reload(ctx context.Context, unit string, opts Options, args ...string) error {
|
||||
return reload(ctx, unit, opts, args...)
|
||||
}
|
||||
|
||||
// Show a selected property of a unit. Accepted properties are predefined in the
|
||||
// properties subpackage to guarantee properties are valid and assist code-completion.
|
||||
//
|
||||
|
||||
@@ -44,10 +44,6 @@ func restart(_ context.Context, _ string, _ Options, _ ...string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func reload(_ context.Context, _ string, _ Options, _ ...string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func show(_ context.Context, _ string, _ properties.Property, _ Options, _ ...string) (string, error) {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
@@ -115,12 +115,6 @@ func restart(ctx context.Context, unit string, opts Options, args ...string) err
|
||||
return err
|
||||
}
|
||||
|
||||
func reload(ctx context.Context, unit string, opts Options, args ...string) error {
|
||||
a := prepareArgs("reload", opts, append([]string{unit}, args...)...)
|
||||
_, _, _, err := execute(ctx, a)
|
||||
return err
|
||||
}
|
||||
|
||||
func show(ctx context.Context, unit string, property properties.Property, opts Options, args ...string) (string, error) {
|
||||
extra := append([]string{unit, "--property", string(property)}, args...)
|
||||
a := prepareArgs("show", opts, extra...)
|
||||
|
||||
Reference in New Issue
Block a user