Skip to content

Rewrite of download #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Move utilities to a package to keep things clean
  • Loading branch information
matteosuppo committed May 4, 2016
commit d6e7189e02d6be409c91f88067a508a2c8d6c047
3 changes: 2 additions & 1 deletion conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"net/http"

log "github.com/Sirupsen/logrus"
"github.com/arduino/arduino-create-agent/utilities"
"github.com/gin-gonic/gin"
"github.com/googollee/go-socket.io"
)
Expand Down Expand Up @@ -95,7 +96,7 @@ func uploadHandler(c *gin.Context) {

buffer := bytes.NewBuffer(data.Hex)

path, err := saveFileonTempDir(data.Filename, buffer)
path, err := utilities.SaveFileonTempDir(data.Filename, buffer)
if err != nil {
c.String(http.StatusBadRequest, err.Error())
}
Expand Down
16 changes: 14 additions & 2 deletions discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@
package main

import (
log "github.com/Sirupsen/logrus"
"github.com/oleksandr/bonjour"
"net"
"strings"
"time"

log "github.com/Sirupsen/logrus"
"github.com/oleksandr/bonjour"
)

const timeoutConst = 2
Expand Down Expand Up @@ -143,3 +144,14 @@ func getPorts() ([]OsSerialPort, error) {
return arrPorts, nil
}
}

// Filter returns a new slice containing all OsSerialPort in the slice that satisfy the predicate f.
func Filter(vs []OsSerialPort, f func(OsSerialPort) bool) []OsSerialPort {
var vsf []OsSerialPort
for _, v := range vs {
if f(v) {
vsf = append(vsf, v)
}
}
return vsf
}
4 changes: 3 additions & 1 deletion killbrowser_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ package main
import (
"os/exec"
"strings"

"github.com/arduino/arduino-create-agent/utilities"
)

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

return pipe_commands(ps, grep, head)
return utilities.PipeCommands(ps, grep, head)
}

func killBrowser(process string) ([]byte, error) {
Expand Down
4 changes: 3 additions & 1 deletion seriallist_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"path/filepath"
"strconv"
"strings"

"github.com/arduino/arduino-create-agent/utilities"
)

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

cmdOutput2, _ := pipe_commands(ueventcmd, grep3cmd)
cmdOutput2, _ := utilities.PipeCommands(ueventcmd, grep3cmd)
cmdOutput2S := string(cmdOutput2)

if len(cmdOutput2S) == 0 {
Expand Down
145 changes: 0 additions & 145 deletions utilities.go

This file was deleted.

89 changes: 89 additions & 0 deletions utilities/utilities.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package utilities

import (
"bytes"
"errors"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
)

// SaveFileonTempDir creates a temp directory and saves the file data as the
// filename in that directory.
// Returns an error if the agent doesn't have permission to create the folder.
// Returns an error if the filename doesn't form a valid path.
//
// Note that path could be defined and still there could be an error.
func SaveFileonTempDir(filename string, data io.Reader) (path string, err error) {
// Create Temp Directory
tmpdir, err := ioutil.TempDir("", "arduino-create-agent")
if err != nil {
return "", errors.New("Could not create temp directory to store downloaded file. Do you have permissions?")
}

// Determine filename
filename, err = filepath.Abs(tmpdir + "/" + filename)
if err != nil {
return "", err
}

// Touch file
output, err := os.Create(filename)
if err != nil {
return filename, err
}
defer output.Close()

// Write file
_, err = io.Copy(output, data)
if err != nil {
return filename, err
}
return filename, nil
}

// PipeCommands executes the commands received as input by feeding the output of
// one to the input of the other, exactly like Unix Pipe (|).
// Returns the output of the final command and the eventual error.
//
// code inspired by https://gist.github.com/tyndyll/89fbb2c2273f83a074dc
func PipeCommands(commands ...*exec.Cmd) ([]byte, error) {
var errorBuffer, outputBuffer bytes.Buffer
pipeStack := make([]*io.PipeWriter, len(commands)-1)
i := 0
for ; i < len(commands)-1; i++ {
stdinPipe, stdoutPipe := io.Pipe()
commands[i].Stdout = stdoutPipe
commands[i].Stderr = &errorBuffer
commands[i+1].Stdin = stdinPipe
pipeStack[i] = stdoutPipe
}
commands[i].Stdout = &outputBuffer
commands[i].Stderr = &errorBuffer

if err := call(commands, pipeStack); err != nil {
return nil, err
}

return outputBuffer.Bytes(), nil
}

func call(stack []*exec.Cmd, pipes []*io.PipeWriter) (err error) {
if stack[0].Process == nil {
if err = stack[0].Start(); err != nil {
return err
}
}
if len(stack) > 1 {
if err = stack[1].Start(); err != nil {
return err
}
defer func() {
pipes[0].Close()
err = call(stack[1:], pipes[1:])
}()
}
return stack[0].Wait()
}