feat: add --update/-u flag to pull git-backed cheatpaths (#552)

Iterates over configured cheatpaths and runs git pull on each one that
is a git repository with a clean worktree. Supports SSH remotes via key
file discovery and SSH agent fallback. Works with --path filtering.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Christopher Allen Lane
2026-02-15 19:40:07 -05:00
parent 417b5b4e42
commit 80e0e0d3ae
11 changed files with 535 additions and 16 deletions

42
cmd/cheat/cmd_update.go Normal file
View File

@@ -0,0 +1,42 @@
package main
import (
"errors"
"fmt"
"os"
"github.com/go-git/go-git/v5"
"github.com/spf13/cobra"
"github.com/cheat/cheat/internal/config"
"github.com/cheat/cheat/internal/repo"
)
// cmdUpdate updates git-backed cheatpaths.
func cmdUpdate(_ *cobra.Command, _ []string, conf config.Config) {
hasError := false
for _, path := range conf.Cheatpaths {
err := repo.Pull(path.Path)
switch {
case err == nil:
fmt.Printf("%s: ok\n", path.Name)
case errors.Is(err, git.ErrRepositoryNotExists):
fmt.Printf("%s: skipped (not a git repository)\n", path.Name)
case errors.Is(err, repo.ErrDirtyWorktree):
fmt.Printf("%s: skipped (dirty worktree)\n", path.Name)
default:
fmt.Fprintf(os.Stderr, "%s: error (%v)\n", path.Name, err)
hasError = true
}
}
if hasError {
os.Exit(1)
}
}

View File

@@ -15,7 +15,7 @@ import (
"github.com/cheat/cheat/internal/installer"
)
const version = "5.0.0"
const version = "5.1.0"
var rootCmd = &cobra.Command{
Use: "cheat [cheatsheet]",
@@ -60,6 +60,9 @@ remember.`,
To remove (delete) the foo/bar cheatsheet:
cheat --rm foo/bar
To update all git-backed cheatpaths:
cheat --update
To view the configuration file path:
cheat --conf
@@ -87,6 +90,7 @@ func init() {
f.BoolP("list", "l", false, "List cheatsheets")
f.BoolP("regex", "r", false, "Treat search <phrase> as a regex")
f.BoolP("tags", "T", false, "List all tags in use")
f.BoolP("update", "u", false, "Update git-backed cheatpaths")
f.BoolP("version", "v", false, "Print the version number")
f.Bool("conf", false, "Display the config file path")
@@ -221,6 +225,7 @@ func run(cmd *cobra.Command, args []string) error {
listFlag, _ := f.GetBool("list")
briefFlag, _ := f.GetBool("brief")
tagsFlag, _ := f.GetBool("tags")
updateFlag, _ := f.GetBool("update")
tagVal, _ := f.GetString("tag")
switch {
@@ -239,6 +244,9 @@ func run(cmd *cobra.Command, args []string) error {
case tagsFlag:
cmdTags(cmd, args, conf)
case updateFlag:
cmdUpdate(cmd, args, conf)
case f.Changed("search"):
cmdSearch(cmd, args, conf)