-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
65 lines (55 loc) · 1.47 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
package main
import (
"context"
"errors"
"flag"
"github.com/google/go-github/v47/github"
"github.com/guoyk93/grace"
"github.com/guoyk93/grace/gracemain"
"golang.org/x/oauth2"
"os"
"strings"
"time"
)
func main() {
var (
err error
ctx, _ = gracemain.WithSignalCancel(context.Background())
)
defer gracemain.Exit(&err)
defer grace.Guard(&err)
var (
optOwner string
optRepo string
optPath string
optBranch string
)
flag.StringVar(&optOwner, "owner", "guoyk93", "github repository owner")
flag.StringVar(&optRepo, "repo", "lastwill", "github repository name")
flag.StringVar(&optPath, "path", "beacon.txt", "path to beacon file")
flag.StringVar(&optBranch, "branch", "main", "branch name")
flag.Parse()
envToken := strings.TrimSpace(os.Getenv("GITHUB_TOKEN"))
if envToken == "" {
err = errors.New("missing environment variable $GITHUB_TOKEN")
return
}
client := github.NewClient(oauth2.NewClient(ctx, oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: envToken},
)))
content, _, _ := grace.Must3(
client.Repositories.GetContents(
ctx,
optOwner, optRepo, optPath, &github.RepositoryContentGetOptions{
Ref: optBranch,
},
),
)
newContent := time.Now().Format(time.RFC3339)
_, _ = grace.Must2(client.Repositories.UpdateFile(ctx, optOwner, optRepo, optPath, &github.RepositoryContentFileOptions{
Message: grace.Ptr("update beacon"),
Content: []byte(newContent),
SHA: content.SHA,
Branch: grace.Ptr(optBranch),
}))
}