@@ -2,13 +2,13 @@ package goth
22
33import (
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 })
0 commit comments