Skip to content

Commit eabd831

Browse files
committed
adding logrus to arduino sketch and arduino sketch sync
1 parent bb5fd96 commit eabd831

File tree

1 file changed

+58
-10
lines changed

1 file changed

+58
-10
lines changed

cmd/arduino_sketch.go

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ import (
4040
"strings"
4141
"time"
4242

43+
"github.com/sirupsen/logrus"
44+
4345
"github.com/bcmi-labs/arduino-cli/auth"
4446
"github.com/bcmi-labs/arduino-cli/create_client_helpers"
4547
"github.com/bgentry/go-netrc/netrc"
@@ -72,27 +74,37 @@ var arduinoSketchSyncCmd = &cobra.Command{
7274
}
7375

7476
func executeSketchCommand(cmd *cobra.Command, args []string) {
77+
logrus.Info("Executing `arduino sketch`")
78+
logrus.Error("No subcommand specified, showing help message")
7579
formatter.PrintErrorMessage("No subcommand specified")
7680
cmd.Help()
81+
logrus.Error("Bad Call Exit")
7782
os.Exit(errBadCall)
7883
}
7984

8085
func executeSketchSyncCommand(cmd *cobra.Command, args []string) {
86+
logrus.Info("Executing `arduino sketch sync`")
8187
if len(args) > 0 {
88+
logrus.Error("No arguments are accepted for this command")
8289
formatter.PrintErrorMessage("No arguments are accepted")
8390
os.Exit(errBadCall)
8491
}
8592

8693
sketchbook, err := common.GetDefaultArduinoHomeFolder()
8794
if err != nil {
95+
logrus.WithError(err).Error("Cannot get sketchbook folder")
8896
formatter.PrintErrorMessage("Cannot get sketchbook folder")
8997
os.Exit(errCoreConfig)
9098
}
9199

100+
isTextMode := formatter.IsCurrentFormat("text")
101+
102+
logrus.Info("Setting priority")
92103
priority := arduinoSketchSyncFlags.Priority
93104

94105
if priority == "ask-once" {
95-
if !formatter.IsCurrentFormat("text") {
106+
if !isTextMode {
107+
logrus.Error("ask mode for this command is only supported using text format")
96108
formatter.PrintErrorMessage("ask mode for this command is only supported using text format")
97109
os.Exit(errBadCall)
98110
}
@@ -109,44 +121,50 @@ func executeSketchSyncCommand(cmd *cobra.Command, args []string) {
109121
}
110122
}
111123

112-
//loader
113-
isTextMode := formatter.IsCurrentFormat("text")
124+
logrus.Infof("Priority set to %s", priority)
125+
126+
logrus.Info("Preparing")
114127

115128
var loader *spinner.Spinner
116129

117-
if isTextMode {
130+
if isTextMode && !GlobalFlags.Debug {
118131
loader = spinner.New(spinner.CharSets[27], 100*time.Millisecond)
119132
loader.Prefix = "Syncing Sketches... "
120133

121134
loader.Start()
122135
}
123136

124137
stopSpinner := func() {
125-
if isTextMode {
138+
if isTextMode && !GlobalFlags.Debug {
126139
loader.Stop()
127140
}
128141
}
129142

130143
startSpinner := func() {
131-
if isTextMode {
144+
if isTextMode && !GlobalFlags.Debug {
132145
loader.Start()
133146
}
134147
}
135148

149+
logrus.Info("Logging in")
136150
username, bearerToken, err := login()
137151
if err != nil {
138152
stopSpinner()
153+
logrus.WithError(err).Error("Cannot login")
139154
formatter.PrintError(err)
140155
os.Exit(errNetwork)
141156
}
142157

158+
logrus.Info("Finding local sketches")
143159
sketchMap := sketches.Find(sketchbook, "libraries") //exclude libraries folder
144160

161+
logrus.Info("Finding online sketches")
145162
client := createClient.New(nil)
146163
tok := "Bearer " + bearerToken
147164
resp, err := client.SearchSketches(context.Background(), createClient.SearchSketchesPath(), nil, &username, &tok)
148165
if err != nil {
149166
stopSpinner()
167+
logrus.WithError(err).Error("Cannot get create sketches, sync failed")
150168
formatter.PrintErrorMessage("Cannot get create sketches, sync failed")
151169
os.Exit(errNetwork)
152170
}
@@ -155,6 +173,7 @@ func executeSketchSyncCommand(cmd *cobra.Command, args []string) {
155173
onlineSketches, err := client.DecodeArduinoCreateSketches(resp)
156174
if err != nil {
157175
stopSpinner()
176+
logrus.WithError(err).Error("Cannot unmarshal response from create, sync failed")
158177
formatter.PrintErrorMessage("Cannot unmarshal response from create, sync failed")
159178
os.Exit(errGeneric)
160179
}
@@ -166,6 +185,7 @@ func executeSketchSyncCommand(cmd *cobra.Command, args []string) {
166185

167186
maxLength := len(sketchMap) + len(onlineSketchesMap)
168187

188+
logrus.Info("Syncing sketches")
169189
// create output result struct with empty arrays.
170190
result := output.SketchSyncResult{
171191
PushedSketches: make([]string, 0, maxLength),
@@ -175,18 +195,21 @@ func executeSketchSyncCommand(cmd *cobra.Command, args []string) {
175195
}
176196

177197
for _, item := range sketchMap {
178-
179198
itemOnline, hasConflict := onlineSketchesMap[item.Name]
180199
if hasConflict {
200+
logrus.Warnf("Conflict found for sketch `%s`", item.Name)
181201
item.ID = itemOnline.ID.String()
182202
//solve conflicts
183203
if priority == "ask-always" {
184204
stopSpinner()
185205

186-
if !formatter.IsCurrentFormat("text") {
206+
logrus.Warn("Asking user what to do")
207+
if !isTextMode {
208+
logrus.WithField("format", GlobalFlags.Format).Error("ask mode for this command is only supported using text format")
187209
formatter.PrintErrorMessage("ask mode for this command is only supported using text format")
188210
os.Exit(errBadCall)
189211
}
212+
190213
firstAsk := true
191214
for priority != "pull-remote" &&
192215
priority != "push-local" &&
@@ -198,48 +221,59 @@ func executeSketchSyncCommand(cmd *cobra.Command, args []string) {
198221
fmt.Scanln(&priority)
199222
firstAsk = false
200223
}
224+
logrus.Warnf("Decision has been taken: %s", priority)
201225

202226
startSpinner()
203227
}
204228
switch priority {
205229
case "push-local":
230+
logrus.Infof("Pushing local sketch `%s` as edit", item.Name)
206231
err := editSketch(*item, sketchbook, bearerToken)
207232
if err != nil {
233+
logrus.WithError(err).Warnf("Cannot push `%s`", item.Name)
208234
result.Errors = append(result.Errors, output.SketchSyncError{
209235
Sketch: item.Name,
210236
Error: err,
211237
})
212238
} else {
239+
logrus.Infof("`%s` pushed", item.Name)
213240
result.PushedSketches = append(result.PushedSketches, item.Name)
214241
}
215242
break
216243
case "pull-remote":
244+
logrus.Infof("Pulling remote sketch `%s`", item.Name)
217245
err := pullSketch(itemOnline, sketchbook, bearerToken)
218246
if err != nil {
247+
logrus.WithError(err).Warnf("Cannot pull `%s`", item.Name)
219248
result.Errors = append(result.Errors, output.SketchSyncError{
220249
Sketch: item.Name,
221250
Error: err,
222251
})
223252
} else {
253+
logrus.Infof("`%s` pulled", item.Name)
224254
result.PulledSketches = append(result.PulledSketches, item.Name)
225255
}
226256
break
227257
case "skip":
258+
logrus.Warnf("Skipping `%s`", item.Name)
228259
result.SkippedSketches = append(result.SkippedSketches, item.Name)
229260
break
230261
default:
262+
logrus.Warnf("Skipping by default `%s`", item.Name)
231263
priority = "skip"
232264
result.SkippedSketches = append(result.SkippedSketches, item.Name)
233265
}
234-
235266
} else { //only local, push
267+
logrus.Info("No conflict, pushing `%s` as new sketch", item.Name)
236268
err := pushSketch(*item, sketchbook, bearerToken)
237269
if err != nil {
270+
logrus.WithError(err).Warnf("Cannot push `%s`", item.Name)
238271
result.Errors = append(result.Errors, output.SketchSyncError{
239272
Sketch: item.Name,
240273
Error: err,
241274
})
242275
} else {
276+
logrus.Infof("`%s` pushed", item.Name)
243277
result.PushedSketches = append(result.PushedSketches, item.Name)
244278
}
245279
}
@@ -250,19 +284,23 @@ func executeSketchSyncCommand(cmd *cobra.Command, args []string) {
250284
continue
251285
}
252286
//only online, pull
287+
logrus.Infof("Pulling only online sketch `%s`", *item.Name)
253288
err := pullSketch(item, sketchbook, bearerToken)
254289
if err != nil {
290+
logrus.WithError(err).Warnf("Cannot pull `%s`", *item.Name)
255291
result.Errors = append(result.Errors, output.SketchSyncError{
256292
Sketch: *item.Name,
257293
Error: err,
258294
})
259295
} else {
296+
logrus.Infof("`%s` pulled", *item.Name)
260297
result.PulledSketches = append(result.PulledSketches, *item.Name)
261298
}
262299
}
263300

264301
stopSpinner()
265302
formatter.Print(result)
303+
logrus.Info("Done")
266304
}
267305

268306
func pushSketch(sketch sketches.Sketch, sketchbook string, bearerToken string) error {
@@ -411,23 +449,30 @@ func login() (string, string, error) {
411449
return "", "", err
412450
}
413451

452+
logrus.Info("Reading ~/.netrc file")
414453
netRCFile := filepath.Join(home, ".netrc")
415454
file, err := os.OpenFile(netRCFile, os.O_CREATE|os.O_RDONLY, 0666)
416455
if err != nil {
456+
logrus.WithError(err).Error("Cannot read ~/.netrc file")
417457
return "", "", err
418458
}
419459
NetRC, err := netrc.Parse(file)
420460
if err != nil {
461+
logrus.WithError(err).Error("Cannot parse ~/.netrc file")
421462
return "", "", err
422463
}
423464

465+
logrus.Info("Searching for user credentials into the ~/.netrc file")
424466
arduinoMachine := NetRC.FindMachine("arduino.cc")
425467
if arduinoMachine.Name != "arduino.cc" {
426-
return "", "", errors.New("Credentials not present, try login with arduino login first")
468+
logrus.WithError(err).Error("Credentials not found")
469+
return "", "", errors.New("Credentials not found, try typing `arduino login` to login")
427470
}
428471

472+
logrus.Info("Refreshing user session")
429473
newToken, err := authConf.Refresh(arduinoMachine.Password)
430474
if err != nil {
475+
logrus.WithError(err).Error("Session expired, try typing `arduino login` to login again")
431476
return "", "", err
432477
}
433478

@@ -444,10 +489,13 @@ func login() (string, string, error) {
444489
if err == nil { //serialize new info
445490
err := ioutil.WriteFile(netRCFile, content, 0666)
446491
if err != nil {
492+
logrus.WithError(err).Error("Cannot write new ~/.netrc file")
447493
formatter.Print(err.Error())
448494
}
449495
} else {
496+
logrus.WithError(err).Error("Cannot serialize ~/.netrc file")
450497
formatter.Print(err.Error())
451498
}
499+
logrus.Info("Login successful")
452500
return arduinoMachine.Login, token, nil
453501
}

0 commit comments

Comments
 (0)