Skip to content

Commit 09e47b9

Browse files
committed
adding log for arduino core commands
1 parent eabd831 commit 09e47b9

File tree

3 files changed

+142
-28
lines changed

3 files changed

+142
-28
lines changed

cmd/arduino_config.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
package cmd
3131

3232
import (
33-
"fmt"
33+
"os"
34+
35+
"github.com/sirupsen/logrus"
3436

3537
"github.com/bcmi-labs/arduino-cli/cmd/formatter"
3638
"github.com/bcmi-labs/arduino-cli/configs"
@@ -42,6 +44,7 @@ var arduinoConfigCmd = &cobra.Command{
4244
Short: `Arduino Configuration Commands`,
4345
Long: `Arduino Configuration Commands`,
4446
Example: `arduino config init # Initializes a new config file into the default location`,
47+
Run: executeConfigCommand,
4548
}
4649

4750
var arduinoConfigInitCmd = &cobra.Command{
@@ -53,19 +56,43 @@ arduino config init --default # Creates a config file with default configuration
5356
Run: executeConfigInitCommand,
5457
}
5558

59+
func executeConfigCommand(cmd *cobra.Command, args []string) {
60+
logrus.Info("Executing `arduino config`")
61+
logrus.Warn("No subcommand specified for this command")
62+
formatter.PrintErrorMessage("No subcommand specified")
63+
if formatter.IsCurrentFormat("text") {
64+
logrus.Warn("Showing help message")
65+
cmd.Help()
66+
}
67+
logrus.Info("Done")
68+
}
69+
5670
func executeConfigInitCommand(cmd *cobra.Command, args []string) {
71+
logrus.Info("Executing `arduino config init`")
72+
5773
var conf configs.Configs
58-
if !arduinoConfigInitFlags.Default && formatter.IsCurrentFormat("text") {
74+
75+
if !arduinoConfigInitFlags.Default {
76+
if !formatter.IsCurrentFormat("text") {
77+
logrus.Error("The interactive mode is supported only in text mode")
78+
formatter.PrintErrorMessage("The interactive mode is supported only in text mode")
79+
os.Exit(errBadCall)
80+
}
81+
logrus.Info("Asking questions to the user to populate configuration")
5982
conf = ConfigsFromQuestions()
6083
} else {
84+
logrus.Info("Setting default configuration")
6185
conf = configs.Default()
6286
}
6387
err := conf.Serialize(arduinoConfigInitFlags.Location)
6488
if err != nil {
65-
formatter.PrintErrorMessage(fmt.Sprint("Config file creation error: ", err))
89+
logrus.WithError(err).Error("Cannot create config file")
90+
formatter.PrintErrorMessage("Cannot create config file")
91+
os.Exit(errGeneric)
6692
} else {
6793
formatter.PrintResult("Config file PATH: " + arduinoConfigInitFlags.Location)
6894
}
95+
logrus.Info("Done")
6996
}
7097

7198
// ConfigsFromQuestions asks some questions to the user to properly initialize configs.

cmd/arduino_core.go

Lines changed: 112 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import (
3333
"os"
3434
"path/filepath"
3535

36+
"github.com/sirupsen/logrus"
37+
3638
"github.com/bcmi-labs/arduino-cli/cmd/formatter"
3739
"github.com/bcmi-labs/arduino-cli/cmd/output"
3840
"github.com/bcmi-labs/arduino-cli/cmd/pretty_print"
@@ -98,30 +100,38 @@ func init() {
98100
}
99101

100102
func executeCoreCommand(cmd *cobra.Command, args []string) {
103+
logrus.Info("Executing `arduino core`")
101104
if arduinoCoreFlags.updateIndex {
105+
logrus.Info("Updating package index")
102106
common.ExecUpdateIndex(prettyPrints.DownloadCoreFileIndex())
103107
} else {
108+
logrus.Warn("No subcommand specified, showing help message")
104109
cmd.Help()
105110
os.Exit(errBadCall)
106111
}
112+
logrus.Info("Done")
107113
}
108114

109115
func executeCoreListCommand(cmd *cobra.Command, args []string) {
116+
logrus.Info("Executing `arduino core list`")
110117
pkgHome, err := common.GetDefaultPkgFolder()
111118
if err != nil {
119+
logrus.WithError(err).Error("Cannot get packages folder")
112120
formatter.PrintError(err)
113121
os.Exit(errCoreConfig)
114122
}
115123

116124
dir, err := os.Open(pkgHome)
117125
if err != nil {
126+
logrus.WithError(err).Error("Cannot open packages folder")
118127
formatter.PrintErrorMessage("Cannot open packages folder")
119128
os.Exit(errCoreConfig)
120129
}
121130
defer dir.Close()
122131

123132
dirFiles, err := dir.Readdir(0)
124133
if err != nil {
134+
logrus.WithError(err).Error("Cannot read into packages folder")
125135
formatter.PrintErrorMessage("Cannot read into packages folder")
126136
os.Exit(errCoreConfig)
127137
}
@@ -130,6 +140,7 @@ func executeCoreListCommand(cmd *cobra.Command, args []string) {
130140
InstalledPackages: make([]output.InstalledPackage, 0, 10),
131141
}
132142

143+
logrus.Info("Listing")
133144
for _, file := range dirFiles {
134145
if !file.IsDir() {
135146
continue
@@ -140,25 +151,35 @@ func executeCoreListCommand(cmd *cobra.Command, args []string) {
140151
InstalledCores: make([]output.InstalledStuff, 0, 5),
141152
InstalledTools: make([]output.InstalledStuff, 0, 5),
142153
}
154+
logrus.Infof("Getting installed cores of package: `%s`", packageName)
143155
getInstalledCores(packageName, &pkg.InstalledCores)
156+
logrus.Infof("Getting installed tools of package: `%s`", packageName)
144157
getInstalledTools(packageName, &pkg.InstalledTools)
158+
logrus.Infof("Adding package of dir: `%s` to the list", file)
145159
pkgs.InstalledPackages = append(pkgs.InstalledPackages, pkg)
146160
}
147161

148162
formatter.Print(pkgs)
163+
logrus.Info("Done")
149164
}
150165

151166
func executeCoreDownloadCommand(cmd *cobra.Command, args []string) {
167+
logrus.Info("Executing `arduino core download`")
168+
152169
if len(args) < 1 {
170+
logrus.Error("No core specified for download command, exiting")
153171
formatter.PrintErrorMessage("No core specified for download command")
154172
os.Exit(errBadCall)
155173
}
156174

175+
logrus.Info("Getting packages status context")
157176
status, err := getPackagesStatusContext()
158177
if err != nil {
159-
os.Exit(errGeneric)
178+
logrus.WithError(err).Error("Cannot get packages status context")
179+
os.Exit(errCoreConfig)
160180
}
161181

182+
logrus.Info("Preparing download")
162183
IDTuples := cores.ParseArgs(args)
163184

164185
coresToDownload, toolsToDownload, failOutputs := status.Process(IDTuples)
@@ -171,83 +192,144 @@ func executeCoreDownloadCommand(cmd *cobra.Command, args []string) {
171192
downloads[i] = toolsToDownload[i].DownloadItem
172193
}
173194

195+
logrus.Info("Downloading tool dependencies of all cores requested")
174196
releases.ParallelDownload(downloads, true, "Downloaded", &outputResults.Tools, "tool")
175197
downloads = make([]releases.DownloadItem, len(coresToDownload))
176198
for i := range coresToDownload {
177199
downloads[i] = coresToDownload[i].DownloadItem
178200
}
201+
logrus.Info("Downloading cores")
179202
releases.ParallelDownload(downloads, true, "Downloaded", &outputResults.Cores, "core")
180203

181204
formatter.Print(outputResults)
205+
logrus.Info("Done")
182206
}
183207

184208
func executeCoreInstallCommand(cmd *cobra.Command, args []string) {
209+
logrus.Info("Executing `arduino core download`")
210+
185211
if len(args) < 1 {
212+
logrus.Error("No core specified for download command, exiting")
186213
formatter.PrintErrorMessage("No core specified for download command")
187214
os.Exit(errBadCall)
188215
}
189216

217+
logrus.Info("Getting packages status context")
190218
status, err := getPackagesStatusContext()
191219
if err != nil {
192-
formatter.PrintError(err)
220+
logrus.WithError(err).Error("Cannot get packages status context")
193221
os.Exit(errCoreConfig)
194222
}
195223

224+
logrus.Info("Preparing download")
196225
IDTuples := cores.ParseArgs(args)
226+
197227
coresToDownload, toolsToDownload, failOutputs := status.Process(IDTuples)
198228
failOutputsCount := len(failOutputs)
199229
outputResults := output.CoreProcessResults{
200230
Cores: failOutputs,
231+
Tools: make([]output.ProcessResult, 0, 10),
201232
}
202-
203233
downloads := make([]releases.DownloadItem, len(toolsToDownload))
204234
for i := range toolsToDownload {
205235
downloads[i] = toolsToDownload[i].DownloadItem
206236
}
207-
releases.ParallelDownload(downloads, false, "Installed", &outputResults.Tools, "tool")
208237

238+
logrus.Info("Downloading tool dependencies of all cores requested")
239+
releases.ParallelDownload(downloads, false, "Downloaded", &outputResults.Tools, "tool")
209240
downloads = make([]releases.DownloadItem, len(coresToDownload))
210241
for i := range coresToDownload {
211242
downloads[i] = coresToDownload[i].DownloadItem
212243
}
213-
releases.ParallelDownload(downloads, false, "Installed", &outputResults.Cores, "core")
244+
logrus.Info("Downloading cores")
245+
releases.ParallelDownload(downloads, false, "Downloaded", &outputResults.Cores, "core")
214246

247+
logrus.Info("Installing tool dependencies")
215248
for i, item := range toolsToDownload {
249+
logrus.WithField("Package", item.Package).
250+
WithField("Name", item.Name).
251+
WithField("Version", item.Release.VersionName()).
252+
Info("Installing tool")
253+
254+
toolRoot, err := common.GetDefaultToolsFolder(item.Package)
255+
if err != nil {
256+
logrus.WithError(err).Error("Cannot get tool install path, try again.")
257+
formatter.PrintErrorMessage("Cannot get tool install path, try again.")
258+
os.Exit(errCoreConfig)
259+
}
260+
possiblePath := filepath.Join(toolRoot, item.Name, item.Release.VersionName())
261+
216262
err = cores.InstallTool(item.Package, item.Name, item.Release)
217263
if err != nil {
218-
outputResults.Tools[i] = output.ProcessResult{
219-
ItemName: item.Name,
220-
Error: err.Error(),
264+
if os.IsExist(err) {
265+
logrus.WithError(err).Warnf("Cannot install tool `%s`, it is already installed", item.Name)
266+
outputResults.Tools[i] = output.ProcessResult{
267+
ItemName: item.Name,
268+
Status: "Already Installed",
269+
Path: possiblePath,
270+
}
271+
} else {
272+
logrus.WithError(err).Warnf("Cannot install tool `%s`", item.Name)
273+
outputResults.Tools[i] = output.ProcessResult{
274+
ItemName: item.Name,
275+
Status: "",
276+
Error: err.Error(),
277+
}
221278
}
222279
} else {
223-
toolRoot, err := common.GetDefaultToolsFolder(item.Package)
224-
if err != nil {
225-
formatter.PrintErrorMessage("Cannot get tool install path, try again.")
226-
os.Exit(errCoreConfig)
280+
logrus.Info("Adding installed tool to final result")
281+
outputResults.Tools[i] = output.ProcessResult{
282+
ItemName: item.Name,
283+
Status: "Installed",
284+
Path: possiblePath,
227285
}
228-
outputResults.Tools[i].Path = filepath.Join(toolRoot, item.Name, item.Release.VersionName())
229286
}
230287
}
231288

232289
for i, item := range coresToDownload {
290+
logrus.WithField("Package", item.Package).
291+
WithField("Name", item.Name).
292+
WithField("Version", item.Release.VersionName()).
293+
Info("Installing core")
294+
295+
coreRoot, err := common.GetDefaultCoresFolder(item.Package)
296+
if err != nil {
297+
logrus.WithError(err).Error("Cannot get core install path, try again.")
298+
formatter.PrintErrorMessage("Cannot get core install path, try again.")
299+
os.Exit(errCoreConfig)
300+
}
301+
possiblePath := filepath.Join(coreRoot, item.Name, item.Release.VersionName())
302+
233303
err = cores.Install(item.Package, item.Name, item.Release)
234304
if err != nil {
235-
outputResults.Cores[i+failOutputsCount] = output.ProcessResult{
236-
ItemName: item.Name,
237-
Status: "",
238-
Error: err.Error(),
305+
if os.IsExist(err) {
306+
logrus.WithError(err).Warnf("Cannot install core `%s`, it is already installed", item.Name)
307+
outputResults.Cores[i+failOutputsCount] = output.ProcessResult{
308+
ItemName: item.Name,
309+
Status: "Already Installed",
310+
Path: possiblePath,
311+
}
312+
} else {
313+
logrus.WithError(err).Warnf("Cannot install core `%s`", item.Name)
314+
outputResults.Cores[i+failOutputsCount] = output.ProcessResult{
315+
ItemName: item.Name,
316+
Status: "",
317+
Error: err.Error(),
318+
}
239319
}
240320
} else {
241-
coreRoot, err := common.GetDefaultCoresFolder(item.Package)
242-
if err != nil {
243-
formatter.PrintErrorMessage("Cannot get core install path, try again.")
244-
os.Exit(errCoreConfig)
321+
logrus.Info("Adding installed core to final result")
322+
323+
outputResults.Cores[i+failOutputsCount] = output.ProcessResult{
324+
ItemName: item.Name,
325+
Status: "Installed",
326+
Path: possiblePath,
245327
}
246-
outputResults.Cores[i+failOutputsCount].Path = filepath.Join(coreRoot, item.Name, item.Release.VersionName())
247328
}
248329
}
249330

250331
formatter.Print(outputResults)
332+
logrus.Info("Done")
251333
}
252334

253335
// getInstalledCores gets the installed cores and puts them in the output struct.
@@ -261,18 +343,21 @@ func getInstalledTools(packageName string, tools *[]output.InstalledStuff) {
261343
}
262344

263345
// getInstalledStuff is a generic procedure to get installed cores or tools and put them in an output struct.
264-
func getInstalledStuff(packageName string, stuff *[]output.InstalledStuff, startPathFunc func(string) (string, error)) {
265-
stuffHome, err := startPathFunc(packageName)
346+
func getInstalledStuff(packageName string, stuff *[]output.InstalledStuff, defaultFolderFunc func(string) (string, error)) {
347+
stuffHome, err := defaultFolderFunc(packageName)
266348
if err != nil {
349+
logrus.WithError(err).Warn("Cannot get default folder ")
267350
return
268351
}
269352
stuffHomeFolder, err := os.Open(stuffHome)
270353
if err != nil {
354+
logrus.WithError(err).Warn("Cannot open default folder")
271355
return
272356
}
273357
defer stuffHomeFolder.Close()
274358
stuffFolders, err := stuffHomeFolder.Readdir(0)
275359
if err != nil {
360+
logrus.WithError(err).Warn("Cannot read into default folder")
276361
return
277362
}
278363
for _, stuffFolderInfo := range stuffFolders {
@@ -282,13 +367,16 @@ func getInstalledStuff(packageName string, stuff *[]output.InstalledStuff, start
282367
stuffName := stuffFolderInfo.Name()
283368
stuffFolder, err := os.Open(filepath.Join(stuffHome, stuffName))
284369
if err != nil {
370+
logrus.WithError(err).Warn("Cannot open inner directory")
285371
continue
286372
}
287373
defer stuffFolder.Close()
288374
versions, err := stuffFolder.Readdirnames(0)
289375
if err != nil {
376+
logrus.WithError(err).Warn("Cannot read into inner directory")
290377
continue
291378
}
379+
logrus.WithField("Name", stuffName).Info("Item added")
292380
*stuff = append(*stuff, output.InstalledStuff{
293381
Name: stuffName,
294382
Versions: versions,

cmd/error_codes.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
const (
4-
errOk int = iota
54
errNoConfigFile int = iota
65
errBadCall int = iota
76
errGeneric int = iota

0 commit comments

Comments
 (0)