From 16540f0ba448e3e4e1a23ec8250aad5aa60363bd Mon Sep 17 00:00:00 2001 From: Matthias Fasching Date: Thu, 5 May 2022 13:56:37 +0000 Subject: [PATCH 1/2] Remove depenceny on oncer --- go.mod | 2 -- go.sum | 2 -- internal/cmd/plugins.go | 6 ++++-- internal/plugins/events.go | 6 ++++-- internal/plugins/plugins.go | 8 +++++--- 5 files changed, 13 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index f87875c9..fa1b1f66 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,6 @@ require ( github.com/gobuffalo/events v1.4.2 github.com/gobuffalo/flect v0.2.5 github.com/gobuffalo/genny/v2 v2.0.9 - github.com/gobuffalo/here v0.6.6 // indirect github.com/gobuffalo/logger v1.0.6 github.com/gobuffalo/meta v0.3.1 github.com/gobuffalo/plush/v4 v4.1.11 @@ -20,7 +19,6 @@ require ( github.com/gobuffalo/refresh v1.13.1 github.com/google/go-cmp v0.5.8 github.com/markbates/grift v1.5.0 - github.com/markbates/oncer v1.0.0 github.com/markbates/safe v1.0.1 github.com/markbates/sigtx v1.0.0 github.com/psanford/memfs v0.0.0-20210214183328-a001468d78ef diff --git a/go.sum b/go.sum index 02d0151f..86e9a657 100644 --- a/go.sum +++ b/go.sum @@ -185,8 +185,6 @@ github.com/gobuffalo/helpers v0.6.4/go.mod h1:m2aOKsTl3KB0RUwwpxf3tykaaitujQ3iri github.com/gobuffalo/here v0.4.0/go.mod h1:bTNk/uKZgycuB358iR0D32dI9kHBClBGpXjW2HVHkNo= github.com/gobuffalo/here v0.6.5 h1:OjrFcVbQBXff4EN+/m2xa+i1Wy6lW+3fn9Jf+b5WDXY= github.com/gobuffalo/here v0.6.5/go.mod h1:y6q8eG7YstM/DfOKKAyHV1plrNsuYS5dcIerm8Habas= -github.com/gobuffalo/here v0.6.6 h1:/o+jfSwe36pKQ577grsXGoMYql/zheiGwg1XFo3CBJU= -github.com/gobuffalo/here v0.6.6/go.mod h1:C4JZL5PsXWKzP/CAchaIzuUWlaae6CaAiMYQ0ieY62M= github.com/gobuffalo/httptest v1.5.1/go.mod h1:uEeEFF2BRyTMNAATqFQAKYvpHrWWPNoJbIB3YPuANNM= github.com/gobuffalo/logger v1.0.6 h1:nnZNpxYo0zx+Aj9RfMPBm+x9zAU2OayFh/xrAWi34HU= github.com/gobuffalo/logger v1.0.6/go.mod h1:J31TBEHR1QLV2683OXTAItYIg8pv2JMHnF/quuAbMjs= diff --git a/internal/cmd/plugins.go b/internal/cmd/plugins.go index 05307965..367d0f61 100644 --- a/internal/cmd/plugins.go +++ b/internal/cmd/plugins.go @@ -1,16 +1,18 @@ package cmd import ( + "sync" + "github.com/gobuffalo/cli/internal/plugins" - "github.com/markbates/oncer" "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) var _plugs plugins.List +var initPlugsOnce sync.Once func plugs() plugins.List { - oncer.Do("buffalo/cmd/plugins", func() { + initPlugsOnce.Do(func() { var err error _plugs, err = plugins.Available() if err == nil { diff --git a/internal/plugins/events.go b/internal/plugins/events.go index bd18f6b5..e08269ec 100644 --- a/internal/plugins/events.go +++ b/internal/plugins/events.go @@ -6,10 +6,10 @@ import ( "os" "os/exec" "strings" + "sync" "github.com/gobuffalo/envy" "github.com/gobuffalo/events" - "github.com/markbates/oncer" "github.com/markbates/safe" ) @@ -19,10 +19,12 @@ const ( EvtSetupFinished = "buffalo-plugins:setup:finished" ) +var loadPluginsOnce sync.Once + // Load will add listeners for any plugins that support "events" func Load() error { var errResult error - oncer.Do("events.LoadPlugins", func() { + loadPluginsOnce.Do(func() { // don't send plugins events during testing if envy.Get("GO_ENV", "development") == "test" { return diff --git a/internal/plugins/plugins.go b/internal/plugins/plugins.go index f0b7560c..6596580d 100644 --- a/internal/plugins/plugins.go +++ b/internal/plugins/plugins.go @@ -10,21 +10,22 @@ import ( "os/exec" "path/filepath" "strings" + "sync" "time" "github.com/gobuffalo/cli/internal/plugins/plugdeps" "github.com/gobuffalo/envy" "github.com/gobuffalo/meta" - "github.com/markbates/oncer" "github.com/sirupsen/logrus" ) const timeoutEnv = "BUFFALO_PLUGIN_TIMEOUT" var t = time.Second * 2 +var initTimeoutOnce sync.Once func timeout() time.Duration { - oncer.Do("plugins.timeout", func() { + initTimeoutOnce.Do(func() { rawTimeout, err := envy.MustGet(timeoutEnv) if err == nil { if parsed, err := time.ParseDuration(rawTimeout); err == nil { @@ -43,6 +44,7 @@ func timeout() time.Duration { type List map[string]Commands var _list List +var scanPlugsOnce sync.Once // Available plugins for the `buffalo` command. // It will look in $GOPATH/bin and the `./plugins` directory. @@ -64,7 +66,7 @@ var _list List // process. func Available() (List, error) { var err error - oncer.Do("plugins.Available", func() { + scanPlugsOnce.Do(func() { defer func() { if err := saveCache(); err != nil { logrus.Error(err) From 0eeb3ca3c72f35146fe72b9b46854a3aadd95f4f Mon Sep 17 00:00:00 2001 From: Matthias Fasching Date: Sat, 14 May 2022 21:12:26 +0000 Subject: [PATCH 2/2] Remove safe from dependencies --- go.mod | 1 - internal/genny/build/validate.go | 23 ++++++++++++++++++--- internal/plugins/events.go | 35 +++++++++++++++----------------- 3 files changed, 36 insertions(+), 23 deletions(-) diff --git a/go.mod b/go.mod index fa1b1f66..90b3a356 100644 --- a/go.mod +++ b/go.mod @@ -19,7 +19,6 @@ require ( github.com/gobuffalo/refresh v1.13.1 github.com/google/go-cmp v0.5.8 github.com/markbates/grift v1.5.0 - github.com/markbates/safe v1.0.1 github.com/markbates/sigtx v1.0.0 github.com/psanford/memfs v0.0.0-20210214183328-a001468d78ef github.com/sirupsen/logrus v1.8.1 diff --git a/internal/genny/build/validate.go b/internal/genny/build/validate.go index 0fa2f885..17653f05 100644 --- a/internal/genny/build/validate.go +++ b/internal/genny/build/validate.go @@ -1,6 +1,7 @@ package build import ( + "errors" "fmt" "html/template" "io/fs" @@ -8,7 +9,6 @@ import ( "github.com/gobuffalo/genny/v2" "github.com/gobuffalo/plush/v4" - "github.com/markbates/safe" ) // TemplateValidator is given a file and returns an @@ -17,7 +17,7 @@ import ( type TemplateValidator func(f genny.File) error // ValidateTemplates returns a genny.RunFn that will walk the -// given box and run each of the files found through each of the +// given filesystem and run each of the files found through each of the // template validators func ValidateTemplates(fsys fs.FS, tvs []TemplateValidator) genny.RunFn { if len(tvs) == 0 { @@ -42,7 +42,7 @@ func ValidateTemplates(fsys fs.FS, tvs []TemplateValidator) genny.RunFn { } f := genny.NewFile(path, b) for _, tv := range tvs { - err := safe.Run(func() { + err := safeRun(func() { if err := tv(f); err != nil { errs = append(errs, fmt.Sprintf("template error in file %s: %s", path, err.Error())) } @@ -85,3 +85,20 @@ func GoTemplateValidator(f genny.File) error { _, err := t.Parse(f.String()) return err } + +// safeRun the function safely knowing that if it panics +// the panic will be caught and returned as an error +func safeRun(fn func()) (err error) { + defer func() { + if ex := recover(); ex != nil { + if e, ok := ex.(error); ok { + err = e + return + } + err = errors.New(fmt.Sprint(ex)) + } + }() + + fn() + return nil +} diff --git a/internal/plugins/events.go b/internal/plugins/events.go index e08269ec..84843b4b 100644 --- a/internal/plugins/events.go +++ b/internal/plugins/events.go @@ -10,7 +10,6 @@ import ( "github.com/gobuffalo/envy" "github.com/gobuffalo/events" - "github.com/markbates/safe" ) const ( @@ -43,25 +42,23 @@ func Load() error { } err := func(c Command) error { - return safe.RunE(func() error { - n := fmt.Sprintf("[PLUGIN] %s %s", c.Binary, c.Name) - fn := func(e events.Event) { - b, err := json.Marshal(e) - if err != nil { - fmt.Println("error trying to marshal event", e, err) - return - } - cmd := exec.Command(c.Binary, c.UseCommand, string(b)) - cmd.Stderr = os.Stderr - cmd.Stdout = os.Stdout - cmd.Stdin = os.Stdin - if err := cmd.Run(); err != nil { - fmt.Println("error trying to send event", strings.Join(cmd.Args, " "), err) - } + n := fmt.Sprintf("[PLUGIN] %s %s", c.Binary, c.Name) + fn := func(e events.Event) { + b, err := json.Marshal(e) + if err != nil { + fmt.Println("error trying to marshal event", e, err) + return } - events.NamedListen(n, events.Filter(c.ListenFor, fn)) - return nil - }) + cmd := exec.Command(c.Binary, c.UseCommand, string(b)) + cmd.Stderr = os.Stderr + cmd.Stdout = os.Stdout + cmd.Stdin = os.Stdin + if err := cmd.Run(); err != nil { + fmt.Println("error trying to send event", strings.Join(cmd.Args, " "), err) + } + } + events.NamedListen(n, events.Filter(c.ListenFor, fn)) + return nil }(c) if err != nil { errResult = err