-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
202 lines (157 loc) · 3.95 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"context"
"flag"
"fmt"
"hash/crc32"
"io/fs"
"log"
"os"
"path/filepath"
"time"
"cloud.google.com/go/storage"
"github.com/gobwas/glob"
"google.golang.org/api/option"
"gopkg.in/yaml.v2"
)
var (
credentialsFile = flag.String("credentials-file", "", "The Google service account credentials file")
bucketName = flag.String("bucket-name", "", "The Google Cloud Storage bucket name")
configFile = flag.String("config-file", "", "The configuration file")
)
func main() {
flag.Parse()
if *bucketName == "" {
log.Fatalf("Bucket name is not configured")
}
now := time.Now()
config, err := newConfig()
if err != nil {
log.Fatalf("Error creating config: %v", err)
}
matcher, err := newMatcher(config)
if err != nil {
log.Fatalf("Error creating matcher: %v", err)
}
client, err := newClient()
if err != nil {
log.Fatalf("Error creating client: %v", err)
}
var paths []string
err = filepath.WalkDir(".", func(path string, info fs.DirEntry, err error) error {
if info.IsDir() {
return nil
}
if !matcher.match(path) {
return nil
}
paths = append(paths, path)
return nil
})
if err != nil {
log.Fatalf("Error walking files: %v", err)
return
}
for _, path := range paths {
data, err := os.ReadFile(path)
if err != nil {
log.Printf("Error reading file: %v", err)
continue
}
err = client.updateObject(path, data, now)
if err != nil {
log.Printf("Error updating object: %v", err)
continue
}
}
}
type Config struct {
Files []string `json:"files"`
}
func newConfig() (*Config, error) {
config := Config{}
if *configFile == "" {
return &config, nil
}
data, err := os.ReadFile(*configFile)
if err != nil {
return nil, fmt.Errorf("error reading configuration file '%v': %v", *configFile, err)
}
err = yaml.Unmarshal(data, &config)
if err != nil {
return nil, fmt.Errorf("error parsing configuration file '%v': %v", *configFile, err)
}
return &config, nil
}
type Matcher struct {
globs []glob.Glob
}
func newMatcher(config *Config) (*Matcher, error) {
matcher := Matcher{}
for _, pattern := range config.Files {
glob, err := glob.Compile(pattern, '/')
if err != nil {
return nil, fmt.Errorf("error compiling pattern '%v': %v", pattern, err)
}
matcher.globs = append(matcher.globs, glob)
}
return &matcher, nil
}
func (m *Matcher) match(value string) bool {
for _, glob := range m.globs {
if glob.Match(value) {
return true
}
}
return false
}
type Client struct {
client *storage.Client
}
func newClient() (*Client, error) {
ctx := context.Background()
var options []option.ClientOption
if *credentialsFile != "" {
options = append(options, option.WithCredentialsFile(*credentialsFile))
}
client, err := storage.NewClient(ctx, options...)
if err != nil {
return nil, fmt.Errorf("error creating client: %v", err)
}
return &Client{
client: client,
}, nil
}
func (c *Client) updateObject(path string, data []byte, now time.Time) error {
ctx := context.Background()
object := c.client.Bucket(*bucketName).Object(path)
attrs, err := object.Attrs(ctx)
if err != nil && err != storage.ErrObjectNotExist {
return fmt.Errorf("error reading attributes for object '%v': %v", path, err)
}
CRC32C := crc32.Checksum(data, crc32.MakeTable(crc32.Castagnoli))
if attrs == nil || attrs.CRC32C != CRC32C {
writer := object.NewWriter(ctx)
writer.CustomTime = now
writer.CRC32C = CRC32C
writer.SendCRC32C = true
_, err = writer.Write(data)
if err != nil {
return fmt.Errorf("error writing data for object '%v': %v", path, err)
}
err = writer.Close()
if err != nil {
return fmt.Errorf("error writing object '%v': %v", path, err)
}
log.Printf("Wrote object '%v'", path)
} else {
_, err = object.Update(ctx, storage.ObjectAttrsToUpdate{
CustomTime: now,
})
if err != nil {
return fmt.Errorf("error updating object attributes for '%v': %v", path, err)
}
log.Printf("Updated attributes for object '%v'", path)
}
return nil
}