From f0dd8acdebcc38273d5cbd9add9ef337895a3dac Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Wed, 12 Apr 2023 11:22:30 +0100 Subject: [PATCH] Improve error message. --- file.go | 17 ++++++++++------- file_darwin.go | 25 +++++++++++++++++-------- file_util_test.go | 2 +- file_windows.go | 2 +- 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/file.go b/file.go index aacf15e..f2f0220 100644 --- a/file.go +++ b/file.go @@ -325,12 +325,15 @@ func isUniformTypeIdentifier(pattern string) bool { return true } -func splitDirAndName(path string) (dir, name string) { - if path != "" { - fi, err := os.Stat(path) - if err == nil && fi.IsDir() { - return path, "" - } +func splitDirAndName(path string) (dir, name string, err error) { + if path == "" { + return "", "", nil } - return filepath.Split(path) + fi, err := os.Stat(path) + if err == nil && fi.IsDir() { + return path, "", nil + } + dir, name = filepath.Split(path) + _, err = os.Stat(dir) + return dir, name, err } diff --git a/file_darwin.go b/file_darwin.go index 2f11bab..c23db23 100644 --- a/file_darwin.go +++ b/file_darwin.go @@ -2,11 +2,14 @@ package zenity import "github.com/ncruces/zenity/internal/zenutil" -func selectFile(opts options) (string, error) { +func selectFile(opts options) (name string, err error) { var data zenutil.File data.Options.Prompt = opts.title data.Options.Invisibles = opts.showHidden - data.Options.Location, _ = splitDirAndName(opts.filename) + data.Options.Location, _, err = splitDirAndName(opts.filename) + if err != nil { + return "", err + } if opts.attach != nil { data.Application = opts.attach } @@ -25,13 +28,16 @@ func selectFile(opts options) (string, error) { return strResult(opts, out, err) } -func selectFileMultiple(opts options) ([]string, error) { +func selectFileMultiple(opts options) (list []string, err error) { var data zenutil.File + data.Separator = zenutil.Separator + data.Options.Multiple = true data.Options.Prompt = opts.title data.Options.Invisibles = opts.showHidden - data.Options.Location, _ = splitDirAndName(opts.filename) - data.Options.Multiple = true - data.Separator = zenutil.Separator + data.Options.Location, _, err = splitDirAndName(opts.filename) + if err != nil { + return nil, err + } if opts.attach != nil { data.Application = opts.attach } @@ -50,11 +56,14 @@ func selectFileMultiple(opts options) ([]string, error) { return lstResult(opts, out, err) } -func selectFileSave(opts options) (string, error) { +func selectFileSave(opts options) (name string, err error) { var data zenutil.File data.Options.Prompt = opts.title data.Options.Invisibles = opts.showHidden - data.Options.Location, data.Options.Name = splitDirAndName(opts.filename) + data.Options.Location, data.Options.Name, err = splitDirAndName(opts.filename) + if err != nil { + return "", err + } if opts.attach != nil { data.Application = opts.attach } diff --git a/file_util_test.go b/file_util_test.go index 84672b2..30af134 100644 --- a/file_util_test.go +++ b/file_util_test.go @@ -28,7 +28,7 @@ func Test_splitDirAndName(t *testing.T) { } for i, tt := range tests { - gotDir, gotName := splitDirAndName(tt.path) + gotDir, gotName, _ := splitDirAndName(tt.path) if gotDir != tt.wantDir { t.Errorf("splitDirAndName[%d].dir = %q; want %q", i, gotDir, tt.wantDir) } diff --git a/file_windows.go b/file_windows.go index b9c33c4..15ad749 100644 --- a/file_windows.go +++ b/file_windows.go @@ -321,7 +321,7 @@ func browseForFolderCallback(wnd win.HWND, msg uint32, lparam, data uintptr) uin } func initDirNameExt(filename string, name []uint16) (dir *uint16, ext *uint16) { - d, n := splitDirAndName(filename) + d, n, _ := splitDirAndName(filename) e := filepath.Ext(n) if n != "" { copy(name, syscall.StringToUTF16(n))