From a2f538f114ff0e2823ae3174ce203b02321fbe49 Mon Sep 17 00:00:00 2001 From: Christopher Allen Lane Date: Fri, 26 Aug 2022 19:20:24 -0400 Subject: [PATCH] refactor(repo): create `repo` package - Refactor `installer.clone` into new `repo.Clone` package and method. - Refactor `sheets.isGitDir` into `repo.GitDir`. Both of these changes read better, and will facilitate cleaner architecture when `--update` is implemented. --- internal/installer/run.go | 3 ++- internal/{installer => repo}/clone.go | 8 ++++---- internal/{sheets => repo}/gitdir.go | 8 ++++---- internal/repo/update.go | 1 + internal/sheets/load.go | 3 ++- 5 files changed, 13 insertions(+), 10 deletions(-) rename internal/{installer => repo}/clone.go (65%) rename internal/{sheets => repo}/gitdir.go (95%) create mode 100644 internal/repo/update.go diff --git a/internal/installer/run.go b/internal/installer/run.go index 302c653..19f9126 100644 --- a/internal/installer/run.go +++ b/internal/installer/run.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/cheat/cheat/internal/config" + "github.com/cheat/cheat/internal/repo" ) // Run runs the installer @@ -45,7 +46,7 @@ func Run(configs string, confpath string) error { if yes { // clone the community cheatsheets fmt.Printf("Cloning community cheatsheets to %s.\n", community) - if err := clone(community); err != nil { + if err := repo.Clone(community); err != nil { return fmt.Errorf("failed to clone cheatsheets: %v", err) } diff --git a/internal/installer/clone.go b/internal/repo/clone.go similarity index 65% rename from internal/installer/clone.go rename to internal/repo/clone.go index f171e80..32c6113 100644 --- a/internal/installer/clone.go +++ b/internal/repo/clone.go @@ -1,4 +1,4 @@ -package installer +package repo import ( "fmt" @@ -7,11 +7,11 @@ import ( "github.com/go-git/go-git/v5" ) -// clone clones the community cheatsheets -func clone(path string) error { +// Clone clones the repo available at `url` +func Clone(url string) error { // clone the community cheatsheets - _, err := git.PlainClone(path, false, &git.CloneOptions{ + _, err := git.PlainClone(url, false, &git.CloneOptions{ URL: "https://github.com/cheat/cheatsheets.git", Depth: 1, Progress: os.Stdout, diff --git a/internal/sheets/gitdir.go b/internal/repo/gitdir.go similarity index 95% rename from internal/sheets/gitdir.go rename to internal/repo/gitdir.go index 63b0e42..b532827 100644 --- a/internal/sheets/gitdir.go +++ b/internal/repo/gitdir.go @@ -1,4 +1,4 @@ -package sheets +package repo import ( "fmt" @@ -6,9 +6,9 @@ import ( "strings" ) -// isGitDir returns `true` if `path` is within a `.git` directory, or `false` -// otherwise -func isGitDir(path string) (bool, error) { +// GitDir returns `true` if we are iterating over a directory contained within +// a repositories `.git` directory. +func GitDir(path string) (bool, error) { /* A bit of context is called for here, because this functionality has diff --git a/internal/repo/update.go b/internal/repo/update.go new file mode 100644 index 0000000..e0281bf --- /dev/null +++ b/internal/repo/update.go @@ -0,0 +1 @@ +package repo diff --git a/internal/sheets/load.go b/internal/sheets/load.go index 833d111..2ae8ca0 100644 --- a/internal/sheets/load.go +++ b/internal/sheets/load.go @@ -8,6 +8,7 @@ import ( "strings" cp "github.com/cheat/cheat/internal/cheatpath" + "github.com/cheat/cheat/internal/repo" "github.com/cheat/cheat/internal/sheet" ) @@ -52,7 +53,7 @@ func Load(cheatpaths []cp.Cheatpath) ([]map[string]sheet.Sheet, error) { // Don't walk the `.git` directory. Doing so creates // hundreds/thousands of needless syscalls and could // potentially harm performance on machines with slow disks. - skip, err := isGitDir(path) + skip, err := repo.GitDir(path) if err != nil { return fmt.Errorf("failed to identify .git directory: %v", err) }