Skip to content

Commit 9132ff4

Browse files
committed
better error messages if 'zfs' is not in the $PATH
1 parent df8f09e commit 9132ff4

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

cmd/zfs-snap-diff/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ func main() {
5151

5252
datasetName := flag.Arg(0)
5353
if len(datasetName) == 0 {
54-
fmt.Fprintf(os.Stderr, "\nABORT:\n paramter <ZFS_DATASET_NAME> missing\n")
5554
if datasetNames, err := zfs.AvailableDatasetNames(); err == nil {
55+
fmt.Fprintf(os.Stderr, "\nABORT:\n paramter <ZFS_DATASET_NAME> missing\n")
5656
names := strings.Join(datasetNames, " | ")
5757
fmt.Fprintf(os.Stderr, "\nUSAGE:\n %s [OPTIONS] <ZFS_DATASET_NAME>\n\n", zfsSnapDiffBin)
5858
fmt.Fprintf(os.Stderr, " <ZFS_DATASET_NAMES>: %s\n\n", names)
59+
fmt.Fprintf(os.Stderr, "For more information use `%s -h`", zfsSnapDiffBin)
5960
} else {
60-
fmt.Fprintf(os.Stderr, "\nUSAGE:\n %s [OPTIONS] <ZFS_DATASET_NAME>\n\n", zfsSnapDiffBin)
61+
fmt.Fprintf(os.Stderr, "ERROR:\n\n %v\n", err)
6162
}
62-
fmt.Fprintf(os.Stderr, "For more information use `%s -h`", zfsSnapDiffBin)
6363
return
6464
}
6565

pkg/zfs/zfs.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ func AvailableDatasetNames() ([]string, error) {
5353
if stdout, _, err := cmd.Exec("list", "-H", "-t", "filesystem", "-o", "name"); err == nil {
5454
datasetNames := strings.Split(stdout, "\n")
5555
return datasetNames, nil
56+
} else if _, ok := err.(ExecutableNotFound); ok {
57+
return nil, errors.New("'zfs' executable not found. Try again with the '-use-sudo' flag")
5658
} else {
5759
return nil, err
5860
}
@@ -61,22 +63,25 @@ func AvailableDatasetNames() ([]string, error) {
6163
func NewZFSForFilePath(path string) (ZFS, Dataset, error) {
6264
cmd := NewZFSCmd(config.Get.ZFS.UseSudo)
6365
stdout, _, err := cmd.Exec("list", "-Ho", "name")
64-
if err != nil {
65-
return ZFS{}, Dataset{}, err
66-
}
67-
for _, pool := range strings.Split(stdout, "\n") {
68-
z, err := NewZFS(pool)
69-
if err != nil {
70-
continue
71-
}
72-
ds, err := z.FindDatasetForPath(path)
73-
if err != nil {
74-
continue
75-
}
66+
if err == nil {
67+
for _, pool := range strings.Split(stdout, "\n") {
68+
z, err := NewZFS(pool)
69+
if err != nil {
70+
continue
71+
}
72+
ds, err := z.FindDatasetForPath(path)
73+
if err != nil {
74+
continue
75+
}
7676

77-
return z, ds, nil
77+
return z, ds, nil
78+
}
79+
return ZFS{}, Dataset{}, fmt.Errorf("dataset for file-path: %s not found", path)
80+
} else if _, ok := err.(ExecutableNotFound); ok {
81+
return ZFS{}, Dataset{}, errors.New("'zfs' executable not found. Try again with the '-use-sudo' flag")
82+
} else {
83+
return ZFS{}, Dataset{}, err
7884
}
79-
return ZFS{}, Dataset{}, fmt.Errorf("dataset for file-path: %s not found", path)
8085
}
8186

8287
func (self *ZFS) Name() string {

0 commit comments

Comments
 (0)