-
Notifications
You must be signed in to change notification settings - Fork 0
/
syscalls.go
57 lines (47 loc) · 1.2 KB
/
syscalls.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
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"time"
)
func commit(date time.Time) {
filePath := fmt.Sprintf("%d/%d-%d.txt", date.Year(), date.Month(), date.Day())
text := fmt.Sprintf("commit for " + dateFormat(date))
//append some dummy changes to the file
appendToFile(filePath, text)
env := os.Environ()
env = append(env, fmt.Sprintf("GIT_COMMITTER_DATE=\"%s\"", dateFormat(date)))
cmd := exec.Command("git", "add", ".")
cmd.Env = env
err := cmd.Run()
if err != nil {
fmt.Println(err)
}
cmd = exec.Command("git", "commit", "--date", fmt.Sprintf("\"%s\"", dateFormat(date)), "-m", fmt.Sprintf("\"%s\"", text))
cmd.Env = env
err = cmd.Run()
if err != nil {
fmt.Println(err)
}
}
func appendToFile(filePath, text string) {
folderPath := filepath.Dir(filePath)
if _, err := os.Stat(folderPath); os.IsNotExist(err) {
err = os.MkdirAll(folderPath, 0755)
if err != nil {
fmt.Println(err)
panic("can't create commit-folder, make sure path exists ")
}
}
f, err := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println(err)
}
defer f.Close()
if _, err := f.WriteString(text + "\n"); err != nil {
log.Println(err)
}
}