Files
iris-context/commands/sys/ps.go
T
VERSE ffa336196a chore: use go fix to enhance codebase (#29)
This pull request modernizes the codebase by adopting Go 1.21+ features, including built-in functions like min and max, standard library iterators such as strings.SplitSeq and strings.FieldsSeq, utility functions like maps.Copy and slices.Contains, and string manipulation helpers like strings.Cut and strings.CutPrefix. Feedback is provided on a test file where the usage of strings.Builder can be further optimized to avoid unnecessary string allocations during concatenation.

`go fix -diff ./...`
2026-07-04 19:44:38 +07:00

110 lines
2.7 KiB
Go

package sys
import (
"context"
"os/exec"
"strings"
"time"
"github.com/versenilvis/iris/commands/core"
)
func processGenerator(tokens []string, _ string, _ string) []core.Suggestion {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// output: "pid command"
cmd := exec.CommandContext(ctx, "ps", "-eo", "pid,comm")
out, err := cmd.Output()
if err != nil {
return nil
}
seen := make(map[string]bool)
var results []core.Suggestion
for line := range strings.SplitSeq(string(out), "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
parts := strings.Fields(line)
if len(parts) < 2 {
continue
}
pid := parts[0]
if pid[0] < '0' || pid[0] > '9' {
continue
}
name := parts[1]
// suggest both PID and process name
if !seen[pid] {
seen[pid] = true
results = append(results, core.Suggestion{Cmd: pid, Desc: name})
}
}
return results
}
func init() {
core.Register(&core.Spec{
Name: "ps",
Description: "report processes",
Options: []core.Option{
{Name: "-e", Description: "all processes"},
{Name: "-f", Description: "full format"},
{Name: "-u", Description: "by user"},
{Name: "aux", Description: "all + user + x"},
{Name: "-o", Description: "custom format"},
{Name: "--pid", Description: "filter by PID"},
{Name: "--sort", Description: "sort by field"},
},
})
core.Register(&core.Spec{
Name: "kill",
Description: "send signal to process",
Generator: processGenerator,
Options: []core.Option{
{Name: "-9", Description: "SIGKILL (force)"},
{Name: "-15", Description: "SIGTERM (graceful)"},
{Name: "-2", Description: "SIGINT"},
{Name: "-HUP", Description: "SIGHUP (reload)"},
{Name: "-l", Description: "list signal names"},
},
})
core.Register(&core.Spec{
Name: "killall",
Description: "kill by process name",
Generator: processGenerator,
Options: []core.Option{
{Name: "-9", Description: "SIGKILL"},
{Name: "-s", Description: "specify signal"},
{Name: "-u", Description: "only for user"},
{Name: "-i", Description: "confirm each"},
},
})
core.Register(&core.Spec{
Name: "pkill",
Description: "kill by pattern",
Options: []core.Option{
{Name: "-9", Description: "SIGKILL"},
{Name: "-f", Description: "match full command"},
{Name: "-u", Description: "match by user"},
},
})
core.Register(&core.Spec{
Name: "pgrep",
Description: "find process by pattern",
Options: []core.Option{
{Name: "-l", Description: "list name and PID"},
{Name: "-f", Description: "match full command"},
{Name: "-u", Description: "match by user"},
{Name: "-a", Description: "list command line"},
},
})
}