✨ feat: add up command
This commit is contained in:
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