-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
232 lines (198 loc) · 4.59 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package main
import (
"archive/tar"
"context"
"encoding/base64"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"github.com/google/go-github/v32/github"
"golang.org/x/oauth2"
)
var (
name = flag.String("name", "", "the author name, defaults to the owner name of the token")
email = flag.String("email", "", "the author email, defaults to the owner email of the token")
branch = flag.String("branch", "main", "the git branch")
commitMsg = flag.String("commit-msg", "update submitted via go-ghwrite", "the commit message")
readTar = flag.Bool("read-tar", false, "interpret input as tarball and upload individual files")
)
func usage() {
fmt.Fprintf(os.Stderr, `Usage of: %s [opts]
# single file
go-ghwrite [opts] repo/slug:targetfile < sourcefile
# multiple files
tar cvf - file1 file2 file3 | go-ghwrite -read-tar repo/slug:
Parameters:
`, os.Args[0])
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, ("\nA valid Github token with scope `repo` is required in GOGHWRITE_TOKEN.\n"))
}
type GithubWriter struct {
ctx context.Context
client *github.Client
owner string
repo string
}
func main() {
flag.Usage = usage
flag.Parse()
if (*name != "" && *email == "") || (*name == "" && *email != "") {
flag.Usage()
os.Exit(1)
}
var owner, repo, destpath string
if flag.NArg() != 1 {
flag.Usage()
os.Exit(1)
}
wannabeSlug := flag.Arg(0)
if !strings.Contains(wannabeSlug, ":") {
flag.Usage()
os.Exit(1)
}
split := strings.Split(wannabeSlug, ":")
destpath = split[1]
if !strings.Contains(split[0], "/") {
flag.Usage()
os.Exit(1)
}
reposlug := strings.Split(split[0], "/")
owner = reposlug[0]
repo = reposlug[1]
var token string
if v, ok := os.LookupEnv("GOGHWRITE_TOKEN"); !ok {
flag.Usage()
os.Exit(1)
} else {
token = v
}
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
writer := GithubWriter{
ctx: ctx,
client: client,
owner: owner,
repo: repo,
}
repoSHA, _, err := client.Repositories.GetCommitSHA1(ctx, owner, repo, *branch, "")
if err != nil {
fmt.Println(err)
return
}
entries := make([]*github.TreeEntry, 0)
if *readTar {
tr := tar.NewReader(os.Stdin)
for {
hdr, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
fmt.Println(err)
return
}
data, err := ioutil.ReadAll(tr)
if err != nil {
fmt.Println(err)
return
}
path := hdr.Name
if destpath != "" {
path = destpath + "/" + path
}
fmt.Printf("staging %s… ", path)
sha, err := writer.createBlob(data)
if err != nil {
fmt.Println(err)
continue
}
fmt.Println(sha)
entry := treeEntryBlob(path, sha)
entries = append(entries, entry)
}
} else {
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("staging %s\n", destpath)
sha, err := writer.createBlob(data)
if err != nil {
fmt.Println(err)
return
}
entry := treeEntryBlob(destpath, sha)
entries = append(entries, entry)
}
if len(entries) == 0 {
return
}
tree, _, err := client.Git.CreateTree(ctx, owner, repo, repoSHA, entries)
if err != nil {
fmt.Println(err)
return
}
commit := &github.Commit{}
if *name != "" {
author := &github.CommitAuthor{}
author.Name = name
author.Email = email
commit.Author = author
}
commit.Message = commitMsg
commit.Tree = tree
c := github.Commit{}
c.SHA = &repoSHA
commit.Parents = []*github.Commit{&c}
newCommit, _, err := client.Git.CreateCommit(ctx, owner, repo, commit)
if err != nil {
fmt.Println(err)
return
}
ref := &github.Reference{}
branchRef := fmt.Sprintf("refs/heads/%s", *branch)
ref.Ref = &branchRef
obj := &github.GitObject{}
commitType := "commit"
obj.Type = &commitType
commitSHA := newCommit.GetSHA()
obj.SHA = &commitSHA
ref.Object = obj
_, _, err = client.Git.UpdateRef(ctx, owner, repo, ref, false)
if err != nil {
fmt.Println(err)
return
}
fmt.Fprintf(os.Stderr, "committed as %s\n", commitSHA)
}
func (w GithubWriter) createBlob(data []byte) (string, error) {
fblob := &github.Blob{}
b64c := "base64"
fblob.Encoding = &b64c
b64data := base64.StdEncoding.EncodeToString(data)
fblob.Content = &b64data
blob, _, err := w.client.Git.CreateBlob(w.ctx, w.owner, w.repo, fblob)
if err != nil {
return "", err
}
return blob.GetSHA(), nil
}
func treeEntryBlob(path string, fileSHA string) *github.TreeEntry {
mode := "100644"
blobType := "blob"
entry := github.TreeEntry{
SHA: &fileSHA,
Path: &path,
Mode: &mode,
Type: &blobType,
}
return &entry
}