docs(backpack,beachcomber,boardwalk,bonfire,bouncer): compile README Go examples via src= (quick-261118-qsd)
- backpack: Greeter and ExampleApp_Publish replace the whole-file fence - beachcomber: Post's Searchable methods plus a new SearchIndexSchema, a Plugin.Boot that installs the gate, and the existing gate/search regions - boardwalk: new example_test.go; the mount region runs in ExampleHandler - bonfire: importPostsCommand and acmeMain (the binary's main as a function returning the exit code), run by ExampleExecute; ExampleCatalog_Has - bouncer: users/FindByID and ExampleNewJWTGuard; RefreshRoute run by TestDocsRefreshRoute on a production compass config
This commit is contained in:
@@ -2,7 +2,9 @@ package bonfire_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
@@ -160,3 +162,68 @@ func ExampleCheckDocs() {
|
||||
}
|
||||
// Output: blog:reindex: missing long help, examples, option --batch description
|
||||
}
|
||||
|
||||
// importPostsCommand is the blog:import command of the acme binary.
|
||||
func importPostsCommand() bonfire.Command {
|
||||
return bonfire.Command{
|
||||
Name: "blog:import",
|
||||
Description: "Import posts from a feed",
|
||||
Args: []bonfire.Arg{{Name: "url", Description: "Feed URL", Required: true}},
|
||||
Flags: []bonfire.Flag{
|
||||
{Name: "dry-run", Description: "Report without writing", Bare: true},
|
||||
{Name: "tag", Description: "Tag to apply (repeatable)", Repeatable: true},
|
||||
},
|
||||
Run: func(ctx context.Context, in bonfire.Input, out bonfire.Output) error {
|
||||
url, _ := in.Argument("url")
|
||||
dryRun := bonfire.FlagBool(in, "dry-run")
|
||||
return out.Spinner("Importing "+url, func() error {
|
||||
out.Table([]string{"Tag"}, [][]string{{"news"}})
|
||||
if dryRun {
|
||||
out.Warning("dry run: nothing written")
|
||||
}
|
||||
return nil
|
||||
})
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// acmeMain is the body of the acme binary's main function, which calls
|
||||
// os.Exit(acmeMain(os.Args[1:], os.Stdout, os.Stderr)).
|
||||
func acmeMain(args []string, stdout, stderr io.Writer) int {
|
||||
g := bonfire.PeekGlobals(args)
|
||||
root, err := bonfire.NewRoot("acme", []bonfire.Command{importPostsCommand()}, stdout,
|
||||
bonfire.WithVersion(bonfire.BuildVersion()))
|
||||
if err != nil {
|
||||
return bonfire.Fail(stderr, "acme", err, g)
|
||||
}
|
||||
return bonfire.Execute(context.Background(), root, args)
|
||||
}
|
||||
|
||||
func ExampleExecute() {
|
||||
code := acmeMain([]string{"blog:import", "--dry-run", "https://example.com/feed"}, os.Stdout, os.Stdout)
|
||||
fmt.Println("exit code:", code)
|
||||
// Output:
|
||||
// [...] Importing https://example.com/feed
|
||||
// Tag
|
||||
// news
|
||||
// ⚠ dry run: nothing written
|
||||
// exit code: 0
|
||||
}
|
||||
|
||||
func ExampleCatalog_Has() {
|
||||
ctx := context.Background()
|
||||
catalog := bonfire.NewCatalog([]bonfire.Command{importPostsCommand()})
|
||||
if catalog.Has("blog:import") {
|
||||
err := catalog.Call(ctx, "blog:import", []string{"--dry-run", "https://example.com/feed"}, os.Stdout)
|
||||
fmt.Println(err)
|
||||
}
|
||||
err := catalog.Call(ctx, "blog:export", nil, os.Stdout)
|
||||
fmt.Println(errors.Is(err, bonfire.ErrUnknownCommand)) // not registered in this binary
|
||||
// Output:
|
||||
// [...] Importing https://example.com/feed
|
||||
// Tag
|
||||
// news
|
||||
// ⚠ dry run: nothing written
|
||||
// <nil>
|
||||
// true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user