Skip to content

Commit 15d670d

Browse files
authored
Merge pull request #49 from gobuffalo/fixing-could-not-find-desired-block
fixing could not find desired block (fixes #48)
2 parents b571c17 + 5adbca7 commit 15d670d

File tree

15 files changed

+119
-99
lines changed

15 files changed

+119
-99
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ Buffalo-goth is a plugin for [buffalo cli](https://github.com/gobuffalo/cli)
88
that makes it easy to integrate [goth](https://github.com/markbates/goth)
99
into your Buffalo application.
1010

11-
In Buffalo `v0.9.4` the built in generator for [github.com/markbates/goth](https://github.com/markbates/goth) was removed in favor of this plugin.
12-
1311
## Installation
1412

1513
```console

SHOULDERS.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ Thank you to the following **GIANTS**:
2929
* [github.com/kr/text](https://godoc.org/github.com/kr/text)
3030
* [github.com/microcosm-cc/bluemonday](https://godoc.org/github.com/microcosm-cc/bluemonday)
3131
* [github.com/pkg/diff](https://godoc.org/github.com/pkg/diff)
32-
* [github.com/pkg/errors](https://godoc.org/github.com/pkg/errors)
3332
* [github.com/pmezard/go-difflib](https://godoc.org/github.com/pmezard/go-difflib)
3433
* [github.com/rogpeppe/go-internal](https://godoc.org/github.com/rogpeppe/go-internal)
3534
* [github.com/russross/blackfriday/v2](https://godoc.org/github.com/russross/blackfriday/v2)
@@ -52,6 +51,5 @@ Thank you to the following **GIANTS**:
5251
* [golang.org/x/tools](https://godoc.org/golang.org/x/tools)
5352
* [golang.org/x/xerrors](https://godoc.org/golang.org/x/xerrors)
5453
* [gopkg.in/check.v1](https://godoc.org/gopkg.in/check.v1)
55-
* [gopkg.in/errgo.v2](https://godoc.org/gopkg.in/errgo.v2)
5654
* [gopkg.in/yaml.v2](https://godoc.org/gopkg.in/yaml.v2)
5755
* [gopkg.in/yaml.v3](https://godoc.org/gopkg.in/yaml.v3)

cmd/auth.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package cmd
22

33
import (
44
"context"
5+
"fmt"
56
"os/exec"
67

78
"github.com/gobuffalo/buffalo-goth/genny/auth"
89
"github.com/gobuffalo/genny/v2"
910
"github.com/gobuffalo/genny/v2/gogen"
10-
"github.com/pkg/errors"
1111
"github.com/spf13/cobra"
1212
)
1313

@@ -33,13 +33,13 @@ var authCmd = &cobra.Command{
3333
opts.Providers = args
3434
gg, err := auth.New(opts)
3535
if err != nil {
36-
return errors.WithStack(err)
36+
return err
3737
}
3838
gg.With(r)
3939

4040
g, err := gogen.Fmt(r.Root)
4141
if err != nil {
42-
return errors.WithStack(err)
42+
return fmt.Errorf("formatting error: %w", err)
4343
}
4444
r.With(g)
4545

cmd/generate.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package cmd
22

33
import (
44
"context"
5+
"fmt"
56
"os/exec"
67

78
"github.com/gobuffalo/buffalo-goth/genny/goth"
89
"github.com/gobuffalo/genny/v2"
910
"github.com/gobuffalo/genny/v2/gogen"
10-
"github.com/pkg/errors"
1111
"github.com/spf13/cobra"
1212
)
1313

@@ -34,13 +34,13 @@ var generateCmd = &cobra.Command{
3434
opts.Providers = args
3535
g, err := goth.New(opts)
3636
if err != nil {
37-
return errors.WithStack(err)
37+
return err
3838
}
3939
r.With(g)
4040

4141
g, err = gogen.Fmt(r.Root)
4242
if err != nil {
43-
return errors.WithStack(err)
43+
return fmt.Errorf("formatting error: %w", err)
4444
}
4545
r.With(g)
4646

cmd/version.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import (
1111
// versionCmd represents the version command
1212
var versionCmd = &cobra.Command{
1313
Use: "version",
14-
Short: "current version of goth",
14+
Short: "current version of buffalo-goth",
1515
RunE: func(cmd *cobra.Command, args []string) error {
16-
fmt.Println("goth", goth.Version)
16+
fmt.Println("buffalo-goth", goth.Version)
1717
return nil
1818
},
1919
}

genny/auth/auth.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package auth
33
import (
44
"bytes"
55
"embed"
6+
"fmt"
67
"io/fs"
78
"os/exec"
89
"strings"
@@ -11,7 +12,6 @@ import (
1112
"github.com/gobuffalo/genny/v2"
1213
"github.com/gobuffalo/genny/v2/gogen"
1314
"github.com/gobuffalo/meta"
14-
"github.com/pkg/errors"
1515

1616
"github.com/gobuffalo/buffalo-goth/genny/goth"
1717
)
@@ -25,19 +25,19 @@ func New(opts *Options) (*genny.Group, error) {
2525
Providers: opts.Providers,
2626
})
2727
if err != nil {
28-
return gg, errors.WithStack(err)
28+
return gg, err
2929
}
3030
gg.Add(g)
3131

3232
sub, err := fs.Sub(templates, "templates")
3333
if err != nil {
34-
return gg, errors.WithStack(err)
34+
return gg, fmt.Errorf("failed to get subtree of templates: %w", err)
3535
}
3636

3737
g = genny.New()
3838

3939
if err := g.FS(sub); err != nil {
40-
return gg, errors.WithStack(err)
40+
return gg, fmt.Errorf("failed to add subtree: %w", err)
4141
}
4242

4343
h := template.FuncMap{
@@ -59,7 +59,7 @@ func New(opts *Options) (*genny.Group, error) {
5959

6060
f, err := r.FindFile(path)
6161
if err != nil {
62-
return errors.WithStack(err)
62+
return fmt.Errorf("setup goth: %w", err)
6363
}
6464

6565
bb := &bytes.Buffer{}

genny/auth/auth_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ var app *buffalo.App
6868
func App() *buffalo.App {
6969
if app == nil {
7070
app = buffalo.New(buffalo.Options{})
71+
72+
// NOTE: this block should go before any resources
73+
// that need to be protected by buffalo-goth!
7174
auth := app.Group("/auth")
7275
app.Use(SetCurrentUser)
7376
app.Use(Authorize)
@@ -77,6 +80,7 @@ func App() *buffalo.App {
7780
auth.DELETE("", AuthDestroy)
7881
auth.Middleware.Skip(Authorize, bah, AuthCallback)
7982
auth.GET("/{provider}/callback", AuthCallback)
83+
8084
}
8185
8286
return app

genny/auth/templates/actions/auth.go.tmpl

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@ package actions
22

33
import (
44
"fmt"
5+
"net/http"
56
"os"
67

8+
"{{.app.ModelsPkg}}"
9+
710
"github.com/gobuffalo/buffalo"
8-
"{{.app.ModelsPkg}}"
9-
"github.com/markbates/going/defaults"
11+
"github.com/gobuffalo/nulls"
12+
"github.com/gobuffalo/pop/v6"
13+
"github.com/gobuffalo/x/defaults"
1014
"github.com/markbates/goth"
1115
"github.com/markbates/goth/gothic"
1216
{{range .providers -}}
1317
"github.com/markbates/goth/providers/{{ downcase . }}"
14-
{{end -}}
15-
"github.com/gobuffalo/pop/v6"
16-
"github.com/gobuffalo/nulls"
18+
{{end -}}
1719
"github.com/pkg/errors"
1820
)
1921

@@ -30,7 +32,7 @@ func init() {
3032
func AuthCallback(c buffalo.Context) error {
3133
gu, err := gothic.CompleteUserAuth(c.Response(), c.Request())
3234
if err != nil {
33-
return c.Error(401, err)
35+
return c.Error(http.StatusUnauthorized, err)
3436
}
3537
tx := c.Value("tx").(*pop.Connection)
3638
q := tx.Where("provider = ? and provider_id = ?", gu.Provider, gu.UserID)
@@ -58,13 +60,13 @@ func AuthCallback(c buffalo.Context) error {
5860
}
5961

6062
c.Flash().Add("success", "You have been logged in")
61-
return c.Redirect(302, "/")
63+
return c.Redirect(http.StatusFound, "/")
6264
}
6365

6466
func AuthDestroy(c buffalo.Context) error {
6567
c.Session().Clear()
6668
c.Flash().Add("success", "You have been logged out")
67-
return c.Redirect(302, "/")
69+
return c.Redirect(http.StatusFound, "/")
6870
}
6971

7072
func SetCurrentUser(next buffalo.Handler) buffalo.Handler {
@@ -85,7 +87,7 @@ func Authorize(next buffalo.Handler) buffalo.Handler {
8587
return func(c buffalo.Context) error {
8688
if uid := c.Session().Get("current_user_id"); uid == nil {
8789
c.Flash().Add("danger", "You must be authorized to see that page")
88-
return c.Redirect(302, "/")
90+
return c.Redirect(http.StatusFound, "/")
8991
}
9092
return next(c)
9193
}

genny/goth/goth.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package goth
22

33
import (
44
"embed"
5+
"fmt"
56
"io/fs"
67
"strings"
78
"text/template"
89

910
"github.com/gobuffalo/genny/v2"
1011
"github.com/gobuffalo/genny/v2/gogen"
11-
"github.com/pkg/errors"
1212
)
1313

1414
//go:embed templates
@@ -18,15 +18,15 @@ func New(opts *Options) (*genny.Generator, error) {
1818
g := genny.New()
1919

2020
if len(opts.Providers) == 0 {
21-
return g, errors.New("you must specify at least one provider")
21+
return g, fmt.Errorf("you must specify at least one provider")
2222
}
2323

2424
sub, err := fs.Sub(templates, "templates")
2525
if err != nil {
26-
return g, errors.WithStack(err)
26+
return g, fmt.Errorf("failed to get subtree of templates: %w", err)
2727
}
2828
if err := g.FS(sub); err != nil {
29-
return g, errors.WithStack(err)
29+
return g, fmt.Errorf("failed to add subtree: %w", err)
3030
}
3131

3232
h := template.FuncMap{
@@ -44,22 +44,41 @@ func New(opts *Options) (*genny.Generator, error) {
4444

4545
f, err := r.FindFile(path)
4646
if err != nil {
47-
return errors.WithStack(err)
47+
return fmt.Errorf("setup goth: %w", err)
4848
}
4949

5050
f, err = gogen.AddImport(f, "github.com/markbates/goth/gothic")
5151
if err != nil {
52-
return errors.WithStack(err)
52+
return fmt.Errorf("could not add import: %w", err)
5353
}
5454

5555
expressions := []string{
56+
"",
57+
"// NOTE: this block should go before any resources",
58+
"// that need to be protected by buffalo-goth!",
5659
"auth := app.Group(\"/auth\")",
5760
"auth.GET(\"/{provider}\", buffalo.WrapHandlerFunc(gothic.BeginAuthHandler))",
5861
"auth.GET(\"/{provider}/callback\", AuthCallback)",
62+
"",
5963
}
60-
f, err = gogen.AddInsideBlock(f, "if app == nil {", expressions...)
64+
65+
f, err = gogen.AddInsideBlock(f, "appOnce.Do(func() {", expressions...)
6166
if err != nil {
62-
return errors.WithStack(err)
67+
if strings.Contains(err.Error(), "could not find desired block") {
68+
// TODO: remove this block some day soon
69+
// add this block for compatibility with the apps built with
70+
// the old version of Buffalo CLI (v0.18.8 or older)
71+
f, err = gogen.AddInsideBlock(f, "if app == nil {", expressions...)
72+
if err != nil {
73+
if err != nil {
74+
return fmt.Errorf("could not add a code block: %w", err)
75+
} else {
76+
r.Logger.Warnf("This app was built with CLI v0.18.8 or older. See https://gobuffalo.io/documentation/known-issues/#cli-v0.18.8")
77+
}
78+
}
79+
} else {
80+
return fmt.Errorf("could not add a code block: %w", err)
81+
}
6382
}
6483
return r.File(f)
6584
})

genny/goth/goth_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,13 @@ var app *buffalo.App
7373
func App() *buffalo.App {
7474
if app == nil {
7575
app = buffalo.New(buffalo.Options{})
76+
77+
// NOTE: this block should go before any resources
78+
// that need to be protected by buffalo-goth!
7679
auth := app.Group("/auth")
7780
auth.GET("/{provider}", buffalo.WrapHandlerFunc(gothic.BeginAuthHandler))
7881
auth.GET("/{provider}/callback", AuthCallback)
82+
7983
}
8084
8185
return app
@@ -85,14 +89,15 @@ const authAfter = `package actions
8589
8690
import (
8791
"fmt"
92+
"net/http"
8893
"os"
8994
9095
"github.com/gobuffalo/buffalo"
9196
"github.com/markbates/goth"
9297
"github.com/markbates/goth/gothic"
9398
"github.com/markbates/goth/providers/github"
94-
"github.com/markbates/goth/providers/twitter"
95-
)
99+
"github.com/markbates/goth/providers/twitter"
100+
)
96101
97102
func init() {
98103
gothic.Store = App().SessionStore
@@ -106,9 +111,9 @@ func init() {
106111
func AuthCallback(c buffalo.Context) error {
107112
user, err := gothic.CompleteUserAuth(c.Response(), c.Request())
108113
if err != nil {
109-
return c.Error(401, err)
114+
return c.Error(http.StatusUnauthorized, err)
110115
}
111116
// Do something with the user, maybe register them/sign them in
112-
return c.Render(200, r.JSON(user))
117+
return c.Render(http.StatusOK, r.JSON(user))
113118
}
114119
`

0 commit comments

Comments
 (0)