Skip to content

Commit 54b274f

Browse files
matteosuppofacchinm
authored andcommitted
Move utilities to a package to keep things clean
1 parent e9a0a91 commit 54b274f

File tree

6 files changed

+111
-150
lines changed

6 files changed

+111
-150
lines changed

conn.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"net/http"
1515

1616
log "github.com/Sirupsen/logrus"
17+
"github.com/arduino/arduino-create-agent/utilities"
1718
"github.com/gin-gonic/gin"
1819
"github.com/googollee/go-socket.io"
1920
)
@@ -95,7 +96,7 @@ func uploadHandler(c *gin.Context) {
9596

9697
buffer := bytes.NewBuffer(data.Hex)
9798

98-
path, err := saveFileonTempDir(data.Filename, buffer)
99+
path, err := utilities.SaveFileonTempDir(data.Filename, buffer)
99100
if err != nil {
100101
c.String(http.StatusBadRequest, err.Error())
101102
}

discovery.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@
2929
package main
3030

3131
import (
32-
log "github.com/Sirupsen/logrus"
33-
"github.com/oleksandr/bonjour"
3432
"net"
3533
"strings"
3634
"time"
35+
36+
log "github.com/Sirupsen/logrus"
37+
"github.com/oleksandr/bonjour"
3738
)
3839

3940
const timeoutConst = 2
@@ -143,3 +144,14 @@ func getPorts() ([]OsSerialPort, error) {
143144
return arrPorts, nil
144145
}
145146
}
147+
148+
// Filter returns a new slice containing all OsSerialPort in the slice that satisfy the predicate f.
149+
func Filter(vs []OsSerialPort, f func(OsSerialPort) bool) []OsSerialPort {
150+
var vsf []OsSerialPort
151+
for _, v := range vs {
152+
if f(v) {
153+
vsf = append(vsf, v)
154+
}
155+
}
156+
return vsf
157+
}

killbrowser_linux.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ package main
33
import (
44
"os/exec"
55
"strings"
6+
7+
"github.com/arduino/arduino-create-agent/utilities"
68
)
79

810
func findBrowser(process string) ([]byte, error) {
911
ps := exec.Command("ps", "-A", "-o", "command")
1012
grep := exec.Command("grep", process)
1113
head := exec.Command("head", "-n", "1")
1214

13-
return pipe_commands(ps, grep, head)
15+
return utilities.PipeCommands(ps, grep, head)
1416
}
1517

1618
func killBrowser(process string) ([]byte, error) {

seriallist_linux.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"path/filepath"
77
"strconv"
88
"strings"
9+
10+
"github.com/arduino/arduino-create-agent/utilities"
911
)
1012

1113
func associateVidPidWithPort(ports []OsSerialPort) []OsSerialPort {
@@ -14,7 +16,7 @@ func associateVidPidWithPort(ports []OsSerialPort) []OsSerialPort {
1416
ueventcmd := exec.Command("cat", "/sys/class/tty/"+filepath.Base(ports[index].Name)+"/device/uevent")
1517
grep3cmd := exec.Command("grep", "PRODUCT=")
1618

17-
cmdOutput2, _ := pipe_commands(ueventcmd, grep3cmd)
19+
cmdOutput2, _ := utilities.PipeCommands(ueventcmd, grep3cmd)
1820
cmdOutput2S := string(cmdOutput2)
1921

2022
if len(cmdOutput2S) == 0 {

utilities.go

-145
This file was deleted.

utilities/utilities.go

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package utilities
2+
3+
import (
4+
"bytes"
5+
"errors"
6+
"io"
7+
"io/ioutil"
8+
"os"
9+
"os/exec"
10+
"path/filepath"
11+
)
12+
13+
// SaveFileonTempDir creates a temp directory and saves the file data as the
14+
// filename in that directory.
15+
// Returns an error if the agent doesn't have permission to create the folder.
16+
// Returns an error if the filename doesn't form a valid path.
17+
//
18+
// Note that path could be defined and still there could be an error.
19+
func SaveFileonTempDir(filename string, data io.Reader) (path string, err error) {
20+
// Create Temp Directory
21+
tmpdir, err := ioutil.TempDir("", "arduino-create-agent")
22+
if err != nil {
23+
return "", errors.New("Could not create temp directory to store downloaded file. Do you have permissions?")
24+
}
25+
26+
// Determine filename
27+
filename, err = filepath.Abs(tmpdir + "/" + filename)
28+
if err != nil {
29+
return "", err
30+
}
31+
32+
// Touch file
33+
output, err := os.Create(filename)
34+
if err != nil {
35+
return filename, err
36+
}
37+
defer output.Close()
38+
39+
// Write file
40+
_, err = io.Copy(output, data)
41+
if err != nil {
42+
return filename, err
43+
}
44+
return filename, nil
45+
}
46+
47+
// PipeCommands executes the commands received as input by feeding the output of
48+
// one to the input of the other, exactly like Unix Pipe (|).
49+
// Returns the output of the final command and the eventual error.
50+
//
51+
// code inspired by https://gist.github.com/tyndyll/89fbb2c2273f83a074dc
52+
func PipeCommands(commands ...*exec.Cmd) ([]byte, error) {
53+
var errorBuffer, outputBuffer bytes.Buffer
54+
pipeStack := make([]*io.PipeWriter, len(commands)-1)
55+
i := 0
56+
for ; i < len(commands)-1; i++ {
57+
stdinPipe, stdoutPipe := io.Pipe()
58+
commands[i].Stdout = stdoutPipe
59+
commands[i].Stderr = &errorBuffer
60+
commands[i+1].Stdin = stdinPipe
61+
pipeStack[i] = stdoutPipe
62+
}
63+
commands[i].Stdout = &outputBuffer
64+
commands[i].Stderr = &errorBuffer
65+
66+
if err := call(commands, pipeStack); err != nil {
67+
return nil, err
68+
}
69+
70+
return outputBuffer.Bytes(), nil
71+
}
72+
73+
func call(stack []*exec.Cmd, pipes []*io.PipeWriter) (err error) {
74+
if stack[0].Process == nil {
75+
if err = stack[0].Start(); err != nil {
76+
return err
77+
}
78+
}
79+
if len(stack) > 1 {
80+
if err = stack[1].Start(); err != nil {
81+
return err
82+
}
83+
defer func() {
84+
pipes[0].Close()
85+
err = call(stack[1:], pipes[1:])
86+
}()
87+
}
88+
return stack[0].Wait()
89+
}

0 commit comments

Comments
 (0)