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

improving command error message info #37

Merged
merged 1 commit into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions zfs/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package zfs

import (
"bufio"
"fmt"
"io"
"os/exec"
"strings"
)
Expand Down Expand Up @@ -68,17 +70,23 @@ func poolNames() ([]string, error) {
if err != nil {
return nil, err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return nil, err
}
scanner := bufio.NewScanner(out)

if err = cmd.Start(); err != nil {
return nil, err
return nil, fmt.Errorf("Failed to start command '%s': %w", cmd.String(), err)
}

for scanner.Scan() {
pools = append(pools, scanner.Text())
}

stde, _ := io.ReadAll(stderr)
if err = cmd.Wait(); err != nil {
return nil, err
return nil, fmt.Errorf("Failed to execute command '%s'; output: '%s' (%w)", cmd.String(), strings.TrimSpace(string(stde)), err)
}

return pools, nil
Expand Down
15 changes: 13 additions & 2 deletions zfs/zfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package zfs
import (
"encoding/csv"
"errors"
"fmt"
"io"
"os/exec"
"strings"
)

var (
Expand Down Expand Up @@ -69,14 +71,19 @@ func execute(pool string, h handler, cmd string, args ...string) error {
return err
}

stderr, err := c.StderrPipe()
if err != nil {
return err
}

r := csv.NewReader(out)
r.Comma = '\t'
r.LazyQuotes = true
r.ReuseRecord = true
r.FieldsPerRecord = 3

if err = c.Start(); err != nil {
return err
return fmt.Errorf("Failed to start command '%s': %w", c.String(), err)
}

for {
Expand All @@ -92,7 +99,11 @@ func execute(pool string, h handler, cmd string, args ...string) error {
}
}

return c.Wait()
stde, _ := io.ReadAll(stderr)
if err = c.Wait(); err != nil {
return fmt.Errorf("Failed to execute command '%s'; output: '%s' (%w)", c.String(), strings.TrimSpace(string(stde)), err)
}
return nil
}

// New instantiates a ZFS Client
Expand Down
Loading