Share
Embed
Transcript
42 slides
-
What We’ll Talk About •Interfaces •Simple Design Patterns •Limiting Scope
•CLIs •Plugins •more… -
Usage: buffalo [command] Available Commands: build Build the application binary,
including bundling of assets (packr & webpack) db [PLUGIN] [DEPRECATED] please use `buffalo pop` instead. destroy Destroy generated components dev Run the Buffalo app in 'development' mode fix Attempt to fix a Buffalo application's API to match version v0.15.3 generate Generate application components help Help about any command heroku [PLUGIN] helps with heroku setup and deployment for buffalo applications info Print diagnostic information (useful for debugging) new Creates a new Buffalo application plugins tools for working with buffalo plugins pop [PLUGIN] A tasty treat for all your database needs routes Print all defined routes setup Setup a newly created, or recently checked out application. task Run grift tasks test Run the tests for the Buffalo app. Use --force-migrations to skip schema load. version Print the version information Context $ buffalo -h -
Problems •Dependencies - modules, packages, etc… •Hard-coded tools - Pop,
Webpack, Plush, etc… •Spaghetti code, poorly tested, etc… •Incorrect generated output/Mis-matched versions -
Plugins •Executables named like buffalo-<plugin-name> •in $BUFFALO_PLUGIN_PATH •in $GOPATH/bin •in
./plugins •Implement an available command returning JSON plugin information -
Problems •Slow •Discovery •Caching •Hard to write/use •No versioning of
plugins/Version mismatch •Still can’t replace or access key components •… -
Requirements •The CLI and plugins need to be in go.mod
•The application should control the whole CLI experience •Plugins need to be simple Go •Little to no magic •Independent with minimal dependencies (standard library) -
Auto-Detecting CLI $ buffalo setup arg1 arg2 If ./cmd/buffalo/main.go Else
print help $ go run ./cmd/buffalo/main.go setup arg1 arg2 Process with local CLI/plugins -
func main() { fmt.Print("~~~~ Using coke/cmd/buffalo ~~~\n\n") ctx := context.Background()
pwd, err := os.Getwd() if err != nil { log.Fatal(err) } buffalo, err := cli.New() if err != nil { log.Fatal(err) } // append your plugins here // buffalo.Plugins = append(buffalo.Plugins, ...) err = buffalo.Main(ctx, pwd, os.Args[1:]) if err != nil { log.Fatal(err) } } Local CLI ./cmd/buffalo/main.go -
buffalo.Main(ctx, pwd, os.Args[1:]) A Callable API ./cmd/buffalo/main.go type Buffalo struct
{ Plugins []plugins.Plugin } func (b *Buffalo) Main(ctx context.Context, root string, args []string) error github.com/gobuffalo/buffalo-cli/v2/cli#Buffalo -
Hand-waving the Solution •Plugins and CLI versioned in go.mod •Developers
can create unique toolsets for each app •Can compile cmd/buffalo and have an app specific Buffalo binary $ go build -o bin/buffalo ./cmd/buffalo $ ./bin/buffalo build arg1 arg2 -
func main() { ctx := context.Background() pwd, _ := os.Getwd()
buffalo := &cli.Buffalo{} err := buffalo.Main(ctx, pwd, os.Args[1:]) if err != nil { log.Fatal(err) } } Zero Value Tools for working with Buffalo applications $ buffalo --------- Using Plugins: Type Name Description ---- ---- ----------- *cli.Buffalo buffalo Tools for working with Buffalo applications -
Eating the Dog Food •Every sub-command is a plugin •Core
and 3rd party plugins use the same API •No private APIs -
Small and Concise •Interfaces should be 1-2 methods •Interfaces should
try and use standard library types •Plugins define their own eco-system •Plugins should only expose a few interfaces -
The Life of a Subcommand type Commander interface { plugins.Plugin
Main(ctx context.Context, root string, args []string) error } github.com/gobuffalo/buffalo-cli/v2/cli#Commander -
The Setup Plugin type Cmd struct {} func (Cmd) PluginName()
string func (cmd *Cmd) Main(ctx context.Context, root string, args []string) error github.com/gobuffalo/buffalo-cli/v2/cli/cmds/setup#Cmd -
Setting up as a Service $ buffalo setup Before Setup
Install Dependencies Validate System Setup Create/Migrate Databases Install Webpack Dependencies Start Background Services After Setup Run tests Report Errors -
type BeforeSetuper interface { plugins.Plugin BeforeSetup(ctx context.Context, root string, args
[]string) error } type Setuper interface { plugins.Plugin Setup(ctx context.Context, root string, args []string) error } type AfterSetuper interface { plugins.Plugin AfterSetup(ctx context.Context, root string, args []string, err error) error } Setupers github.com/gobuffalo/buffalo-cli/v2/cli/cmds/setup -
func (cmd *Cmd) Main(ctx context.Context, root string, args []string) error
{ err := cmd.run(ctx, root, args) return cmd.afterSetup(ctx, root, args, err) } func (cmd *Cmd) run(ctx context.Context, root string, args []string) error { if err := cmd.beforeSetup(ctx, root, args); err != nil { return err } for _, p := range cmd.ScopedPlugins() { if s, ok := p.(Setuper); ok { if err := s.Setup(ctx, root, args); err != nil { return err } } } return nil } Setting up for Success! github.com/gobuffalo/buffalo-cli/v2/cli/cmds/setup#Cmd.Main -
func (cmd *Cmd) beforeSetup(ctx context.Context, root string, args []string) error
{ plugs := cmd.ScopedPlugins() for _, p := range plugs { if bb, ok := p.(BeforeSetuper); ok { if err := bb.BeforeSetup(ctx, root, args); err != nil { return err } } } return nil } func (cmd *Cmd) afterSetup(ctx context.Context, root string, args []string, err error) error { plugs := cmd.ScopedPlugins() for _, p := range plugs { if bb, ok := p.(AfterSetuper); ok { if err := bb.AfterSetup(ctx, root, args, err); err != nil { return err } } } return err } Before and After github.com/gobuffalo/buffalo-cli/v2/cli/cmds/setup#Cmd -
type Setup struct {} func (Setup) PluginName() string func (s
*Setup) Setup(ctx context.Context, root string, args []string) error Pop Goes the Plugin github.com/gobuffalo/buffalo-cli/v2/cli/internal/plugins/pop/setup#Setup type Migrater interface { MigrateDB(ctx context.Context, root string, args []string) error } type DBSeeder interface { SeedDB(ctx context.Context, root string, args []string) error } -
// Scoper can be implemented to return a slice of
plugins that // are important to the type defining it. type Scoper interface { ScopedPlugins() []Plugin } type Feeder func() []Plugin // Needer can be implemented to receive a Feeder function // that can be used to gain access to other plugins in the system. type Needer interface { WithPlugins(Feeder) } Needing and Feeding github.com/gobuffalo/plugins -
var _ BeforeSetuper = beforeSetuper(nil) type beforeSetuper func(ctx context.Context, root
string, args []string) error func (b beforeSetuper) PluginName() string { return "before-setuper" } func (b beforeSetuper) BeforeSetup(ctx context.Context, root string, args []string) error { return b(ctx, root, args) } Testing Types