forked from zsa/wally-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
96 lines (82 loc) · 1.86 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
package main
import (
"flag"
"fmt"
"github.com/briandowns/spinner"
"gopkg.in/cheggaaa/pb.v1"
"net/url"
"os"
"path/filepath"
"time"
)
var appVersion = "2.0.0"
type state struct {
step int
total int
sent int
}
func main() {
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s: [flags] <firmware file>\n", os.Args[0])
flag.PrintDefaults()
}
version := flag.Bool("version", false, "print the version and exit")
flag.Parse()
if *version {
fmt.Println(fmt.Sprintf("wally-cli v%s", appVersion))
os.Exit(0)
}
if flag.NArg() != 1 {
flag.Usage()
os.Exit(2)
}
path := ""
extension := ""
if uri, err := url.Parse(flag.Arg(0)); err == nil {
switch uri.Scheme {
case "", "file":
extension = filepath.Ext(uri.Path)
switch extension {
case ".bin", ".hex":
path = uri.Path
}
}
}
if path == "" {
fmt.Println("Please provide a valid firmware file: a .hex file (ErgoDox EZ) or a .bin file (Moonlander / Planck EZ)")
os.Exit(2)
}
if _, err := os.Stat(path); os.IsNotExist(err) {
fmt.Println("The file path you specified does not exist:", path)
os.Exit(1)
}
sp := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
sp.Suffix = " Press the reset button of your keyboard."
sp.Start()
spinnerStopped := false
var progress *pb.ProgressBar
progressStarted := false
s := state{step: 0, total: 0, sent: 0}
if extension == ".bin" {
go dfuFlash(path, &s)
}
if extension == ".hex" {
go teensyFlash(path, &s)
}
for s.step != 2 {
time.Sleep(500 * time.Millisecond)
if s.step > 0 {
if spinnerStopped == false {
sp.Stop()
spinnerStopped = true
}
if progressStarted == false {
progressStarted = true
progress = pb.StartNew(s.total)
}
progress.Set(s.sent)
}
}
progress.Finish()
fmt.Println("Your keyboard was successfully flashed and rebooted. Enjoy the new firmware!")
}