Skip to content

Commit ac7253a

Browse files
mtardytklauser
authored andcommitted
Fix process command crash when provided with no args
Use cobra.MinimumNArgs to require at least one argument and handle errors in processing the arguments. Improve the use help string.
1 parent e6260f5 commit ac7253a

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

internal/cmd/process.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,38 @@ import (
1919
// ProcessCommand displays information about a Go process.
2020
func ProcessCommand() *cobra.Command {
2121
return &cobra.Command{
22-
Use: "process",
22+
Use: "process <pid> [period]",
2323
Aliases: []string{"pid", "proc"},
2424
Short: "Prints information about a Go process.",
25+
Args: cobra.MinimumNArgs(1),
2526
RunE: func(cmd *cobra.Command, args []string) error {
26-
ProcessInfo(args)
27-
return nil
27+
return ProcessInfo(args)
2828
},
2929
}
3030
}
3131

3232
// ProcessInfo takes arguments starting with pid|:addr and grabs all kinds of
3333
// useful Go process information.
34-
func ProcessInfo(args []string) {
34+
func ProcessInfo(args []string) error {
3535
pid, err := strconv.Atoi(args[0])
36+
if err != nil {
37+
return fmt.Errorf("error parsing the first argument: %w", err)
38+
}
3639

3740
var period time.Duration
3841
if len(args) >= 2 {
3942
period, err = time.ParseDuration(args[1])
4043
if err != nil {
41-
secs, _ := strconv.Atoi(args[1])
44+
secs, err := strconv.Atoi(args[1])
45+
if err != nil {
46+
return fmt.Errorf("error parsing the second argument: %w", err)
47+
}
4248
period = time.Duration(secs) * time.Second
4349
}
4450
}
4551

4652
processInfo(pid, period)
53+
return nil
4754
}
4855

4956
func processInfo(pid int, period time.Duration) {

0 commit comments

Comments
 (0)