forked from github/zfs_exporter
fix(log): Improve command execution error output
This commit is contained in:
parent
30f2d5a069
commit
2277832fc2
12
zfs/pool.go
12
zfs/pool.go
@ -2,6 +2,8 @@ package zfs
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@ -68,17 +70,23 @@ func poolNames() ([]string, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
stderr, err := cmd.StderrPipe()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
scanner := bufio.NewScanner(out)
|
scanner := bufio.NewScanner(out)
|
||||||
|
|
||||||
if err = cmd.Start(); err != nil {
|
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() {
|
for scanner.Scan() {
|
||||||
pools = append(pools, scanner.Text())
|
pools = append(pools, scanner.Text())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
stde, _ := io.ReadAll(stderr)
|
||||||
if err = cmd.Wait(); err != nil {
|
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
|
return pools, nil
|
||||||
|
15
zfs/zfs.go
15
zfs/zfs.go
@ -3,8 +3,10 @@ package zfs
|
|||||||
import (
|
import (
|
||||||
"encoding/csv"
|
"encoding/csv"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -69,6 +71,11 @@ func execute(pool string, h handler, cmd string, args ...string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
stderr, err := c.StderrPipe()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
r := csv.NewReader(out)
|
r := csv.NewReader(out)
|
||||||
r.Comma = '\t'
|
r.Comma = '\t'
|
||||||
r.LazyQuotes = true
|
r.LazyQuotes = true
|
||||||
@ -76,7 +83,7 @@ func execute(pool string, h handler, cmd string, args ...string) error {
|
|||||||
r.FieldsPerRecord = 3
|
r.FieldsPerRecord = 3
|
||||||
|
|
||||||
if err = c.Start(); err != nil {
|
if err = c.Start(); err != nil {
|
||||||
return err
|
return fmt.Errorf("Failed to start command '%s': %w", c.String(), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
@ -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
|
// New instantiates a ZFS Client
|
||||||
|
Loading…
Reference in New Issue
Block a user