From 2e4849398a731ada64c733694799d95030725a60 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Fri, 6 May 2022 13:48:31 +0100 Subject: [PATCH] Better error handling. --- cmd/zenity/main.go | 6 +++-- entry_darwin.go | 6 +++++ internal/zenutil/osa_generated.go | 31 ++++++++++++++++------- internal/zenutil/osa_generator.go | 3 +++ internal/zenutil/osascripts/color.gojs | 12 ++++++++- internal/zenutil/osascripts/date.gojs | 10 ++++---- internal/zenutil/osascripts/dialog.gojs | 17 +++++++++---- internal/zenutil/osascripts/file.gojs | 16 +++++++++--- internal/zenutil/osascripts/list.gojs | 12 ++++++++- internal/zenutil/osascripts/progress.gojs | 4 +-- msg_darwin.go | 6 +++++ 11 files changed, 94 insertions(+), 29 deletions(-) diff --git a/cmd/zenity/main.go b/cmd/zenity/main.go index fe43981..099c990 100644 --- a/cmd/zenity/main.go +++ b/cmd/zenity/main.go @@ -405,10 +405,12 @@ func loadFlags() []zenity.Option { ico = zenity.WarningIcon case "dialog-password": ico = zenity.PasswordIcon - default: + case unspecified: ico = zenity.NoIcon + default: + opts = append(opts, zenity.CustomIcon(icon)) } - if icon != unspecified { + if ico != 0 { opts = append(opts, ico) } diff --git a/entry_darwin.go b/entry_darwin.go index 686cea1..80b20f1 100644 --- a/entry_darwin.go +++ b/entry_darwin.go @@ -1,6 +1,8 @@ package zenity import ( + "os" + "github.com/ncruces/zenity/internal/zenutil" ) @@ -13,6 +15,10 @@ func entry(text string, opts options) (string, error) { data.Options.Hidden = opts.hideText data.Options.Timeout = zenutil.Timeout if opts.customIcon != "" { + _, err := os.Stat(opts.customIcon) + if err != nil { + return "", err + } data.IconPath = opts.customIcon } else { data.Options.Icon = opts.icon.String() diff --git a/internal/zenutil/osa_generated.go b/internal/zenutil/osa_generated.go index a6f6f8e..bde7b69 100644 --- a/internal/zenutil/osa_generated.go +++ b/internal/zenutil/osa_generated.go @@ -16,7 +16,11 @@ var scripts = template.Must(template.New("").Funcs(template.FuncMap{"json": func var app=Application.currentApplication() app.includeStandardAdditions=true app.activate() -var res=app.chooseColor({defaultColor:{{json .}}}) +ObjC.import('stdio') +ObjC.import('stdlib') +try{var res=app.chooseColor({defaultColor:{{json .}}})}catch(e){if(e.errorNumber===-128)$.exit(1) +$.dprintf(2,e) +$.exit(-1)} {'rgb('+res.map(x=>Math.round(x*255))+')'} {{- end}} {{define "date" -}} @@ -48,7 +52,8 @@ var res=alert.runModal switch(res){case $.NSAlertThirdButtonReturn:$.puts({{json .Extra}}) case $.NSAlertSecondButtonReturn:$.exit(1)} var fmt=$.NSDateFormatter.alloc.init -fmt.locale=$.NSLocale.localeWithLocaleIdentifier("en_US_POSIX");fmt.dateFormat={{json .Format}} +fmt.locale=$.NSLocale.localeWithLocaleIdentifier("en_US_POSIX") +fmt.dateFormat={{json .Format}} fmt.stringFromDate(date.dateValue) {{- end}} {{define "dialog" -}} @@ -61,7 +66,9 @@ var opts={{json .Options}} {{- if .IconPath}} opts.withIcon=Path({{json .IconPath}}) {{- end}} -var res=app.{{.Operation}}({{json .Text}},opts) +try{var res=app.{{.Operation}}({{json .Text}},opts)}catch(e){if(e.errorNumber===-128)$.exit(1) +$.dprintf(2,e) +$.exit(-1)} if(res.gaveUp){$.exit(5)} if(res.buttonReturned==={{json .Extra}}){$.puts(res.buttonReturned) $.exit(1)} @@ -71,13 +78,21 @@ res.textReturned var app=Application.currentApplication() app.includeStandardAdditions=true app.activate() -var res=app.{{.Operation}}({{json .Options}}) +ObjC.import('stdio') +ObjC.import('stdlib') +try{var res=app.{{.Operation}}({{json .Options}})}catch(e){if(e.errorNumber===-128)$.exit(1) +$.dprintf(2,e) +$.exit(-1)} if(Array.isArray(res)){res.join({{json .Separator}})}else{res.toString()} {{- end}} {{define "list" -}} var app=Application.currentApplication() app.includeStandardAdditions=true -var res=app.chooseFromList({{json .Items}},{{json .Options}}) +ObjC.import('stdio') +ObjC.import('stdlib') +try{var res=app.chooseFromList({{json .Items}},{{json .Options}})}catch(e){$.dprintf(2,e) +$.exit(-1)} +if(res===false)$.exit(1) if(res.length!==0)res.join({{json .Separator}}) {{- end}} {{define "notify" -}} @@ -97,12 +112,10 @@ Progress.totalUnitCount={{.Total}} {{- if .Description}} Progress.description={{json .Description}} {{- end}} -while(true){var s -try{s=$.readline('')}catch(e){if(e.errorNumber===-128)$.exit(1) +while(true){try{var s=$.readline('')}catch(e){if(e.errorNumber===-128)$.exit(1) break} if(s.indexOf('#')===0){Progress.additionalDescription=s.slice(1) continue} var i=parseInt(s) -if(i>=0&&Progress.totalUnitCount>0){Progress.completedUnitCount=i -continue}} +if(i>=0&&Progress.totalUnitCount>0){Progress.completedUnitCount=i}} {{- end}}`)) diff --git a/internal/zenutil/osa_generator.go b/internal/zenutil/osa_generator.go index b41ebc7..5d7decb 100644 --- a/internal/zenutil/osa_generator.go +++ b/internal/zenutil/osa_generator.go @@ -31,6 +31,9 @@ func main() { if err != nil { log.Fatal(err) } + if bytes.ContainsAny(data, "`") { + log.Fatalf("illegal character: %s: `", name) + } _, err = template.New(file.Name()).Funcs(funcs).Parse(string(data)) if err != nil { log.Fatal(err) diff --git a/internal/zenutil/osascripts/color.gojs b/internal/zenutil/osascripts/color.gojs index 4eb6d60..4a54623 100644 --- a/internal/zenutil/osascripts/color.gojs +++ b/internal/zenutil/osascripts/color.gojs @@ -2,5 +2,15 @@ var app = Application.currentApplication() app.includeStandardAdditions = true app.activate() -var res = app.chooseColor({defaultColor: {{json .}}}) +ObjC.import('stdio') +ObjC.import('stdlib') + +try { + var res = app.chooseColor({defaultColor: {{json .}}}) +} catch (e) { + if (e.errorNumber === -128) $.exit(1) + $.dprintf(2, e) + $.exit(-1) +} + {'rgb(' + res.map(x => Math.round(x * 255)) + ')'} \ No newline at end of file diff --git a/internal/zenutil/osascripts/date.gojs b/internal/zenutil/osascripts/date.gojs index efd8914..66cbe70 100644 --- a/internal/zenutil/osascripts/date.gojs +++ b/internal/zenutil/osascripts/date.gojs @@ -20,21 +20,21 @@ alert.setMessageText({{json .Text}}) alert.addButtonWithTitle({{json .OK}}) alert.addButtonWithTitle({{json .Cancel}}).keyEquivalent = '\033' {{- if .Info}} - alert.setInformativeText({{json .Info}}) + alert.setInformativeText({{json .Info}}) {{- end}} {{- if .Extra}} - alert.addButtonWithTitle({{json .Extra}}) + alert.addButtonWithTitle({{json .Extra}}) {{- end}} var res = alert.runModal switch (res) { case $.NSAlertThirdButtonReturn: - $.puts({{json .Extra}}) + $.puts({{json .Extra}}) case $.NSAlertSecondButtonReturn: - $.exit(1) + $.exit(1) } var fmt = $.NSDateFormatter.alloc.init -fmt.locale = $.NSLocale.localeWithLocaleIdentifier("en_US_POSIX"); +fmt.locale = $.NSLocale.localeWithLocaleIdentifier("en_US_POSIX") fmt.dateFormat = {{json .Format}} fmt.stringFromDate(date.dateValue) \ No newline at end of file diff --git a/internal/zenutil/osascripts/dialog.gojs b/internal/zenutil/osascripts/dialog.gojs index b6f8f42..4e94869 100644 --- a/internal/zenutil/osascripts/dialog.gojs +++ b/internal/zenutil/osascripts/dialog.gojs @@ -7,15 +7,22 @@ ObjC.import('stdlib') var opts = {{json .Options}} {{- if .IconPath}} - opts.withIcon = Path({{json .IconPath}}) + opts.withIcon = Path({{json .IconPath}}) {{- end}} -var res = app.{{.Operation}}({{json .Text}}, opts) +try { + var res = app.{{.Operation}}({{json .Text}}, opts) +} catch (e) { + if (e.errorNumber === -128) $.exit(1) + $.dprintf(2, e) + $.exit(-1) +} + if (res.gaveUp) { - $.exit(5) + $.exit(5) } if (res.buttonReturned === {{json .Extra}}) { - $.puts(res.buttonReturned) - $.exit(1) + $.puts(res.buttonReturned) + $.exit(1) } res.textReturned \ No newline at end of file diff --git a/internal/zenutil/osascripts/file.gojs b/internal/zenutil/osascripts/file.gojs index 1229803..6cbbf18 100644 --- a/internal/zenutil/osascripts/file.gojs +++ b/internal/zenutil/osascripts/file.gojs @@ -2,9 +2,19 @@ var app = Application.currentApplication() app.includeStandardAdditions = true app.activate() -var res = app.{{.Operation}}({{json .Options}}) +ObjC.import('stdio') +ObjC.import('stdlib') + +try { + var res = app.{{.Operation}}({{json .Options}}) +} catch (e) { + if (e.errorNumber === -128) $.exit(1) + $.dprintf(2, e) + $.exit(-1) +} + if (Array.isArray(res)) { - res.join({{json .Separator}}) + res.join({{json .Separator}}) } else { - res.toString() + res.toString() } \ No newline at end of file diff --git a/internal/zenutil/osascripts/list.gojs b/internal/zenutil/osascripts/list.gojs index df174dc..589aedb 100644 --- a/internal/zenutil/osascripts/list.gojs +++ b/internal/zenutil/osascripts/list.gojs @@ -1,5 +1,15 @@ var app = Application.currentApplication() app.includeStandardAdditions = true -var res = app.chooseFromList({{json .Items}}, {{json .Options}}) +ObjC.import('stdio') +ObjC.import('stdlib') + +try { + var res = app.chooseFromList({{json .Items}}, {{json .Options}}) +} catch (e) { + $.dprintf(2, e) + $.exit(-1) +} + +if (res === false) $.exit(1) if (res.length !== 0) res.join({{json .Separator}}) \ No newline at end of file diff --git a/internal/zenutil/osascripts/progress.gojs b/internal/zenutil/osascripts/progress.gojs index 3cfc9f9..59e42e1 100644 --- a/internal/zenutil/osascripts/progress.gojs +++ b/internal/zenutil/osascripts/progress.gojs @@ -13,9 +13,8 @@ ObjC.import('readline') {{- end}} while (true) { - var s try { - s = $.readline('') + var s = $.readline('') } catch (e) { if (e.errorNumber === -128) $.exit(1) break @@ -29,6 +28,5 @@ while (true) { var i = parseInt(s) if (i >= 0 && Progress.totalUnitCount > 0) { Progress.completedUnitCount = i - continue } } \ No newline at end of file diff --git a/msg_darwin.go b/msg_darwin.go index 61f54d7..cb14863 100644 --- a/msg_darwin.go +++ b/msg_darwin.go @@ -1,6 +1,8 @@ package zenity import ( + "os" + "github.com/ncruces/zenity/internal/zenutil" ) @@ -21,6 +23,10 @@ func message(kind messageKind, text string, opts options) error { data.Operation = "displayDialog" data.Options.Title = opts.title if opts.customIcon != "" { + _, err := os.Stat(opts.customIcon) + if err != nil { + return err + } data.IconPath = opts.customIcon } else { data.Options.Icon = opts.icon.String()