✨ feat: add up command
This commit is contained in:
		
							
								
								
									
										20
									
								
								cmd/add.go
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								cmd/add.go
									
									
									
									
									
								
							| @@ -1,16 +1,16 @@ | |||||||
| /* | /* | ||||||
| Copyright © 2023 NAME HERE <EMAIL ADDRESS> | Copyright © 2024 James Walker <walkah@walkah.net> | ||||||
| */ | */ | ||||||
| package cmd | package cmd | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"os" | 	"os" | ||||||
| 	"strings" |  | ||||||
|  |  | ||||||
| 	git "github.com/go-git/go-git/v5" | 	git "github.com/go-git/go-git/v5" | ||||||
| 	"github.com/spf13/cobra" | 	"github.com/spf13/cobra" | ||||||
| 	"github.com/spf13/viper" | 	"github.com/spf13/viper" | ||||||
|  | 	"walkah.dev/walkah/gotem/internal/util" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| // addCmd represents the add command | // 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.`, | 	Long:  `This command adds the current path to the list of git clones to track.`, | ||||||
| 	Run: func(cmd *cobra.Command, args []string) { | 	Run: func(cmd *cobra.Command, args []string) { | ||||||
| 		if cwd, err := os.Getwd(); err == nil { | 		if cwd, err := os.Getwd(); err == nil { | ||||||
| 			relativePath, err := getRelativePath(cwd) | 			relativePath, err := util.GetRelativePath(cwd) | ||||||
| 			if err != nil { | 			if err != nil { | ||||||
| 				panic(err) | 				panic(err) | ||||||
| 			} | 			} | ||||||
| @@ -44,17 +44,3 @@ var addCmd = &cobra.Command{ | |||||||
| func init() { | func init() { | ||||||
| 	rootCmd.AddCommand(addCmd) | 	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 |  | ||||||
| } |  | ||||||
|   | |||||||
							
								
								
									
										32
									
								
								cmd/up.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								cmd/up.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,32 @@ | |||||||
|  | /* | ||||||
|  | Copyright © 2024 James Walker <walkah@walkah.net> | ||||||
|  | */ | ||||||
|  | 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) | ||||||
|  | } | ||||||
							
								
								
									
										6
									
								
								flake.lock
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										6
									
								
								flake.lock
									
									
									
										generated
									
									
									
								
							| @@ -20,11 +20,11 @@ | |||||||
|     }, |     }, | ||||||
|     "nixpkgs": { |     "nixpkgs": { | ||||||
|       "locked": { |       "locked": { | ||||||
|         "lastModified": 1719426051, |         "lastModified": 1720110830, | ||||||
|         "narHash": "sha256-yJL9VYQhaRM7xs0M867ZFxwaONB9T2Q4LnGo1WovuR4=", |         "narHash": "sha256-E5dN9GDV4LwMEduhBLSkyEz51zM17XkWZ3/9luvNOPs=", | ||||||
|         "owner": "nixos", |         "owner": "nixos", | ||||||
|         "repo": "nixpkgs", |         "repo": "nixpkgs", | ||||||
|         "rev": "89c49874fb15f4124bf71ca5f42a04f2ee5825fd", |         "rev": "c0d0be00d4ecc4b51d2d6948e37466194c1e6c51", | ||||||
|         "type": "github" |         "type": "github" | ||||||
|       }, |       }, | ||||||
|       "original": { |       "original": { | ||||||
|   | |||||||
							
								
								
									
										42
									
								
								internal/git/git.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								internal/git/git.go
									
									
									
									
									
										Normal file
									
								
							| @@ -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 | ||||||
|  | } | ||||||
							
								
								
									
										34
									
								
								internal/util/util.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								internal/util/util.go
									
									
									
									
									
										Normal file
									
								
							| @@ -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 | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user