Skip to content

Commit 32cf84f

Browse files
move ExecCommand under arduino/utils
1 parent 7d50993 commit 32cf84f

File tree

8 files changed

+103
-124
lines changed

8 files changed

+103
-124
lines changed

Diff for: arduino/builder/utils/utils.go

+69
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package utils
22

33
import (
4+
"bytes"
5+
"io"
46
"os"
57
"strings"
68
"unicode"
79

10+
"github.com/arduino/arduino-cli/executils"
811
f "github.com/arduino/arduino-cli/internal/algorithms"
912
"github.com/arduino/go-paths-helper"
1013
"github.com/pkg/errors"
@@ -179,3 +182,69 @@ func FindFilesInFolder(dir *paths.Path, recurse bool, extensions ...string) (pat
179182
}
180183
return dir.ReadDir(fileFilter)
181184
}
185+
186+
const (
187+
Ignore = 0 // Redirect to null
188+
Show = 1 // Show on stdout/stderr as normal
189+
ShowIfVerbose = 2 // Show if verbose is set, Ignore otherwise
190+
Capture = 3 // Capture into buffer
191+
)
192+
193+
func printableArgument(arg string) string {
194+
if strings.ContainsAny(arg, "\"\\ \t") {
195+
arg = strings.ReplaceAll(arg, "\\", "\\\\")
196+
arg = strings.ReplaceAll(arg, "\"", "\\\"")
197+
return "\"" + arg + "\""
198+
} else {
199+
return arg
200+
}
201+
}
202+
203+
// Convert a command and argument slice back to a printable string.
204+
// This adds basic escaping which is sufficient for debug output, but
205+
// probably not for shell interpretation. This essentially reverses
206+
// ParseCommandLine.
207+
func PrintableCommand(parts []string) string {
208+
return strings.Join(f.Map(parts, printableArgument), " ")
209+
}
210+
211+
func ExecCommand(
212+
verbose bool,
213+
stdoutWriter, stderrWriter io.Writer,
214+
command *executils.Process, stdout int, stderr int,
215+
) ([]byte, []byte, []byte, error) {
216+
verboseInfoBuf := &bytes.Buffer{}
217+
if verbose {
218+
verboseInfoBuf.WriteString(PrintableCommand(command.GetArgs()))
219+
}
220+
221+
stdoutBuffer := &bytes.Buffer{}
222+
if stdout == Capture {
223+
command.RedirectStdoutTo(stdoutBuffer)
224+
} else if stdout == Show || (stdout == ShowIfVerbose && verbose) {
225+
if stdoutWriter != nil {
226+
command.RedirectStdoutTo(stdoutWriter)
227+
} else {
228+
command.RedirectStdoutTo(os.Stdout)
229+
}
230+
}
231+
232+
stderrBuffer := &bytes.Buffer{}
233+
if stderr == Capture {
234+
command.RedirectStderrTo(stderrBuffer)
235+
} else if stderr == Show || (stderr == ShowIfVerbose && verbose) {
236+
if stderrWriter != nil {
237+
command.RedirectStderrTo(stderrWriter)
238+
} else {
239+
command.RedirectStderrTo(os.Stderr)
240+
}
241+
}
242+
243+
err := command.Start()
244+
if err != nil {
245+
return verboseInfoBuf.Bytes(), nil, nil, errors.WithStack(err)
246+
}
247+
248+
err = command.Wait()
249+
return verboseInfoBuf.Bytes(), stdoutBuffer.Bytes(), stderrBuffer.Bytes(), errors.WithStack(err)
250+
}

