Skip to content

Commit 0c9abe5

Browse files
authored
Merge pull request #11 from choria-io/4.1
(#4) allow reading cheats from an embeded fs
2 parents dd9916e + dcea135 commit 0c9abe5

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Some historical points in time are kept:
2626
* Incorporate `github.com/alecthomas/units` and `github.com/alecthomas/template` as local packages
2727
* Changes to make `staticcheck` happy
2828
* A new default template that shortens the help on large apps, old default preserved as `KingpinDefaultUsageTemplate`
29-
* Integration with [cheat](https://github.com/cheat/cheat) (see below)
29+
* Integration with [cheat](https://github.com/cheat/cheat) (see [below](#cheats))
3030

3131
## Cheats
3232

app.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package fisk
33
import (
44
"fmt"
55
"io"
6+
"io/fs"
67
"os"
78
"path/filepath"
89
"regexp"
@@ -358,7 +359,7 @@ func (a *Application) WithCheats(tags ...string) *Application {
358359
dir string
359360
)
360361

361-
a.CheatCommand = a.Command("cheat", fmt.Sprintf("Shows cheats for %s", a.Name)).Action(func(pc *ParseContext) error {
362+
a.CheatCommand = a.Commandf("cheat", "Shows cheats for %s", a.Name).Action(func(pc *ParseContext) error {
362363
switch {
363364
case dir != "":
364365
return a.saveCheats(dir)
@@ -407,6 +408,14 @@ See https://github.com/cheat/cheat for more details`)
407408
return a
408409
}
409410

411+
// CheatFile reads a file from fs and use its contents to call Cheat(). Read errors are fatal.
412+
func (a *Application) CheatFile(fs fs.ReadFileFS, cheat string, file string) *Application {
413+
body, err := fs.ReadFile(file)
414+
a.FatalIfError(err, "cannot load cheat: %v", err)
415+
416+
return a.Cheat(cheat, string(body))
417+
}
418+
410419
// Cheat sets the cheat help text to associate with this application,
411420
// the cheat is the name it will be surfaced as in help, if empty its the
412421
// name of the application.

app_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package fisk
22

33
import (
44
"bytes"
5+
"embed"
56
"errors"
67
"io/ioutil"
78
"os"
@@ -576,3 +577,13 @@ func TestCommandf(t *testing.T) {
576577
assert.Equal(t, x.help, "foo bar")
577578
assert.Equal(t, y.help, "foo bar baz")
578579
}
580+
581+
//go:embed doc.go
582+
var docFS embed.FS
583+
584+
func TestCheatFile(t *testing.T) {
585+
c := newTestApp().CheatFile(docFS, "", "doc.go")
586+
c.Command("x", "x").CheatFile(docFS, "y", "doc.go")
587+
assert.Contains(t, c.cheats["test"], "Package fisk provides")
588+
assert.Contains(t, c.cheats["y"], "Package fisk provides")
589+
}

cmd.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package fisk
22

33
import (
44
"fmt"
5+
"io/fs"
56
"strings"
67
)
78

@@ -247,6 +248,14 @@ func newCommand(app *Application, name, help string) *CmdClause {
247248
return c
248249
}
249250

251+
// CheatFile reads a file from fs and use its contents to call Cheat(). Read errors are fatal.
252+
func (c *CmdClause) CheatFile(fs fs.ReadFileFS, cheat string, file string) *CmdClause {
253+
body, err := fs.ReadFile(file)
254+
c.app.FatalIfError(err, "cannot load cheat: %v", err)
255+
256+
return c.Cheat(cheat, string(body))
257+
}
258+
250259
// Cheat sets the cheat help text to associate with this command,
251260
// the cheat is the name it will be surfaced as in help, if empty it's the
252261
// name of the command.

0 commit comments

Comments
 (0)