-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
179 lines (149 loc) · 3.98 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"context"
"crypto/sha1"
"encoding/hex"
"fmt"
"io"
"log"
"os"
"os/signal"
"path/filepath"
"strconv"
"syscall"
"time"
"github.com/google/go-github/v66/github"
"golang.org/x/oauth2"
)
func main() {
accessToken := os.Getenv("GITHUB_PAT")
if accessToken == "" {
log.Fatal("GITHUB_PAT is not set")
}
owner := os.Getenv("REPO_OWNER")
if accessToken == "" {
log.Fatal("REPO_OWNER is not set")
}
repo := os.Getenv("REPO_NAME")
if accessToken == "" {
log.Fatal("REPO_NAME is not set")
}
basePath := os.Getenv("BASE_PATH")
if accessToken == "" {
log.Fatal("BASE_PATH is not set")
}
// graceful shutdown
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()
go func() {
<-signalChan
fmt.Println("Interrupt signal received, cleaning up...")
cancel()
os.Exit(0)
}()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: accessToken},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
err := getContents(ctx, client, "", owner, repo, basePath)
if err != nil {
log.Fatalf("Error fetching repository contents: %v", err)
}
}
func check(err error) bool {
if err != nil {
log.Println("Error:", err)
return true
}
return false
}
func createDirectory(path string, basePath string) error {
if path == "" {
return fmt.Errorf("invalid directory path")
}
destination := filepath.Join(basePath, path)
err := os.Mkdir(destination, 0755)
if err != nil && !os.IsExist(err) {
return fmt.Errorf("error creating directory %s: %w", destination, err)
}
log.Printf("Created directory: %s", destination)
return nil
}
func getContents(ctx context.Context, client *github.Client, path string, owner string, repo string, basePath string) error {
_, directoryContent, _, err := client.Repositories.GetContents(ctx, owner, repo, path, nil)
if check(err) {
return err
}
for _, c := range directoryContent {
log.Println("Processing:", *c.Type, *c.Path, *c.Size, *c.SHA)
local := filepath.Join(basePath, *c.Path)
log.Println("Local path:", local)
switch *c.Type {
case "file":
err := handleFile(ctx, client, c, local, owner, repo, basePath)
if err != nil {
log.Printf("Error handling file: %v", err)
}
case "dir":
err := createDirectory(*c.Path, basePath)
if err != nil {
log.Printf("Error creating directory: %v", err)
}
getContents(ctx, client, *c.Path, owner, repo, basePath)
}
}
return nil
}
func handleFile(ctx context.Context, client *github.Client, content *github.RepositoryContent, localPath string, owner string, repo string, basePath string) error {
// Check if file exists and compare SHA1
_, err := os.Stat(localPath)
if err == nil {
sha := calculateGitSHA1(localPath)
if *content.SHA == sha {
log.Printf("No need to update file %s, SHA1 is the same", localPath)
return nil
}
}
return downloadContents(ctx, client, content, localPath, owner, repo)
}
func downloadContents(ctx context.Context, client *github.Client, content *github.RepositoryContent, localPath string, owner string, repo string) error {
rc, _, err := client.Repositories.DownloadContents(ctx, owner, repo, *content.Path, nil)
if check(err) {
return err
}
defer rc.Close()
b, err := io.ReadAll(rc)
if check(err) {
return err
}
log.Printf("Writing file: %s", localPath)
f, err := os.Create(localPath)
if check(err) {
return err
}
defer f.Close()
n, err := f.Write(b)
if check(err) {
return err
}
if n != *content.Size {
log.Printf("Warning: written bytes %d do not match expected size %d", n, *content.Size)
}
return nil
}
func calculateGitSHA1(filePath string) string {
b, err := os.ReadFile(filePath)
if check(err) {
return ""
}
contentLen := len(b)
blobSlice := []byte("blob " + strconv.Itoa(contentLen))
blobSlice = append(blobSlice, '\x00')
blobSlice = append(blobSlice, b...)
h := sha1.New()
h.Write(blobSlice)
return hex.EncodeToString(h.Sum(nil))
}