mirror of
https://github.com/cheat/cheat.git
synced 2026-03-07 11:13:33 +01:00
- Remove unused parameters, dead files, and inaccurate doc.go files - Extract shared helpers, eliminate duplication - Rename cheatpath.Cheatpath to cheatpath.Path - Optimize filesystem walks (WalkDir, skip .git) - Move sheet name validation to sheet.Validate - Move integration tests to test/integration/ - Consolidate internal/mock into mocks/ - Move fuzz.sh to test/ - Inline loadSheets helper into command callers - Extract config.New into its own file - Fix stale references in HACKING.md and CLAUDE.md - Restore plan9 build target - Remove redundant and low-value tests - Clean up project documentation Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
59 lines
1.9 KiB
Go
59 lines
1.9 KiB
Go
package installer
|
|
|
|
import (
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/cheat/cheat/internal/config"
|
|
)
|
|
|
|
// cheatsheetDirs returns the community, personal, and work cheatsheet
|
|
// directory paths derived from a config file path.
|
|
func cheatsheetDirs(confpath string) (community, personal, work string) {
|
|
confdir := filepath.Dir(confpath)
|
|
community = filepath.Join(confdir, "cheatsheets", "community")
|
|
personal = filepath.Join(confdir, "cheatsheets", "personal")
|
|
work = filepath.Join(confdir, "cheatsheets", "work")
|
|
return
|
|
}
|
|
|
|
// ExpandTemplate replaces placeholder tokens in the config template with
|
|
// platform-appropriate paths derived from confpath.
|
|
func ExpandTemplate(configs string, confpath string) string {
|
|
community, personal, work := cheatsheetDirs(confpath)
|
|
|
|
// substitute paths
|
|
configs = strings.ReplaceAll(configs, "COMMUNITY_PATH", community)
|
|
configs = strings.ReplaceAll(configs, "PERSONAL_PATH", personal)
|
|
configs = strings.ReplaceAll(configs, "WORK_PATH", work)
|
|
|
|
// locate and set a default pager
|
|
configs = strings.ReplaceAll(configs, "PAGER_PATH", config.Pager())
|
|
|
|
// locate and set a default editor
|
|
if editor, err := config.Editor(); err == nil {
|
|
configs = strings.ReplaceAll(configs, "EDITOR_PATH", editor)
|
|
}
|
|
|
|
return configs
|
|
}
|
|
|
|
// CommentCommunity comments out the community cheatpath block in the config
|
|
// template. This is used when the community cheatsheets directory won't exist
|
|
// (either because the user declined to download them, or because the config
|
|
// is being output as an example).
|
|
func CommentCommunity(configs string, confpath string) string {
|
|
community, _, _ := cheatsheetDirs(confpath)
|
|
|
|
return strings.ReplaceAll(configs,
|
|
" - name: community\n"+
|
|
" path: "+community+"\n"+
|
|
" tags: [ community ]\n"+
|
|
" readonly: true",
|
|
" #- name: community\n"+
|
|
" # path: "+community+"\n"+
|
|
" # tags: [ community ]\n"+
|
|
" # readonly: true",
|
|
)
|
|
}
|