2019-10-20 16:02:28 +02:00
|
|
|
package sheets
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-08-05 01:26:07 +02:00
|
|
|
"io/fs"
|
2019-10-20 16:02:28 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
cp "github.com/cheat/cheat/internal/cheatpath"
|
2022-08-27 01:20:24 +02:00
|
|
|
"github.com/cheat/cheat/internal/repo"
|
2019-10-20 16:02:28 +02:00
|
|
|
"github.com/cheat/cheat/internal/sheet"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Load produces a map of cheatsheet titles to filesystem paths
|
|
|
|
func Load(cheatpaths []cp.Cheatpath) ([]map[string]sheet.Sheet, error) {
|
|
|
|
|
|
|
|
// create a slice of maps of sheets. This structure will store all sheets
|
|
|
|
// that are associated with each cheatpath.
|
|
|
|
sheets := make([]map[string]sheet.Sheet, len(cheatpaths))
|
|
|
|
|
|
|
|
// iterate over each cheatpath
|
|
|
|
for _, cheatpath := range cheatpaths {
|
|
|
|
|
|
|
|
// vivify the map of cheatsheets on this specific cheatpath
|
|
|
|
pathsheets := make(map[string]sheet.Sheet)
|
|
|
|
|
|
|
|
// recursively iterate over the cheatpath, and load each cheatsheet
|
|
|
|
// encountered along the way
|
|
|
|
err := filepath.Walk(
|
|
|
|
cheatpath.Path, func(
|
|
|
|
path string,
|
|
|
|
info os.FileInfo,
|
|
|
|
err error) error {
|
|
|
|
|
|
|
|
// fail if an error occurred while walking the directory
|
|
|
|
if err != nil {
|
2020-03-11 23:51:06 +01:00
|
|
|
return fmt.Errorf("failed to walk path: %v", err)
|
2019-10-20 16:02:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// don't register directories as cheatsheets
|
|
|
|
if info.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculate the cheatsheet's "title" (the phrase with which it may be
|
|
|
|
// accessed. Eg: `cheat tar` - `tar` is the title)
|
|
|
|
title := strings.TrimPrefix(
|
|
|
|
strings.TrimPrefix(path, cheatpath.Path),
|
2019-11-20 01:10:19 +01:00
|
|
|
string(os.PathSeparator),
|
2019-10-20 16:02:28 +02:00
|
|
|
)
|
|
|
|
|
2022-08-09 01:17:59 +02:00
|
|
|
// Don't walk the `.git` directory. Doing so creates
|
|
|
|
// hundreds/thousands of needless syscalls and could
|
|
|
|
// potentially harm performance on machines with slow disks.
|
2022-08-27 01:20:24 +02:00
|
|
|
skip, err := repo.GitDir(path)
|
2022-08-26 19:14:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to identify .git directory: %v", err)
|
|
|
|
}
|
|
|
|
if skip {
|
2022-08-05 01:26:07 +02:00
|
|
|
return fs.SkipDir
|
2019-10-20 16:02:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// parse the cheatsheet file into a `sheet` struct
|
2020-11-27 22:39:34 +01:00
|
|
|
s, err := sheet.New(
|
|
|
|
title,
|
|
|
|
cheatpath.Name,
|
|
|
|
path,
|
|
|
|
cheatpath.Tags,
|
|
|
|
cheatpath.ReadOnly,
|
|
|
|
)
|
2019-10-20 16:02:28 +02:00
|
|
|
if err != nil {
|
2020-03-11 23:51:06 +01:00
|
|
|
return fmt.Errorf(
|
|
|
|
"failed to load sheet: %s, path: %s, err: %v",
|
|
|
|
title,
|
|
|
|
path,
|
|
|
|
err,
|
|
|
|
)
|
2019-10-20 16:02:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// register the cheatsheet on its cheatpath, keyed by its title
|
|
|
|
pathsheets[title] = s
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return sheets, fmt.Errorf("failed to load cheatsheets: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// store the sheets on this cheatpath alongside the other cheatsheets on
|
|
|
|
// other cheatpaths
|
|
|
|
sheets = append(sheets, pathsheets)
|
|
|
|
}
|
|
|
|
|
|
|
|
// return the cheatsheets, grouped by cheatpath
|
|
|
|
return sheets, nil
|
|
|
|
}
|