-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.go
59 lines (49 loc) · 1.4 KB
/
backup.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
package main
import (
"fmt"
"os"
"strings"
"time"
)
func setupRepo() {
// Check if folder exists
_, err := os.Stat(GitRepoFolder)
if os.IsNotExist(err) {
fmt.Println("Git repo folder doesn't exist. Edit your settings: " + getSettingsFile())
os.Exit(1)
}
// Check if folder is a git repository
_, err = runCommand("git status", GitRepoFolder, false)
if err != nil {
fmt.Println("Folder is not initialized with git.")
os.Exit(1)
}
}
func checkForGit() bool {
_, err := runCommand("git --version", "", false)
if err != nil {
return false
}
return true
}
func commitBackup() {
fmt.Println("Committing and pushing updated notes.")
commitMsg := "Backup " + time.Now().Format("02.01.06 - 15:04:05") + ""
commands := [3]string{"git add *", "git commit -a -m \"" + commitMsg + "\"", "git push"}
for _, command := range commands {
out, err := runCommand(command, GitRepoFolder, true)
if err != nil {
outStr := strings.ToLower(string(out))
if strings.Contains(outStr, "nothing to commit") {
fmt.Println("No updated notes to commit.")
} else if strings.Contains(outStr, "authentication failed") {
fmt.Println("Authentication failed! Login and save credentials for git in the command line or follow authentication prompt.")
os.Exit(1)
} else {
fmt.Println(fmt.Sprint(err) + ": " + string(out))
os.Exit(1)
}
}
}
fmt.Println("Pushed Commit '" + commitMsg + "'")
}