Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix process command crash when provided with no args #219

Merged
merged 1 commit into from
Aug 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 12 additions & 5 deletions internal/cmd/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,38 @@ import (
// ProcessCommand displays information about a Go process.
func ProcessCommand() *cobra.Command {
return &cobra.Command{
Use: "process",
Use: "process <pid> [period]",
Aliases: []string{"pid", "proc"},
Short: "Prints information about a Go process.",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ProcessInfo(args)
return nil
return ProcessInfo(args)
},
}
}

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

var period time.Duration
if len(args) >= 2 {
period, err = time.ParseDuration(args[1])
if err != nil {
secs, _ := strconv.Atoi(args[1])
secs, err := strconv.Atoi(args[1])
if err != nil {
return fmt.Errorf("error parsing the second argument: %w", err)
}
period = time.Duration(secs) * time.Second
}
}

processInfo(pid, period)
return nil
}

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