Diff for: arduino/builder/utils/utils_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package utils_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/arduino/arduino-cli/arduino/builder/utils"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestPrintableCommand(t *testing.T) {
11+
parts := []string{
12+
"/path/to/dir with spaces/cmd",
13+
"arg1",
14+
"arg-\"with\"-quotes",
15+
"specialchar-`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/?-argument",
16+
"arg with spaces",
17+
"arg\twith\t\ttabs",
18+
"lastarg",
19+
}
20+
correct := "\"/path/to/dir with spaces/cmd\"" +
21+
" arg1 \"arg-\\\"with\\\"-quotes\"" +
22+
" \"specialchar-`~!@#$%^&*()-_=+[{]}\\\\|;:'\\\",<.>/?-argument\"" +
23+
" \"arg with spaces\" \"arg\twith\t\ttabs\"" +
24+
" lastarg"
25+
result := utils.PrintableCommand(parts)
26+
require.Equal(t, correct, result)
27+
}

Diff for: legacy/builder/builder_utils/utils.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,11 @@ import (
2727

2828
"github.com/arduino/arduino-cli/arduino/builder"
2929
"github.com/arduino/arduino-cli/arduino/builder/progress"
30-
bUtils "github.com/arduino/arduino-cli/arduino/builder/utils"
30+
"github.com/arduino/arduino-cli/arduino/builder/utils"
3131
"github.com/arduino/arduino-cli/arduino/globals"
3232
"github.com/arduino/arduino-cli/executils"
3333
"github.com/arduino/arduino-cli/i18n"
3434
"github.com/arduino/arduino-cli/legacy/builder/constants"
35-
"github.com/arduino/arduino-cli/legacy/builder/utils"
3635
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
3736
"github.com/arduino/go-paths-helper"
3837
"github.com/arduino/go-properties-orderedmap"
@@ -51,7 +50,7 @@ func DirContentIsOlderThan(dir *paths.Path, target *paths.Path, extensions ...st
5150
}
5251
targetModTime := targetStat.ModTime()
5352

54-
files, err := bUtils.FindFilesInFolder(dir, true, extensions...)
53+
files, err := utils.FindFilesInFolder(dir, true, extensions...)
5554
if err != nil {
5655
return false, err
5756
}
@@ -147,7 +146,7 @@ func compileFiles(
147146
validExtensions = append(validExtensions, ext)
148147
}
149148

150-
sources, err := bUtils.FindFilesInFolder(sourceDir, recurse, validExtensions...)
149+
sources, err := utils.FindFilesInFolder(sourceDir, recurse, validExtensions...)
151150
if err != nil {
152151
return nil, err
153152
}
@@ -268,7 +267,7 @@ func compileFileWithRecipe(
268267
return nil, nil, nil, nil, errors.WithStack(err)
269268
}
270269

271-
objIsUpToDate, err := bUtils.ObjFileIsUpToDate(source, objectFile, depsFile)
270+
objIsUpToDate, err := utils.ObjFileIsUpToDate(source, objectFile, depsFile)
272271
if err != nil {
273272
return nil, nil, nil, nil, errors.WithStack(err)
274273
}

Diff for: legacy/builder/phases/linker.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ package phases
1818
import (
1919
"strings"
2020

21+
"github.com/arduino/arduino-cli/arduino/builder/utils"
2122
f "github.com/arduino/arduino-cli/internal/algorithms"
2223
"github.com/arduino/arduino-cli/legacy/builder/builder_utils"
2324
"github.com/arduino/arduino-cli/legacy/builder/constants"
2425
"github.com/arduino/arduino-cli/legacy/builder/types"
25-
"github.com/arduino/arduino-cli/legacy/builder/utils"
2626
"github.com/arduino/go-paths-helper"
2727
"github.com/arduino/go-properties-orderedmap"
2828
"github.com/pkg/errors"

Diff for: legacy/builder/phases/sizer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ import (
2121
"regexp"
2222
"strconv"
2323

24+
"github.com/arduino/arduino-cli/arduino/builder/utils"
2425
"github.com/arduino/arduino-cli/legacy/builder/builder_utils"
2526
"github.com/arduino/arduino-cli/legacy/builder/types"
26-
"github.com/arduino/arduino-cli/legacy/builder/utils"
2727
"github.com/arduino/go-properties-orderedmap"
2828
"github.com/pkg/errors"
2929
)

Diff for: legacy/builder/recipe_runner.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222

2323
"github.com/arduino/arduino-cli/legacy/builder/builder_utils"
2424
"github.com/arduino/arduino-cli/legacy/builder/types"
25-
"github.com/arduino/arduino-cli/legacy/builder/utils"
25+
"github.com/arduino/arduino-cli/arduino/builder/utils"
2626
properties "github.com/arduino/go-properties-orderedmap"
2727
"github.com/pkg/errors"
2828
"github.com/sirupsen/logrus"

Diff for: legacy/builder/test/utils_test.go

-42
This file was deleted.

Diff for: legacy/builder/utils/utils.go

-74
Original file line numberDiff line numberDiff line change
@@ -16,83 +16,9 @@
1616
package utils
1717

1818
import (
19-
"bytes"
20-
"io"
21-
"os"
22-
"strings"
23-
24-
"github.com/arduino/arduino-cli/executils"
25-
f "github.com/arduino/arduino-cli/internal/algorithms"
2619
"github.com/arduino/arduino-cli/legacy/builder/types"
27-
"github.com/pkg/errors"
2820
)
2921

30-
func printableArgument(arg string) string {
31-
if strings.ContainsAny(arg, "\"\\ \t") {
32-
arg = strings.Replace(arg, "\\", "\\\\", -1)
33-
arg = strings.Replace(arg, "\"", "\\\"", -1)
34-
return "\"" + arg + "\""
35-
} else {
36-
return arg
37-
}
38-
}
39-
40-
// Convert a command and argument slice back to a printable string.
41-
// This adds basic escaping which is sufficient for debug output, but
42-
// probably not for shell interpretation. This essentially reverses
43-
// ParseCommandLine.
44-
func PrintableCommand(parts []string) string {
45-
return strings.Join(f.Map(parts, printableArgument), " ")
46-
}
47-
48-
const (
49-
Ignore = 0 // Redirect to null
50-
Show = 1 // Show on stdout/stderr as normal
51-
ShowIfVerbose = 2 // Show if verbose is set, Ignore otherwise
52-
Capture = 3 // Capture into buffer
53-
)
54-
55-
func ExecCommand(
56-
verbose bool,
57-
stdoutWriter, stderrWriter io.Writer,
58-
command *executils.Process, stdout int, stderr int,
59-
) ([]byte, []byte, []byte, error) {
60-
verboseInfoBuf := &bytes.Buffer{}
61-
if verbose {
62-
verboseInfoBuf.WriteString(PrintableCommand(command.GetArgs()))
63-
}
64-
65-
stdoutBuffer := &bytes.Buffer{}
66-
if stdout == Capture {
67-
command.RedirectStdoutTo(stdoutBuffer)
68-
} else if stdout == Show || (stdout == ShowIfVerbose && verbose) {
69-
if stdoutWriter != nil {
70-
command.RedirectStdoutTo(stdoutWriter)
71-
} else {
72-
command.RedirectStdoutTo(os.Stdout)
73-
}
74-
}
75-
76-
stderrBuffer := &bytes.Buffer{}
77-
if stderr == Capture {
78-
command.RedirectStderrTo(stderrBuffer)
79-
} else if stderr == Show || (stderr == ShowIfVerbose && verbose) {
80-
if stderrWriter != nil {
81-
command.RedirectStderrTo(stderrWriter)
82-
} else {
83-
command.RedirectStderrTo(os.Stderr)
84-
}
85-
}
86-
87-
err := command.Start()
88-
if err != nil {
89-
return verboseInfoBuf.Bytes(), nil, nil, errors.WithStack(err)
90-
}
91-
92-
err = command.Wait()
93-
return verboseInfoBuf.Bytes(), stdoutBuffer.Bytes(), stderrBuffer.Bytes(), errors.WithStack(err)
94-
}
95-
9622
type loggerAction struct {
9723
onlyIfVerbose bool
9824
warn bool

0 commit comments

Comments
 (0)