-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-saver.go
97 lines (88 loc) · 2.24 KB
/
git-saver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"time"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/urfave/cli/v2"
)
var (
configFileName = ".git-saver.config.yaml"
defaultCommitMessage = "updated by https://github.com/flobilosaurus/git-saver"
defaultConfigContent = `
repositories:
- localPath: ~/repositories/dotfiles
objectsToSave:
- path: ~/.config/nvim
relativePathInRepo: my-nvim-conf
`
)
func GetConfigPath() string {
userHomePath, err := os.UserHomeDir()
CheckIfError(err)
return filepath.Join(userHomePath, configFileName)
}
func CreateInitialConfig(configFilePath string) {
configExists, err := DoesFileExist(configFilePath)
CheckIfError(err)
if !configExists {
err = os.WriteFile(configFilePath, []byte(defaultConfigContent), 0755)
CheckIfError(err)
} else {
log.Printf("Did not create config as it already exists under: %s", configFilePath)
}
}
func SaveAllObjects() {
configPath := GetConfigPath()
config := ReadConfig(configPath)
for _, configRepo := range config.Repositories {
repoName := GetEqualPadded(configRepo.LocalPath, 40)
processingRepoOutput := fmt.Sprintf("\rprocessing repo '%s' -", repoName)
fmt.Printf("%s", processingRepoOutput)
repo := OpenRepo(configRepo.LocalPath)
Pull(repo)
CopyAllObjectsToRepo(configRepo)
if HasChanges(repo) {
CommitAll(repo, defaultCommitMessage, &object.Signature{
When: time.Now(),
})
fmt.Printf("%s commited ", processingRepoOutput)
} else {
fmt.Printf("%s up to date ", processingRepoOutput)
}
wasPushed := Push(repo)
if wasPushed {
fmt.Printf("%s pushed ", processingRepoOutput)
}
fmt.Print("\n")
}
}
func main() {
app := &cli.App{
Name: "git-saver",
Usage: "automates saving files and folders to git repos",
Commands: []*cli.Command{
{
Name: "save",
Usage: "save all configured files to their repositories",
Action: func(ctx *cli.Context) error {
SaveAllObjects()
return nil
},
},
{
Name: "init-config",
Usage: "create initial config in users home directory",
Action: func(cCtx *cli.Context) error {
CreateInitialConfig(GetConfigPath())
return nil
},
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}