Skip to content

Commit affad98

Browse files
committed
formatting ongoing, completed on install and download commands
1 parent bdbd8ad commit affad98

File tree

4 files changed

+66
-77
lines changed

4 files changed

+66
-77
lines changed

cmd/arduino.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535

3636
"github.com/bcmi-labs/arduino-cli/cmd/formatter"
3737
homedir "github.com/mitchellh/go-homedir"
38-
"github.com/sirupsen/logrus"
3938
"github.com/spf13/cobra"
4039
"github.com/spf13/viper"
4140
)
@@ -119,7 +118,6 @@ func init() {
119118
}
120119

121120
func arduinoPreRun(cmd *cobra.Command, args []string) {
122-
fmt.Println(GlobalFlags)
123121
if !formatter.IsSupported(GlobalFlags.Format) {
124122
GlobalFlags.Format = "text"
125123
}
@@ -143,11 +141,11 @@ func initConfig() {
143141
// Find home directory.
144142
home, err := homedir.Dir()
145143
if err != nil {
146-
logrus.Warn("Error while searching for home directory for this user")
144+
base := "Error while searching for home directory for this user"
147145
if GlobalFlags.Verbose > 0 {
148-
logrus.Warnf(": %s\n", err.Error())
146+
base += fmt.Sprintf(": %s\n", err)
149147
}
150-
logrus.Warnln()
148+
formatter.Print(base)
151149
os.Exit(1)
152150
}
153151

@@ -160,6 +158,6 @@ func initConfig() {
160158

161159
// If a config file is found, read it in.
162160
if err := viper.ReadInConfig(); err == nil {
163-
logrus.Infoln("Using config file:", viper.ConfigFileUsed())
161+
formatter.Print(fmt.Sprintln("Using config file:", viper.ConfigFileUsed()))
164162
}
165163
}

cmd/arduino_lib.go

Lines changed: 14 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import (
4040

4141
"github.com/bcmi-labs/arduino-cli/cmd/formatter"
4242
"github.com/bcmi-labs/arduino-cli/cmd/pretty_print"
43+
"github.com/bcmi-labs/arduino-cli/cmd/structs"
4344
"github.com/bcmi-labs/arduino-cli/common"
4445
"github.com/bcmi-labs/arduino-cli/libraries"
4546
"github.com/bcmi-labs/arduino-cli/task"
@@ -118,42 +119,6 @@ var arduinoLibVersionCmd = &cobra.Command{
118119
},
119120
}
120121

121-
//DownloadResult contains info about a completed Download process.
122-
type DownloadResult struct {
123-
Downloaded []string `json:"downloaded"`
124-
Errors map[string]string `json:"errors"`
125-
}
126-
127-
func (dr DownloadResult) String() string {
128-
ret := ""
129-
for _, name := range dr.Downloaded {
130-
ret += fmt.Sprintf("%s - Downloaded\n", name)
131-
}
132-
ret += "Errors:\n"
133-
for libName, err := range dr.Errors {
134-
ret += fmt.Sprintf("%s - %s\n", libName, err)
135-
}
136-
return ret
137-
}
138-
139-
//InstallResult contains info about a completed installation process.
140-
type InstallResult struct {
141-
Installed []string `json:"installed"`
142-
Errors map[string]string `json:"errors"`
143-
}
144-
145-
func (dr InstallResult) String() string {
146-
ret := ""
147-
for _, name := range dr.Installed {
148-
ret += fmt.Sprintf("%s - Installed\n", name)
149-
}
150-
ret += "Errors:\n"
151-
for libName, err := range dr.Errors {
152-
ret += fmt.Sprintf("%s - %s\n", libName, err)
153-
}
154-
return ret
155-
}
156-
157122
func init() {
158123
arduinoCmd.AddCommand(arduinoLibCmd)
159124
arduinoLibCmd.AddCommand(arduinoLibInstallCmd)
@@ -195,20 +160,12 @@ func executeDownloadCommand(cmd *cobra.Command, args []string) error {
195160

196161
items, failed := extractValidLibraries(args, status)
197162

198-
libraryOK, libraryFails := parallelLibDownloads(items, len(args), true)
163+
libraryResults := parallelLibDownloads(items, true)
199164
for _, fail := range failed {
200-
libraryFails[fail] = "Not Found"
165+
libraryResults[fail] = "Error : Not Found"
201166
}
202167

203-
if GlobalFlags.Verbose > 0 {
204-
prettyPrints.DownloadLib(libraryOK, libraryFails)
205-
} else {
206-
formatter.Print(DownloadResult{
207-
Downloaded: libraryOK,
208-
Errors: libraryFails,
209-
})
210-
211-
}
168+
formatter.Print(structs.LibResultsFromMap(libraryResults))
212169

213170
return nil
214171
}
@@ -230,10 +187,9 @@ func extractValidLibraries(args []string, status *libraries.StatusContext) ([]*l
230187
}
231188

232189
// parallelLibDownloads executes multiple libraries downloads in parallel
233-
func parallelLibDownloads(items []*libraries.Library, argC int, forced bool) ([]string, map[string]string) {
190+
func parallelLibDownloads(items []*libraries.Library, forced bool) map[string]string {
234191
itemC := len(items)
235-
libraryOK := make([]string, 0, itemC)
236-
libraryFails := make(map[string]string, argC-itemC)
192+
libraryResults := make(map[string]string, itemC)
237193

238194
tasks := make(map[string]task.Wrapper, len(items))
239195
progressBars := make([]*pb.ProgressBar, 0, len(items))
@@ -264,14 +220,14 @@ func parallelLibDownloads(items []*libraries.Library, argC int, forced bool) ([]
264220

265221
for libraryName, result := range results {
266222
if result.Error != nil {
267-
libraryFails[libraryName] = result.Error.Error()
223+
libraryResults[libraryName] = fmt.Sprintf("Error : %s", result.Error)
268224
} else {
269-
libraryOK = append(libraryOK, libraryName)
225+
libraryResults[libraryName] = "OK"
270226
}
271227
}
272228
}
273229

274-
return libraryOK, libraryFails
230+
return libraryResults
275231
}
276232

277233
func executeInstallCommand(cmd *cobra.Command, args []string) error {
@@ -297,35 +253,22 @@ func executeInstallCommand(cmd *cobra.Command, args []string) error {
297253
}
298254

299255
items, fails := extractValidLibraries(args, status)
300-
libraryOK := make([]string, 0, len(items))
301256

302-
_, libraryFails := parallelLibDownloads(items, len(args), false)
257+
libraryResults := parallelLibDownloads(items, false)
303258
for _, fail := range fails {
304-
libraryFails[fail] = "Not Found"
259+
libraryResults[fail] = "Not Found"
305260
}
306261

307262
for _, library := range items {
308263
err = libraries.InstallLib(library, library.Latest().Version)
309264
if err != nil {
310-
libraryFails[library.Name] = err.Error()
265+
libraryResults[library.Name] = fmt.Sprintf("Error : %s", err)
311266
} else {
312-
libraryOK = append(libraryOK, library.Name)
267+
libraryResults[library.Name] = "OK"
313268
}
314269
}
315270

316-
if GlobalFlags.Verbose > 0 {
317-
prettyPrints.InstallLib(libraryOK, libraryFails)
318-
} else {
319-
okEnd := len(libraryOK)
320-
for i, library := range libraryOK {
321-
logrus.Infof("%-10s - Installed (%d/%d)\n", library, i+1, okEnd)
322-
}
323-
i := okEnd
324-
for library, failure := range libraryFails {
325-
i++
326-
logrus.Infof("%-10s - Error : %-10s (%d/%d)\n", library, failure, i, len(libraryFails))
327-
}
328-
}
271+
formatter.Print(structs.LibResultsFromMap(libraryResults))
329272

330273
return nil
331274
}

cmd/structs/helpers.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package structs
2+
3+
// LibResultsFromMap returns a LibProcessResults struct from a specified map of results.
4+
func LibResultsFromMap(resMap map[string]string) LibProcessResults {
5+
results := LibProcessResults{
6+
Libraries: make([]libProcessResult, len(resMap)),
7+
}
8+
i := 0
9+
for libName, libResult := range resMap {
10+
results.Libraries[i] = libProcessResult{
11+
LibraryName: libName,
12+
Result: libResult,
13+
}
14+
i++
15+
}
16+
return results
17+
}

cmd/structs/structs.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package structs
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
//LibProcessResults represent the result of a process on libraries.
9+
type LibProcessResults struct {
10+
Libraries []libProcessResult `json:"libraries"`
11+
}
12+
13+
// String returns a string representation of the object.
14+
func (lpr LibProcessResults) String() string {
15+
ret := ""
16+
for _, lr := range lpr.Libraries {
17+
ret += fmt.Sprintln(lr)
18+
}
19+
return strings.TrimSpace(ret)
20+
}
21+
22+
//libProcessResult contains info about a completed process.
23+
type libProcessResult struct {
24+
LibraryName string `json:"libraryName"`
25+
Result string `json:"result"`
26+
}
27+
28+
// String returns a string representation of the object.
29+
func (lr libProcessResult) String() string {
30+
return strings.TrimSpace(fmt.Sprintf("%s - %s", lr.LibraryName, lr.Result))
31+
}

0 commit comments

Comments
 (0)