mirror of
https://github.com/taigrr/systemctl.git
synced 2025-10-06 08:47:02 +08:00
add IsMasked utility
This commit is contained in:
45
helpers.go
45
helpers.go
@@ -2,7 +2,9 @@ package systemctl
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/taigrr/systemctl/properties"
|
"github.com/taigrr/systemctl/properties"
|
||||||
@@ -52,3 +54,46 @@ func GetPID(ctx context.Context, unit string, opts Options) (int, error) {
|
|||||||
}
|
}
|
||||||
return strconv.Atoi(value)
|
return strconv.Atoi(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetMaskedUnits(ctx context.Context, opts Options) ([]string, error) {
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
lines := strings.Split(stdout, "\n")
|
||||||
|
units := []string{}
|
||||||
|
for _, line := range lines {
|
||||||
|
if !strings.Contains(line, "masked") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
entry := strings.Split(line, " ")
|
||||||
|
if len(entry) < 3 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if entry[1] == "masked" {
|
||||||
|
unit := entry[0]
|
||||||
|
uName := strings.Split(unit, ".")
|
||||||
|
unit = uName[0]
|
||||||
|
units = append(units, unit)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return units, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if a service is masked
|
||||||
|
func IsMasked(ctx context.Context, unit string, opts Options) (bool, error) {
|
||||||
|
units, err := GetMaskedUnits(ctx, opts)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
for _, u := range units {
|
||||||
|
if u == unit {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user