-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
103 lines (87 loc) · 1.9 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
package main
//
// Usage ./release <owner> <repo> <tag_name> <file>
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"github.com/google/go-github/github"
"github.com/urfave/cli"
"golang.org/x/oauth2"
)
func validateArgs(a cli.Args) bool {
if !a.Present() {
return false
}
if len(a) < 4 {
return false
}
return true
}
func main() {
app := cli.NewApp()
app.Name = "release"
app.Usage = "Create and Release files to GitHub Releases"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "token",
Value: "",
Usage: "Your GitHub Access Token",
EnvVar: "GITHUB_ACCESS_TOKEN",
},
}
app.Action = func(c *cli.Context) error {
if !validateArgs(c.Args()) {
return fmt.Errorf("missing args")
}
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: c.String("token")},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
owner := c.Args().Get(0)
repo := c.Args().Get(1)
tagName := c.Args().Get(2)
filePath := c.Args().Get(3)
preRelease := true
release, _, err := client.Repositories.GetReleaseByTag(ctx, owner, repo, tagName)
if err != nil {
fmt.Printf("Creating new release for %s/%s @ %s...\n", owner, repo, tagName)
release = &github.RepositoryRelease{
TagName: &tagName,
Name: &tagName,
Prerelease: &preRelease,
}
release, _, err = client.Repositories.CreateRelease(ctx, owner, repo, release)
if err != nil {
return err
}
}
file, err := os.Open(filePath)
if err != nil {
return err
}
fmt.Printf("Uploading Release Asset %s as %s...\n", filePath, filepath.Base(filePath))
_, _, err = client.Repositories.UploadReleaseAsset(
ctx,
owner,
repo,
*release.ID,
&github.UploadOptions{
Name: filepath.Base(filePath),
},
file,
)
if err != nil {
return err
}
return nil
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}