@@ -21,6 +21,7 @@ package certificates
21
21
import (
22
22
"bytes"
23
23
"fmt"
24
+ "io"
24
25
"os"
25
26
"strings"
26
27
"time"
@@ -31,6 +32,7 @@ import (
31
32
"github.com/arduino/arduino-fwuploader/cli/globals"
32
33
"github.com/arduino/arduino-fwuploader/flasher"
33
34
"github.com/arduino/arduino-fwuploader/indexes/download"
35
+ "github.com/arduino/arduino-fwuploader/indexes/firmwareindex"
34
36
"github.com/arduino/go-paths-helper"
35
37
"github.com/sirupsen/logrus"
36
38
"github.com/spf13/cobra"
@@ -65,23 +67,37 @@ func runFlash(cmd *cobra.Command, args []string) {
65
67
// at the end cleanup the fwuploader temp dir
66
68
defer globals .FwUploaderPath .RemoveAll ()
67
69
68
- packageIndex , firmwareIndex := common .InitIndexes ()
69
70
common .CheckFlags (commonFlags .Fqbn , commonFlags .Address )
71
+ if len (certificateURLs ) == 0 && len (certificatePaths ) == 0 {
72
+ feedback .Fatal ("Error during certificates flashing: no certificates provided" , feedback .ErrBadArgument )
73
+ }
74
+
75
+ packageIndex , firmwareIndex := common .InitIndexes ()
70
76
board := common .GetBoard (firmwareIndex , commonFlags .Fqbn )
71
77
uploadToolDir := common .DownloadRequiredToolsForBoard (packageIndex , board )
72
78
73
- if len (certificateURLs ) == 0 && len (certificatePaths ) == 0 {
74
- feedback .Fatal ("Error during certificates flashing: no certificates provided" , feedback .ErrBadArgument )
79
+ var res * flasher.FlashResult
80
+ var err error
81
+ if ! board .IsPlugin () {
82
+ res , err = flashCertificates (board , uploadToolDir )
83
+ } else {
84
+ // TODO
75
85
}
76
86
87
+ feedback .PrintResult (res )
88
+ if err != nil {
89
+ os .Exit (int (feedback .ErrGeneric ))
90
+ }
91
+ }
92
+
93
+ func flashCertificates (board * firmwareindex.IndexBoard , uploadToolDir * paths.Path ) (* flasher.FlashResult , error ) {
77
94
loaderSketchPath , err := download .DownloadSketch (board .LoaderSketch )
78
95
if err != nil {
79
96
feedback .Fatal (fmt .Sprintf ("Error downloading loader sketch from %s: %s" , board .LoaderSketch .URL , err ), feedback .ErrGeneric )
80
97
}
81
98
logrus .Debugf ("loader sketch downloaded in %s" , loaderSketchPath .String ())
82
99
83
100
loaderSketch := strings .ReplaceAll (loaderSketchPath .String (), loaderSketchPath .Ext (), "" )
84
-
85
101
programmerOut , programmerErr , err := common .FlashSketch (board , loaderSketch , uploadToolDir , commonFlags .Address )
86
102
if err != nil {
87
103
feedback .FatalError (err , feedback .ErrGeneric )
@@ -109,38 +125,33 @@ func runFlash(cmd *cobra.Command, args []string) {
109
125
err = fmt .Errorf ("unknown module: %s" , moduleName )
110
126
}
111
127
if err != nil {
112
-
113
128
feedback .Fatal (fmt .Sprintf ("Error during certificates flashing: %s" , err ), feedback .ErrGeneric )
114
129
}
115
130
defer f .Close ()
116
131
117
132
// now flash the certificate
118
133
certFileList := paths .NewPathList (certificatePaths ... )
134
+ flasherOut := new (bytes.Buffer )
135
+ flasherErr := new (bytes.Buffer )
119
136
if feedback .GetFormat () == feedback .JSON {
120
- flasherOut := new (bytes.Buffer )
121
- flasherErr := new (bytes.Buffer )
122
137
err = f .FlashCertificates (& certFileList , certificateURLs , flasherOut )
123
138
if err != nil {
124
139
flasherErr .Write ([]byte (fmt .Sprintf ("Error during certificates flashing: %s" , err )))
125
140
}
126
- // Print the results
127
- feedback .PrintResult (& flasher.FlashResult {
128
- Programmer : (& flasher.ExecOutput {
129
- Stdout : programmerOut .String (),
130
- Stderr : programmerErr .String (),
131
- }),
132
- Flasher : (& flasher.ExecOutput {
133
- Stdout : flasherOut .String (),
134
- Stderr : flasherErr .String (),
135
- }),
136
- })
137
- if err != nil {
138
- os .Exit (int (feedback .ErrGeneric ))
139
- }
140
141
} else {
141
- err = f .FlashCertificates (& certFileList , certificateURLs , os .Stdout )
142
+ err = f .FlashCertificates (& certFileList , certificateURLs , io . MultiWriter ( flasherOut , os .Stdout ) )
142
143
if err != nil {
143
- feedback . Fatal ( fmt .Sprintf ("Error during certificates flashing: %s" , err ), feedback . ErrGeneric )
144
+ os . Stderr . Write ([] byte ( fmt .Sprintf ("Error during certificates flashing: %s" , err )) )
144
145
}
145
146
}
147
+ return & flasher.FlashResult {
148
+ Programmer : & flasher.ExecOutput {
149
+ Stdout : programmerOut .String (),
150
+ Stderr : programmerErr .String (),
151
+ },
152
+ Flasher : & flasher.ExecOutput {
153
+ Stdout : flasherOut .String (),
154
+ Stderr : flasherErr .String (),
155
+ },
156
+ }, err
146
157
}
0 commit comments