Skip to content

Commit c623c7a

Browse files
committed
refactoring of lib list command and pretty prints,
starting to search for a strategy to refactor all prints using a logger
1 parent 658c498 commit c623c7a

File tree

6 files changed

+177
-26
lines changed

6 files changed

+177
-26
lines changed

cmd/arduino_core_list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,5 @@ func init() {
4646
}
4747

4848
func executeCoreListCommand(cmd *cobra.Command, args []string) {
49-
49+
5050
}

cmd/arduino_lib_list.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,27 +52,28 @@ Can be used with -v (or --verbose) flag (up to 2 times) to have longer output.`,
5252
Run: executeListCommand,
5353
}
5454

55-
// arduinoLibListUpdateCmd represents the lib list update command
56-
var arduinoLibListUpdateCmd = &cobra.Command{
57-
Use: "update",
58-
Short: "Updates the library index to latest version",
59-
Long: `Updates the library index to latest version from downloads.arduino.cc repository.`,
60-
Run: execUpdateListIndex,
55+
var arduinoLibListFlags struct {
56+
updateIndex bool
6157
}
6258

6359
func init() {
6460
arduinoLibCmd.AddCommand(arduinoLibListCmd)
65-
arduinoLibListCmd.AddCommand(arduinoLibListUpdateCmd)
61+
arduinoLibListCmd.Flags().BoolVarP(&arduinoLibListFlags.updateIndex, "update-index", "u", false, "Updates the libraries index")
6662
}
6763

6864
func executeListCommand(command *cobra.Command, args []string) {
65+
if arduinoLibListFlags.updateIndex {
66+
execUpdateListIndex(command, args)
67+
return
68+
}
69+
6970
libHome, err := common.GetDefaultLibFolder()
7071
if err != nil {
7172
fmt.Println("Cannot get libraries folder")
7273
return
7374
}
7475

75-
//prettyPrints.Status(status)
76+
//prettyPrints.LibStatus(status)
7677
dir, err := os.Open(libHome)
7778
if err != nil {
7879
fmt.Println("Cannot open libraries folder")

cmd/pretty_print/common.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* This file is part of arduino-cli.
3+
*
4+
* arduino-cli is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*
18+
* As a special exception, you may use this file as part of a free software
19+
* library without restriction. Specifically, if other files instantiate
20+
* templates or use macros or inline functions from this file, or you compile
21+
* this file and link it with other files to produce an executable, this
22+
* file does not by itself cause the resulting executable to be covered by
23+
* the GNU General Public License. This exception does not however
24+
* invalidate any other reasons why the executable file might be covered by
25+
* the GNU General Public License.
26+
*
27+
* Copyright 2017 BCMI LABS SA (http://www.arduino.cc/)
28+
*/
29+
30+
package prettyPrints
31+
32+
import (
33+
"math"
34+
35+
"github.com/sirupsen/logrus"
36+
)
37+
38+
var log *logrus.Entry
39+
40+
func init() {
41+
log = logrus.WithFields(logrus.Fields{})
42+
}
43+
44+
// A TaskWrapper wraps a task to be executed to allow
45+
// Useful messages to be print. It is used to pretty
46+
// print operations.
47+
//
48+
// All Message arrays use VERBOSITY as index.
49+
type TaskWrapper struct {
50+
beforeMessage []string
51+
task Task
52+
afterMessage []string
53+
errorMessage []string
54+
}
55+
56+
// verbosity represents the verbosity level of the message.
57+
//
58+
// Examples:
59+
//
60+
// verbosity 0 Message ""
61+
// verbosity 1 Message "Hi"
62+
// verbosity 2 Message "Hello folk, how are you?"
63+
// type verbosity int
64+
65+
// Task represents a function which can be safely wrapped into a TaskWrapper
66+
type Task func() error
67+
68+
// ExecuteTask executes a task while printing messages to describe what is happening.
69+
func (tw TaskWrapper) ExecuteTask(verb int) error {
70+
var maxUsableVerb int
71+
maxUsableVerb = minVerb(verb, tw.beforeMessage)
72+
log.Info(tw.beforeMessage[maxUsableVerb])
73+
err := tw.task()
74+
if err != nil {
75+
maxUsableVerb = minVerb(verb, tw.errorMessage)
76+
log.Warn(tw.errorMessage[maxUsableVerb])
77+
} else {
78+
maxUsableVerb = minVerb(verb, tw.afterMessage)
79+
log.Info(tw.afterMessage[maxUsableVerb])
80+
}
81+
return err
82+
}
83+
84+
// minVerb tells which is the max level of verbosity for the specified verbosity level (set by another
85+
// function call) and the provided array of strings.
86+
//
87+
// Refer to TaskRunner struct for the usage of the array.
88+
func minVerb(verb1 int, sentences []string) int {
89+
return int(math.Min(float64(verb1), float64(len(sentences)-1)))
90+
}

cmd/pretty_print/pretty_print_core.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* This file is part of arduino-cli.
3+
*
4+
* arduino-cli is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*
18+
* As a special exception, you may use this file as part of a free software
19+
* library without restriction. Specifically, if other files instantiate
20+
* templates or use macros or inline functions from this file, or you compile
21+
* this file and link it with other files to produce an executable, this
22+
* file does not by itself cause the resulting executable to be covered by
23+
* the GNU General Public License. This exception does not however
24+
* invalidate any other reasons why the executable file might be covered by
25+
* the GNU General Public License.
26+
*
27+
* Copyright 2017 BCMI LABS SA (http://www.arduino.cc/)
28+
*/
29+
30+
package prettyPrints
31+
32+
import "github.com/bcmi-labs/arduino-cli/cores"
33+
34+
// PackageStatus pretty prints packages from index status.
35+
func PackageStatus(status *cores.StatusContext) {
36+
37+
}

cmd/pretty_print/pretty_print_lib.go

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@ import (
3636
"github.com/bcmi-labs/arduino-cli/libraries"
3737
)
3838

39-
// Status pretty prints libraries from index status.
40-
func Status(status *libraries.StatusContext, verbosity int) {
39+
func prettyPrint() {
40+
41+
}
42+
43+
// LibStatus pretty prints libraries from index status.
44+
func LibStatus(status *libraries.StatusContext, verbosity int) {
4145
for _, name := range status.Names() {
4246
if verbosity > 0 {
4347
lib := status.Libraries[name]
@@ -56,24 +60,42 @@ func Status(status *libraries.StatusContext, verbosity int) {
5660

5761
// DownloadLibFileIndex shows info regarding the download of a missing (or corrupted) file index.
5862
func DownloadLibFileIndex(verbosity int) error {
59-
if verbosity > 0 {
60-
fmt.Print("Downloading from download.arduino.cc ... ")
61-
}
62-
63-
err := libraries.DownloadLibrariesFile()
64-
if err != nil {
63+
return TaskWrapper{
64+
beforeMessage: []string{
65+
"",
66+
"Downloading from download.arduino.cc ... ",
67+
},
68+
afterMessage: []string{
69+
"",
70+
"OK",
71+
},
72+
errorMessage: []string{
73+
"Cannot download file, check your network connection.",
74+
"ERROR\n" +
75+
"Cannot download file, check your network connection.",
76+
},
77+
task: libraries.DownloadLibrariesFile,
78+
}.ExecuteTask(verbosity)
79+
/*
6580
if verbosity > 0 {
66-
fmt.Println("ERROR")
81+
fmt.Print("Downloading from download.arduino.cc ... ")
6782
}
68-
fmt.Println("Cannot download file, check your network connection.")
69-
return err
70-
}
7183
72-
if verbosity > 0 {
73-
fmt.Println("OK")
74-
}
84+
err := libraries.DownloadLibrariesFile()
85+
if err != nil {
86+
if verbosity > 0 {
87+
fmt.Println("ERROR")
88+
}
89+
fmt.Println("Cannot download file, check your network connection.")
90+
return err
91+
}
92+
93+
if verbosity > 0 {
94+
fmt.Println("OK")
95+
}
7596
76-
return nil
97+
return nil
98+
*/
7799
}
78100

79101
// InstallLib pretty prints info about installarion of one or more libraries.
@@ -126,7 +148,7 @@ func CorruptedLibIndexFix(index *libraries.Index, verbosity int) (*libraries.Sta
126148
return nil, err
127149
}
128150

129-
return libIndexParse(index, verbosity)
151+
return libIndexParse(index, int(verbosity))
130152
}
131153

132154
// libIndexParse pretty prints info about parsing an index file of libraries.

cores/cores.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ func (core *Core) String(verbosity int) (res string) {
103103
} else {
104104
res = fmt.Sprintf("%s\n", core.Name)
105105
}
106+
return
106107
}
107108

108109
func (release *Release) String() string {

0 commit comments

Comments
 (0)