feat: add up command

This commit is contained in:
James Walker 2024-07-07 11:43:57 -04:00
parent 3167f017c1
commit d565c4d8c2
Signed by: walkah
SSH Key Fingerprint: SHA256:f7Gn4jO4BFHZxWfKTTzEAfWz+cLW51IyGFl9MjDyZGI
5 changed files with 114 additions and 20 deletions

View File

@ -1,16 +1,16 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
Copyright © 2024 James Walker <walkah@walkah.net>
*/
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
}

32
cmd/up.go Normal file
View 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)
}

View File

@ -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": {

42
internal/git/git.go Normal file
View 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
View 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
}