mirror of
https://github.com/cheat/cheat.git
synced 2026-03-07 03:03:32 +01:00
chore: housekeeping and refactoring (bump to 4.7.1)
- 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>
This commit is contained in:
@@ -2,7 +2,6 @@ package installer
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
@@ -158,23 +157,3 @@ func TestPromptError(t *testing.T) {
|
||||
t.Errorf("expected 'failed to prompt' error, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPromptIntegration provides a simple integration test
|
||||
func TestPromptIntegration(t *testing.T) {
|
||||
// This demonstrates how the prompt would be used in practice
|
||||
// It's skipped by default since it requires actual user input
|
||||
if os.Getenv("TEST_INTERACTIVE") != "1" {
|
||||
t.Skip("Skipping interactive test - set TEST_INTERACTIVE=1 to run")
|
||||
}
|
||||
|
||||
fmt.Println("\n=== Interactive Prompt Test ===")
|
||||
fmt.Println("You will be prompted to answer a question.")
|
||||
fmt.Println("Try different inputs: y, n, Y, N, empty (just press Enter)")
|
||||
|
||||
result, err := Prompt("Would you like to continue? [Y/n]", true)
|
||||
if err != nil {
|
||||
t.Fatalf("Prompt failed: %v", err)
|
||||
}
|
||||
|
||||
fmt.Printf("You answered: %v\n", result)
|
||||
}
|
||||
|
||||
@@ -3,8 +3,6 @@ package installer
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/cheat/cheat/internal/config"
|
||||
"github.com/cheat/cheat/internal/repo"
|
||||
@@ -13,27 +11,11 @@ import (
|
||||
// Run runs the installer
|
||||
func Run(configs string, confpath string) error {
|
||||
|
||||
// determine the appropriate paths for config data and (optional) community
|
||||
// cheatsheets based on the user's platform
|
||||
confdir := filepath.Dir(confpath)
|
||||
// expand template placeholders with platform-appropriate paths
|
||||
configs = ExpandTemplate(configs, confpath)
|
||||
|
||||
// create paths for community, personal, and work cheatsheets
|
||||
community := filepath.Join(confdir, "cheatsheets", "community")
|
||||
personal := filepath.Join(confdir, "cheatsheets", "personal")
|
||||
work := filepath.Join(confdir, "cheatsheets", "work")
|
||||
|
||||
// set default cheatpaths
|
||||
configs = strings.Replace(configs, "COMMUNITY_PATH", community, -1)
|
||||
configs = strings.Replace(configs, "PERSONAL_PATH", personal, -1)
|
||||
configs = strings.Replace(configs, "WORK_PATH", work, -1)
|
||||
|
||||
// locate and set a default pager
|
||||
configs = strings.Replace(configs, "PAGER_PATH", config.Pager(), -1)
|
||||
|
||||
// locate and set a default editor
|
||||
if editor, err := config.Editor(); err == nil {
|
||||
configs = strings.Replace(configs, "EDITOR_PATH", editor, -1)
|
||||
}
|
||||
// determine cheatsheet directory paths
|
||||
community, personal, work := cheatsheetDirs(confpath)
|
||||
|
||||
// prompt the user to download the community cheatsheets
|
||||
yes, err := Prompt(
|
||||
@@ -51,19 +33,7 @@ func Run(configs string, confpath string) error {
|
||||
return fmt.Errorf("failed to clone cheatsheets: %v", err)
|
||||
}
|
||||
} else {
|
||||
// comment out the community cheatpath in the config since
|
||||
// the directory won't exist
|
||||
configs = strings.Replace(configs,
|
||||
" - name: community\n"+
|
||||
" path: "+community+"\n"+
|
||||
" tags: [ community ]\n"+
|
||||
" readonly: true",
|
||||
" #- name: community\n"+
|
||||
" # path: "+community+"\n"+
|
||||
" # tags: [ community ]\n"+
|
||||
" # readonly: true",
|
||||
-1,
|
||||
)
|
||||
configs = CommentCommunity(configs, confpath)
|
||||
}
|
||||
|
||||
// always create personal and work directories
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package installer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -245,10 +244,10 @@ cheatpaths:
|
||||
if strings.Contains(contentStr, "PERSONAL_PATH") {
|
||||
t.Error("PERSONAL_PATH was not replaced")
|
||||
}
|
||||
if strings.Contains(contentStr, "EDITOR_PATH") && !strings.Contains(contentStr, fmt.Sprintf("editor: %s", "")) {
|
||||
if strings.Contains(contentStr, "EDITOR_PATH") {
|
||||
t.Error("EDITOR_PATH was not replaced")
|
||||
}
|
||||
if strings.Contains(contentStr, "PAGER_PATH") && !strings.Contains(contentStr, fmt.Sprintf("pager: %s", "")) {
|
||||
if strings.Contains(contentStr, "PAGER_PATH") {
|
||||
t.Error("PAGER_PATH was not replaced")
|
||||
}
|
||||
if strings.Contains(contentStr, "WORK_PATH") {
|
||||
|
||||
58
internal/installer/template.go
Normal file
58
internal/installer/template.go
Normal file
@@ -0,0 +1,58 @@
|
||||
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",
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user