From d565c4d8c246b1cd9d565bc77d0bacaea86e76ca Mon Sep 17 00:00:00 2001 From: James Walker Date: Sun, 7 Jul 2024 11:43:57 -0400 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20add=20up=20command?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/add.go | 20 +++----------------- cmd/up.go | 32 ++++++++++++++++++++++++++++++++ flake.lock | 6 +++--- internal/git/git.go | 42 ++++++++++++++++++++++++++++++++++++++++++ internal/util/util.go | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 114 insertions(+), 20 deletions(-) create mode 100644 cmd/up.go create mode 100644 internal/git/git.go create mode 100644 internal/util/util.go diff --git a/cmd/add.go b/cmd/add.go index 38b40e0..94ce0c7 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -1,16 +1,16 @@ /* -Copyright © 2023 NAME HERE +Copyright © 2024 James Walker */ package cmd import ( "fmt" "os" - "strings" git "github.com/go-git/go-git/v5" "github.com/spf13/cobra" "github.com/spf13/viper" + "walkah.dev/walkah/gotem/internal/util" ) // addCmd represents the add command @@ -20,7 +20,7 @@ var addCmd = &cobra.Command{ Long: `This command adds the current path to the list of git clones to track.`, Run: func(cmd *cobra.Command, args []string) { if cwd, err := os.Getwd(); err == nil { - relativePath, err := getRelativePath(cwd) + relativePath, err := util.GetRelativePath(cwd) if err != nil { panic(err) } @@ -44,17 +44,3 @@ var addCmd = &cobra.Command{ func init() { rootCmd.AddCommand(addCmd) } - -func getRelativePath(absolutePath string) (string, error) { - homeDir, err := os.UserHomeDir() - if err != nil { - return "", err - } - - if strings.HasPrefix(absolutePath, homeDir) { - relativePath := strings.TrimPrefix(absolutePath, fmt.Sprintf("%s/", homeDir)) - return relativePath, nil - } - - return absolutePath, nil -} diff --git a/cmd/up.go b/cmd/up.go new file mode 100644 index 0000000..e24ebc2 --- /dev/null +++ b/cmd/up.go @@ -0,0 +1,32 @@ +/* +Copyright © 2024 James Walker +*/ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" + "github.com/spf13/viper" + "walkah.dev/walkah/gotem/internal/git" +) + +// upCmd represents the up command +var upCmd = &cobra.Command{ + Use: "up", + Short: "Update yer git", + Long: `Pulls the latest in all repos`, + Run: func(cmd *cobra.Command, args []string) { + for _, project := range viper.AllSettings() { + path := project.(map[string]interface{})["path"].(string) + err := git.PullLatest(path) + if err != nil { + fmt.Println(err) + } + } + }, +} + +func init() { + rootCmd.AddCommand(upCmd) +} diff --git a/flake.lock b/flake.lock index 9d1efed..e18fc6a 100644 --- a/flake.lock +++ b/flake.lock @@ -20,11 +20,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1719426051, - "narHash": "sha256-yJL9VYQhaRM7xs0M867ZFxwaONB9T2Q4LnGo1WovuR4=", + "lastModified": 1720110830, + "narHash": "sha256-E5dN9GDV4LwMEduhBLSkyEz51zM17XkWZ3/9luvNOPs=", "owner": "nixos", "repo": "nixpkgs", - "rev": "89c49874fb15f4124bf71ca5f42a04f2ee5825fd", + "rev": "c0d0be00d4ecc4b51d2d6948e37466194c1e6c51", "type": "github" }, "original": { diff --git a/internal/git/git.go b/internal/git/git.go new file mode 100644 index 0000000..45639e3 --- /dev/null +++ b/internal/git/git.go @@ -0,0 +1,42 @@ +package git + +import ( + "fmt" + + "github.com/go-git/go-git/v5" + "walkah.dev/walkah/gotem/internal/util" +) + +func PullLatest(repoPath string) error { + fmt.Println("Pulling the latest changes for", repoPath, "...") + path, err := util.GetAbsolutePath(repoPath) + if err != nil { + return err + } + + r, err := git.PlainOpen(path) + if err != nil { + return err + } + + w, err := r.Worktree() + if err != nil { + return err + } + + options := &git.PullOptions{ + RemoteName: "origin", + } + + err = w.Pull(options) + if err != nil { + if err == git.NoErrAlreadyUpToDate { + fmt.Println("Already up to date.") + return nil + } + return err + } + + fmt.Println("Repository has been updated.") + return nil +} diff --git a/internal/util/util.go b/internal/util/util.go new file mode 100644 index 0000000..55777cc --- /dev/null +++ b/internal/util/util.go @@ -0,0 +1,34 @@ +package util + +import ( + "fmt" + "os" + "strings" +) + +func GetRelativePath(absolutePath string) (string, error) { + homeDir, err := os.UserHomeDir() + if err != nil { + return "", err + } + + if strings.HasPrefix(absolutePath, homeDir) { + relativePath := strings.TrimPrefix(absolutePath, fmt.Sprintf("%s/", homeDir)) + return relativePath, nil + } + + return absolutePath, nil +} + +func GetAbsolutePath(relativePath string) (string, error) { + homeDir, err := os.UserHomeDir() + if err != nil { + return "", err + } + + if !strings.HasPrefix(relativePath, "/") { + relativePath = fmt.Sprintf("%s/%s", homeDir, relativePath) + } + + return relativePath, nil +}