/
googleutf.go
51 lines (40 loc) · 1.11 KB
/
googleutf.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
package publisher
import (
"bytes"
"log/slog"
"os"
"github.com/go-git/go-billy/v5"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/jonhadfield/ip-fetcher/providers/googleutf"
)
const googleutfFile = "googleutf.json"
func syncGoogleUTF(wt *git.Worktree, fs billy.Filesystem) (plumbing.Hash, error) {
a := googleutf.New()
originContent, _, _, err := a.FetchData()
if err != nil {
return plumbing.ZeroHash, err
}
rgb, err := fs.Open(googleutfFile)
if err != nil && !os.IsNotExist(err) {
return plumbing.ZeroHash, err
}
// if the file doesn't exist, we need to create it
if err == nil {
var upToDate bool
upToDate, err = isUpToDate(bytes.NewReader(originContent), rgb)
if err != nil || upToDate {
return plumbing.ZeroHash, err
}
slog.Info(googleutfFile, "up to date", upToDate)
}
if err = createFile(fs, googleutfFile, originContent); err != nil {
return plumbing.ZeroHash, err
}
// Adds the new file to the staging area.
_, err = wt.Add(googleutfFile)
if err != nil {
return plumbing.ZeroHash, err
}
return createCommit(wt, "update googleutf data")
}