-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
109 lines (87 loc) · 2.06 KB
/
main.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
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"fmt"
"os"
"os/exec"
"time"
"gopkg.in/yaml.v3"
)
const (
ConfigPath string = "/.push-me-config.yml"
)
func NewConfig() (*Config, error) {
home, _ := os.UserHomeDir()
fullConfigPath := home + ConfigPath
configFile, err := os.ReadFile(fullConfigPath)
if err != nil {
return nil, fmt.Errorf("Error reading config file: %s", err)
}
// Unmarshal the config file into the Config struct
var config Config
err = yaml.Unmarshal(configFile, &config)
if err != nil {
return nil, fmt.Errorf("Error unmarshaling config file: %v", err)
}
// Add home prefix to repos
for i, repo := range config.Repos {
config.Repos[i] = home + "/" + repo
}
return &config, nil
}
type Config struct {
Repos []string `yaml:"repos"`
}
// Git command wrapper for `git add`
func Add(repo string) error {
out, err := exec.Command("git", "-C", repo, "add", ".").Output()
if err != nil {
fmt.Println("`git add` exited abnormally")
return err
}
output := string(out)
fmt.Print(output)
return nil
}
// Git command wrapper for `git commit`
func Commit(repo string) error {
timestamp := time.Now()
message := "auto commit: " + timestamp.Format("20060102150405")
out, err := exec.Command("git", "-C", repo, "commit", "-m", message).Output()
if err != nil {
fmt.Println("`git commit` exited abnormally")
return err
}
output := string(out)
fmt.Print(output)
return nil
}
// Git command wrapper for `git push`
func Push(repo string) error {
out, err := exec.Command("git", "-C", repo, "push").Output()
if err != nil {
fmt.Println("`git push` exited abnormally")
return err
}
output := string(out)
fmt.Print(output)
return nil
}
func main() {
config, err := NewConfig()
if err != nil {
fmt.Println(err)
return
}
// Loop through the repos array
for _, repo := range config.Repos {
if err := Add(repo); err != nil {
fmt.Println("Git add returned an error")
}
if err := Commit(repo); err != nil {
fmt.Println("Git commit returned an error")
}
if err := Push(repo); err != nil {
fmt.Println("Git push returned an error")
}
}
}