Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Reduce dependencies on external libraries #145

Merged
merged 2 commits into from
May 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@ 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
Copy link
Member

@sio4 sio4 May 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, it reverted my change merged yesterday.

If go.mod has a line with indirect comment, usually it could mean intentionally added/fixed version.

Will PR again soon. :-)

github.com/gobuffalo/logger v1.0.6
github.com/gobuffalo/meta v0.3.1
github.com/gobuffalo/plush/v4 v4.1.11
github.com/gobuffalo/pop/v6 v6.0.2
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
github.com/sirupsen/logrus v1.8.1
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
6 changes: 4 additions & 2 deletions internal/cmd/plugins.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
23 changes: 20 additions & 3 deletions internal/genny/build/validate.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package build

import (
"errors"
"fmt"
"html/template"
"io/fs"
"strings"

"github.com/gobuffalo/genny/v2"
"github.com/gobuffalo/plush/v4"
"github.com/markbates/safe"
)

// TemplateValidator is given a file and returns an
Expand All @@ -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 {
Expand All @@ -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()))
}
Expand Down Expand Up @@ -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
}
41 changes: 20 additions & 21 deletions internal/plugins/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import (
"os"
"os/exec"
"strings"
"sync"

"github.com/gobuffalo/envy"
"github.com/gobuffalo/events"
"github.com/markbates/oncer"
"github.com/markbates/safe"
)

const (
Expand All @@ -19,10 +18,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
Expand All @@ -41,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
}
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
})
}
events.NamedListen(n, events.Filter(c.ListenFor, fn))
return nil
}(c)
if err != nil {
errResult = err
Expand Down
8 changes: 5 additions & 3 deletions internal/plugins/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.
Expand All @@ -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)
Expand Down