Skip to content

Commit e71e4b4

Browse files
author
Slugsy
committed
Fixing download of binaries and adding unit tests
1 parent d7c0e53 commit e71e4b4

File tree

4 files changed

+85
-30
lines changed

4 files changed

+85
-30
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ go 1.14
44

55
require (
66
github.com/gorilla/websocket v1.4.2
7+
github.com/stretchr/testify v1.7.0
78
)

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,12 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
13
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
24
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
5+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
6+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
8+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
9+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
10+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
11+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
12+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

webcam.go

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package camtron
33
import (
44
"encoding/base64"
55
"encoding/json"
6+
"errors"
7+
"fmt"
68
"io"
79
"io/ioutil"
810
"log"
@@ -81,23 +83,39 @@ func Shellout(shell string, args ...string) error {
8183
return err
8284
}
8385

84-
func checkForElectronBinary() {
85-
goos := runtime.GOOS
86+
func getLatestUIVersion() (string, error) {
87+
url := "https://api.github.com/repos/vee2xx/camtron-ui/releases"
8688

87-
var filename string
88-
switch goos {
89-
case "windows":
90-
filename = "camtron-win32-x64.zip"
91-
case "darwin":
92-
filename = "camtron-darwin-x64.zip"
93-
case "linux":
94-
filename = "camtron-linux-x64.zip"
95-
default:
96-
log.Fatal("Unsupported OS: %s.\n", goos)
89+
res, err := http.Get(url)
90+
if err != nil {
91+
log.Fatal(err.Error())
9792
}
93+
defer res.Body.Close()
94+
95+
body, err := ioutil.ReadAll(res.Body)
96+
if err != nil {
97+
panic(err.Error())
98+
}
99+
100+
var releases []map[string]interface{}
101+
if err := json.Unmarshal(body, &releases); err != nil {
102+
log.Fatal(err)
103+
}
104+
105+
if len(releases) == 0 {
106+
return "", errors.New("unable to find any versions")
107+
}
108+
return fmt.Sprintf("%v", releases[0]["tag_name"]), nil
109+
}
98110

111+
func downloadBinary(electronBinary string) {
112+
latest, err := getLatestUIVersion()
113+
if err != nil {
114+
log.Fatal(err.Error())
115+
}
116+
filename := electronBinary + ".zip"
99117
if _, err := os.Stat(filename); os.IsNotExist(err) {
100-
url := "https://github.com/vee2xx/camtron-ui/releases/download/v1.0.0/" + filename
118+
url := fmt.Sprintf("https://github.com/vee2xx/camtron-ui/releases/download/%s/%s", latest, filename)
101119

102120
file, err := http.Get(url)
103121
if err != nil {
@@ -116,45 +134,48 @@ func checkForElectronBinary() {
116134
if err != nil {
117135
log.Fatal(err)
118136
}
137+
}
138+
}
119139

120-
cmd := exec.Command("bash", "-c", "unzip camtron-darwin-x64.zip")
121-
err = cmd.Run()
122-
if err != nil {
123-
log.Fatal(err)
124-
}
140+
func UnzipBinary(electronBinary string) {
141+
cmd := exec.Command("bash", "-c", "unzip "+electronBinary+".zip")
142+
err := cmd.Run()
143+
if err != nil {
144+
log.Fatal(err)
125145
}
126146
}
147+
127148
func StartElectron() {
128149
log.Println("INFO: starting electron")
129150

130-
checkForElectronBinary()
131151
goos := runtime.GOOS
132152

133-
// _, filename, _, ok := runtime.Caller(0)
134-
135-
// if !ok {
136-
// panic("No caller information")
137-
// }
138-
139-
// goPath := path.Dir(filename)
140-
141153
var shell string
142154
var args []string
155+
var electronBinary string
143156
switch goos {
144157
case "windows":
145158
shell = "cmd"
146159
args = append(args, "/C")
147160
args = append(args, "cd camtron-win32-x64 && camtron.exe")
161+
electronBinary = "camtron-win32-x64"
148162
case "darwin":
149163
shell = "bash"
150164
args = append(args, "-c")
151165
args = append(args, "cd camtron-darwin-x64 && open camtron.app")
166+
electronBinary = "camtron-darwin-x64"
152167
case "linux":
153168
shell = "bash"
154169
args = append(args, "-c")
155170
args = append(args, "cd camtron-linux-x64 && ./camtron")
171+
electronBinary = "camtron-linux-x64"
156172
default:
157-
log.Println("Unsupported OS: %s.\n", goos)
173+
log.Fatalf("Unsupported OS: %s.\n", goos)
174+
}
175+
176+
if _, err := os.Stat(electronBinary); os.IsNotExist(err) {
177+
downloadBinary(electronBinary)
178+
UnzipBinary(electronBinary)
158179
}
159180

160181
log.Print("Starting Electron")
@@ -170,14 +191,14 @@ func StartElectron() {
170191
select {
171192
case sig := <-c:
172193
if goos == "windows" {
173-
log.Println("Got %s signal. Its windows so gotta kill Electron\n", sig)
194+
log.Printf("Got %s signal. Its windows so gotta kill Electron\n", sig)
174195
cmd := exec.Command("cmd", "/C", " taskkill /f /im camtron.exe")
175196
err := cmd.Run()
176197
if err != nil {
177198
log.Println("Couldn't kill camtron")
178199
}
179200
} else if goos == "darwin" {
180-
log.Println("Got %s signal. Its MacOs so gotta kill Electron\n", sig)
201+
log.Printf("Got %s signal. Its MacOs so gotta kill Electron\n", sig)
181202
cmd := exec.Command("bash", "-c", " killall camtron")
182203
err := cmd.Run()
183204
if err != nil {

webcam_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package camtron
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestBinaryDownload(t *testing.T) {
11+
downloadBinary("camtron-linux-x64")
12+
assert.FileExists(t, "camtron-linux-x64.zip")
13+
}
14+
15+
func TestUnzip(t *testing.T) {
16+
UnzipBinary("camtron-linux-x64")
17+
assert.DirExists(t, "camtron-linux-x64")
18+
}
19+
20+
func TestCleanUp(t *testing.T) {
21+
_ = os.Remove("camtron-linux-x64.zip")
22+
_ = os.RemoveAll("camtron-linux-x64")
23+
}

0 commit comments

Comments
 (0)