Skip to content

Commit fbfa695

Browse files
committed
Extracted function to exec fwuploader plugins
1 parent 944a110 commit fbfa695

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

plugin/plugin.go

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -89,25 +89,12 @@ func (uploader *FwUploader) GetFirmwareVersion(portAddress string, stdout, stder
8989
if portAddress != "" {
9090
args = append(args, "-p", portAddress)
9191
}
92-
proc, err := executils.NewProcessFromPath(nil, uploader.pluginPath, args...)
93-
if err != nil {
94-
return nil, err
95-
}
96-
buffer := &bytes.Buffer{}
97-
if stdout != nil {
98-
proc.RedirectStdoutTo(io.MultiWriter(buffer, stdout))
99-
} else {
100-
proc.RedirectStdoutTo(buffer)
101-
}
102-
if stderr != nil {
103-
proc.RedirectStderrTo(stderr)
104-
}
105-
pluginExecErr := proc.RunWithinContext(context.Background())
92+
execStdout, _, execErr := uploader.exec(stdout, stderr, args...)
10693

10794
res := &GetFirmwareVersionResult{}
10895
fwVersionPrefix := "FIRMWARE-VERSION: "
10996
fwErrorPrefix := "GET-VERSION-ERROR: "
110-
for _, line := range strings.Split(buffer.String(), "\n") {
97+
for _, line := range strings.Split(execStdout.String(), "\n") {
11198
if strings.HasPrefix(line, fwVersionPrefix) {
11299
version := strings.TrimPrefix(line, fwVersionPrefix)
113100
res.FirmwareVersion = semver.ParseRelaxed(version)
@@ -116,18 +103,44 @@ func (uploader *FwUploader) GetFirmwareVersion(portAddress string, stdout, stder
116103
res.Error = strings.TrimPrefix(line, fwErrorPrefix)
117104
}
118105
}
106+
119107
if res.Error != "" {
120-
if pluginExecErr != nil {
121-
err = fmt.Errorf("%s: %w", res.Error, pluginExecErr)
108+
if execErr != nil {
109+
execErr = fmt.Errorf("%s: %w", res.Error, execErr)
122110
} else {
123-
err = errors.New(res.Error)
111+
execErr = errors.New(res.Error)
124112
}
125113
}
126-
return res, err
114+
return res, execErr
127115
}
128116

129117
// GetFirmwareVersionResult contains the result of GetFirmwareVersion command
130118
type GetFirmwareVersionResult struct {
131119
FirmwareVersion *semver.RelaxedVersion
132120
Error string
133121
}
122+
123+
func (uploader *FwUploader) exec(stdout, stderr io.Writer, args ...string) (*bytes.Buffer, *bytes.Buffer, error) {
124+
stdoutBuffer := &bytes.Buffer{}
125+
stderrBuffer := &bytes.Buffer{}
126+
127+
proc, err := executils.NewProcessFromPath(nil, uploader.pluginPath, args...)
128+
if err != nil {
129+
return stdoutBuffer, stderrBuffer, err
130+
}
131+
132+
if stdout != nil {
133+
proc.RedirectStdoutTo(io.MultiWriter(stdoutBuffer, stdout))
134+
} else {
135+
proc.RedirectStdoutTo(stdoutBuffer)
136+
}
137+
138+
if stderr != nil {
139+
proc.RedirectStderrTo(io.MultiWriter(stderrBuffer, stderr))
140+
} else {
141+
proc.RedirectStderrTo(stderr)
142+
}
143+
144+
execErr := proc.RunWithinContext(context.Background())
145+
return stdoutBuffer, stderrBuffer, execErr
146+
}

0 commit comments

Comments
 (0)