feat: single-sheet searching

`--search` will now honor the `<cheatsheet>` argument, making it
possible to constrain a search to a single cheatsheet.
This commit is contained in:
Chris Lane 2020-02-15 06:38:57 -05:00
parent 7b4a268ebd
commit 3e4c1818a9

View File

@ -7,6 +7,7 @@ import (
"strings"
"github.com/cheat/cheat/internal/config"
"github.com/cheat/cheat/internal/sheet"
"github.com/cheat/cheat/internal/sheets"
)
@ -35,6 +36,23 @@ func cmdSearch(opts map[string]interface{}, conf config.Config) {
// local cheatsheets)
consolidated := sheets.Consolidate(cheatsheets)
// if <cheatsheet> was provided, search that single sheet only
if opts["<cheatsheet>"] != nil {
cheatsheet := opts["<cheatsheet>"].(string)
// assert that the cheatsheet exists
s, ok := consolidated[cheatsheet]
if !ok {
fmt.Printf("No cheatsheet found for '%s'.\n", cheatsheet)
os.Exit(0)
}
consolidated = map[string]sheet.Sheet{
cheatsheet: s,
}
}
// sort the cheatsheets alphabetically, and search for matches
for _, sheet := range sheets.Sort(consolidated) {