-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
199 lines (157 loc) · 4.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
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
package main
import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
"reflect"
"slices"
"github.com/google/go-github/v61/github"
"github.com/joho/godotenv"
"github.com/urfave/cli/v3"
"golang.org/x/net/context"
)
const defaultGloveDirectory = "/Volumes"
const leftGloveFilename = "GLV80LHBOOT"
const rightGloveFilename = "GLV80RHBOOT"
const tempArtifactZipFilename = "temp.zip"
const artifactFilename = "glove80.uf2"
func verifyGlovesConnected(glovePath string) (err error) {
leftGlovePath := filepath.Join(glovePath, leftGloveFilename)
lconnected, err := exists(leftGlovePath)
if err != nil {
return
}
rightGlovePath := filepath.Join(glovePath, rightGloveFilename)
rconnected, err := exists(rightGlovePath)
if err != nil {
return
}
if !lconnected {
err = errors.Join(err, errors.New(fmt.Sprintf("Left glove not connected in bootloader mass storage device mode (%s)", leftGlovePath)))
}
if !rconnected {
err = errors.Join(err, errors.New(fmt.Sprintf("Right glove not connected in bootloader mass storage device mode (%s)", rightGlovePath)))
}
return
}
func getLatestArtifact(client *github.Client, owner string, repo string) (*github.Artifact, error) {
artifacts, _, err := client.Actions.ListArtifacts(context.Background(), owner, repo, nil)
if err != nil {
return nil, err
}
if len(artifacts.Artifacts) < 1 {
return nil, errors.New("No artifacts to flash")
}
slices.SortFunc(artifacts.Artifacts, func(i, j *github.Artifact) int {
return j.CreatedAt.GetTime().Compare(*i.CreatedAt.GetTime())
})
latestArtifact := artifacts.Artifacts[0]
return latestArtifact, nil
}
func downloadArtifact(client *github.Client, owner string, repo string, artifactId int64) error {
artifactUrl, _, err := client.Actions.DownloadArtifact(context.Background(), owner, repo, int64(artifactId), 1)
if err != nil {
return err
}
cwd, err := os.Getwd()
if err != nil {
return err
}
downloadDestination := filepath.Join(cwd, tempArtifactZipFilename)
if err := downloadFile(downloadDestination, artifactUrl.String()); err != nil {
return err
}
defer os.Remove(downloadDestination)
if err := unzip(downloadDestination, cwd); err != nil {
return err
}
return nil
}
type FlashConfig struct {
owner string
repo string
glovePath string
}
func flash(config FlashConfig) (err error) {
err = verifyGlovesConnected(config.glovePath)
if err != nil {
return
}
fmt.Printf("Downloading latest uf2 artifact from %s/%s\n", config.owner, config.repo)
token := os.Getenv("GITHUB_PAT")
client := github.NewClient(nil).WithAuthToken(token)
fmt.Println("Successfully authenticated with GitHub")
artifact, err := getLatestArtifact(client, config.owner, config.repo)
if err != nil {
return fmt.Errorf("Error fetching latest uf2 artifact ID: %w", err)
}
fmt.Printf("Latest artifact is %d created at %v\n", artifact.GetID(), artifact.GetCreatedAt())
if artifact.GetExpired() {
return errors.New("Latest artifact is expired")
}
if err = downloadArtifact(client, config.owner, config.repo, artifact.GetID()); err != nil {
return fmt.Errorf("Error downloading latest uf2 artifact: %w", err)
}
cwd, err := os.Getwd()
if err != nil {
return
}
artifactPath := filepath.Join(cwd, artifactFilename)
// TODO: What if it's not extracted with this name?
// Rename the file to a known one after it's extracted?
defer os.Remove(artifactPath)
leftGlovePath := filepath.Join(config.glovePath, leftGloveFilename, artifactFilename)
fmt.Printf("Copying uf2 to left glove at %v\n", leftGlovePath)
err = copy(artifactPath, leftGlovePath)
if err != nil {
fmt.Printf("%v", reflect.TypeOf(err))
return
}
rightGlovePath := filepath.Join(config.glovePath, rightGloveFilename, artifactFilename)
fmt.Printf("Copying uf2 to right glove at %v\n", rightGlovePath)
err = copy(artifactPath, rightGlovePath)
if err != nil {
return
}
fmt.Println("Success!")
return nil
}
func main() {
cmd := &cli.Command{
Name: "flash",
Usage: "Utility for flashing new config to a Glove80 from a ZMK repo",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "directory",
Usage: "Specify the directory where the Glove80 bootloader storage directories will appear",
Value: defaultGloveDirectory,
Aliases: []string{"d"},
DefaultText: defaultGloveDirectory,
},
},
Action: func(ctx context.Context, cmd *cli.Command) (err error) {
if err = godotenv.Load(); err != nil {
return
}
owner, set := os.LookupEnv("OWNER")
if !set {
return errors.New("No OWNER provided in env")
}
repo, set := os.LookupEnv("REPO")
if !set {
return errors.New("No REPO provided in env")
}
_, set = os.LookupEnv("GITHUB_PAT")
if !set {
return errors.New("No GITHUB_PAT provided in env")
}
err = flash(FlashConfig{owner: owner, repo: repo, glovePath: cmd.String("directory")})
return
},
}
if err := cmd.Run(context.Background(), os.Args); err != nil {
log.Fatal(err)
}
}