Skip to content

Commit 9c52474

Browse files
committed
super refactoring
1 parent 792e5cf commit 9c52474

File tree

9 files changed

+169
-56
lines changed

9 files changed

+169
-56
lines changed

cmd/lib_install.go renamed to cmd/lib/lib_install.go

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* Copyright 2017 BCMI LABS SA (http://www.arduino.cc/)
2828
*/
2929

30-
package cmd
30+
package libCmd
3131

3232
import (
3333
"fmt"
@@ -68,26 +68,38 @@ func executeInstallCommand(cmd *cobra.Command, args []string) error {
6868

6969
status, err := libraries.CreateStatusContextFromIndex(index, nil, nil)
7070
if err != nil {
71-
fmt.Println("Cannot parse index file, it may be corrupted. downloading from downloads.arduino.cc")
71+
if GlobalFlags.Verbose > 0 {
72+
fmt.Println("Cannot parse index file, it may be corrupted. downloading from downloads.arduino.cc")
73+
}
7274

7375
err = libraries.DownloadLibrariesFile()
7476
if err != nil {
75-
fmt.Println("ERROR")
77+
if GlobalFlags.Verbose > 0 {
78+
fmt.Println("ERROR")
79+
}
7680
fmt.Println("Cannot download index file, please check your network connection.")
7781
return nil
7882
}
79-
fmt.Println("OK")
83+
if GlobalFlags.Verbose > 0 {
84+
fmt.Println("OK")
85+
}
8086

81-
fmt.Print("Parsing downloaded index file ... ")
87+
if GlobalFlags.Verbose > 0 {
88+
fmt.Print("Parsing downloaded index file ... ")
89+
}
8290

8391
//after download, I retry.
8492
status, err = libraries.CreateStatusContextFromIndex(index, nil, nil)
8593
if err != nil {
86-
fmt.Println("ERROR")
94+
if GlobalFlags.Verbose > 0 {
95+
fmt.Println("ERROR")
96+
}
8797
fmt.Println("Cannot parse downloaded index file")
8898
return nil
8999
}
90-
fmt.Println("OK")
100+
if GlobalFlags.Verbose > 0 {
101+
fmt.Println("OK")
102+
}
91103
}
92104

93105
libraryOK := make([]string, 0, len(args))
@@ -108,17 +120,29 @@ func executeInstallCommand(cmd *cobra.Command, args []string) error {
108120
}
109121
}
110122

123+
if GlobalFlags.Verbose > 0 {
124+
prettyPrint(libraryOK, libraryFails)
125+
} else {
126+
for _, library := range libraryOK {
127+
fmt.Printf("%s - Installed\n", library)
128+
}
129+
for library, failure := range libraryFails {
130+
fmt.Printf("%s - Error : %s\n", library, failure)
131+
}
132+
}
133+
134+
return nil
135+
}
136+
137+
func prettyPrintInstall(libraryOK []string, libraryFails map[string]string) {
111138
if len(libraryFails) > 0 {
112139
fmt.Println("The following libraries were succesfully installed:")
113140
fmt.Println(strings.Join(libraryOK, " "))
114141
fmt.Println("However, the installation process failed on the following libraries:")
115142
for library, failure := range libraryFails {
116143
fmt.Printf("%s - %s\n", library, failure)
117144
}
118-
119145
} else {
120146
fmt.Println("All libraries successfully installed")
121147
}
122-
123-
return nil
124148
}

cmd/lib_list.go renamed to cmd/lib/lib_list.go

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* Copyright 2017 BCMI LABS SA (http://www.arduino.cc/)
2828
*/
2929

30-
package cmd
30+
package libCmd
3131

3232
import (
3333
"fmt"
@@ -47,10 +47,6 @@ Can be used with -v (or --verbose) flag (up to 2 times) to have longer output.`,
4747
Run: executeListCommand,
4848
}
4949

50-
var libListCmdFlags struct {
51-
Verbose int
52-
}
53-
5450
func init() {
5551
LibRoot.AddCommand(LibListCmd)
5652
}
@@ -62,47 +58,93 @@ func executeListCommand(command *cobra.Command, args []string) {
6258

6359
//If it doesn't exist download it
6460
if _, err := os.Stat(libFile); os.IsNotExist(err) {
65-
fmt.Print("Index file does not exist. Downloading it from download.arduino.cc ...")
61+
if GlobalFlags.Verbose > 0 {
62+
fmt.Print("Index file does not exist. Downloading it from download.arduino.cc ...")
63+
}
6664
err := libraries.DownloadLibrariesFile()
6765
if err != nil {
68-
fmt.Println("ERROR")
66+
if GlobalFlags.Verbose > 0 {
67+
fmt.Println("ERROR")
68+
}
6969
fmt.Println("Cannot download index file.")
7070
return
7171
}
72-
fmt.Println("DONE")
72+
if GlobalFlags.Verbose > 0 {
73+
fmt.Println("OK")
74+
}
7375
}
7476

7577
//If it exists but it is corrupt replace it from arduino repository.
7678
index, err := libraries.LoadLibrariesIndex()
7779
if err != nil {
78-
fmt.Print("Index file is corrupt. Downloading a new copy from download.arduino.cc ...")
80+
if GlobalFlags.Verbose > 0 {
81+
fmt.Print("Index file is corrupt. Downloading a new copy from download.arduino.cc ...")
82+
}
7983
err := libraries.DownloadLibrariesFile()
8084
if err != nil {
81-
fmt.Println("ERROR")
85+
if GlobalFlags.Verbose > 0 {
86+
fmt.Println("ERROR")
87+
}
8288
fmt.Println("Cannot download index file.")
8389
return
8490
}
85-
fmt.Println("DONE")
91+
if GlobalFlags.Verbose > 0 {
92+
fmt.Println("OK")
93+
}
8694
index, err = libraries.LoadLibrariesIndex()
8795
if err != nil {
88-
fmt.Printf("Cannot parse index file : %s\n", libFile)
96+
fmt.Println("Cannot parse index file")
8997
return
9098
}
9199
}
92100

93101
//fmt.Printf("libFile = %s\n", libFile)
94102
//fmt.Printf("index = %v\n", index)
95103

96-
libraries, err := libraries.CreateStatusContextFromIndex(index, nil, nil)
104+
status, err := libraries.CreateStatusContextFromIndex(index, nil, nil)
97105
if err != nil {
98-
fmt.Printf("Could not synchronize library status: %s", err)
99-
return
106+
if GlobalFlags.Verbose > 0 {
107+
fmt.Println("Cannot parse index file, it may be corrupted. downloading from downloads.arduino.cc")
108+
}
109+
110+
err = libraries.DownloadLibrariesFile()
111+
if err != nil {
112+
if GlobalFlags.Verbose > 0 {
113+
fmt.Println("ERROR")
114+
}
115+
fmt.Println("Cannot download index file, please check your network connection.")
116+
return
117+
}
118+
if GlobalFlags.Verbose > 0 {
119+
fmt.Println("OK")
120+
}
121+
122+
if GlobalFlags.Verbose > 0 {
123+
fmt.Print("Parsing downloaded index file ... ")
124+
}
125+
126+
//after download, I retry.
127+
status, err = libraries.CreateStatusContextFromIndex(index, nil, nil)
128+
if err != nil {
129+
if GlobalFlags.Verbose > 0 {
130+
fmt.Println("ERROR")
131+
}
132+
fmt.Println("Cannot parse downloaded index file")
133+
return
134+
}
135+
if GlobalFlags.Verbose > 0 {
136+
fmt.Println("OK")
137+
}
100138
}
101139

140+
prettyPrintStatus(status)
141+
}
142+
143+
func prettyPrintStatus(status *libraries.StatusContext) {
102144
//Pretty print libraries from index.
103-
for _, name := range libraries.Names() {
145+
for _, name := range status.Names() {
104146
if GlobalFlags.Verbose > 0 {
105-
lib := libraries.Libraries[name]
147+
lib := status.Libraries[name]
106148
fmt.Print(lib)
107149
if GlobalFlags.Verbose > 1 {
108150
for _, r := range lib.Releases {

cmd/lib_root.go renamed to cmd/lib/lib_root.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@
2727
* Copyright 2017 BCMI LABS SA (http://www.arduino.cc/)
2828
*/
2929

30-
package cmd
30+
package libCmd
3131

3232
import (
33+
cliCmd "github.com/bcmi-labs/arduino-cli/cmd"
3334
"github.com/spf13/cobra"
3435
)
3536

@@ -41,5 +42,5 @@ var LibRoot = &cobra.Command{
4142
}
4243

4344
func init() {
44-
RootCmd.AddCommand(LibRoot)
45+
cliCmd.RootCmd.AddCommand(LibRoot)
4546
}

cmd/lib_search.go renamed to cmd/lib/lib_search.go

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14-
package cmd
14+
package libCmd
1515

1616
import (
1717
"errors"
@@ -50,20 +50,47 @@ func executeSearch(cmd *cobra.Command, args []string) error {
5050
return nil
5151
}
5252

53-
libraries, err := libraries.CreateStatusContextFromIndex(index, nil, nil)
53+
status, err := libraries.CreateStatusContextFromIndex(index, nil, nil)
5454
if err != nil {
55-
fmt.Printf("Could not synchronize library status: %s", err)
56-
return nil
55+
if GlobalFlags.Verbose > 0 {
56+
fmt.Println("Cannot parse index file, it may be corrupted. downloading from downloads.arduino.cc")
57+
}
58+
59+
err = libraries.DownloadLibrariesFile()
60+
if err != nil {
61+
if GlobalFlags.Verbose > 0 {
62+
fmt.Println("ERROR")
63+
}
64+
fmt.Println("Cannot download index file, please check your network connection.")
65+
return nil
66+
}
67+
if GlobalFlags.Verbose > 0 {
68+
fmt.Println("OK")
69+
fmt.Print("Parsing downloaded index file ... ")
70+
}
71+
72+
//after download, I retry.
73+
status, err = libraries.CreateStatusContextFromIndex(index, nil, nil)
74+
if err != nil {
75+
if GlobalFlags.Verbose > 0 {
76+
fmt.Println("ERROR")
77+
}
78+
fmt.Println("Cannot parse downloaded index file")
79+
return nil
80+
}
81+
if GlobalFlags.Verbose > 0 {
82+
fmt.Println("OK")
83+
}
5784
}
5885

5986
found := false
6087

6188
//Pretty print libraries from index.
62-
for _, name := range libraries.Names() {
89+
for _, name := range status.Names() {
6390
if strings.Contains(strings.ToLower(name), query) {
6491
found = true
6592
if GlobalFlags.Verbose > 0 {
66-
lib := libraries.Libraries[name]
93+
lib := status.Libraries[name]
6794
fmt.Print(lib)
6895
if GlobalFlags.Verbose > 1 {
6996
for _, r := range lib.Releases {

cmd/lib_uninstall.go renamed to cmd/lib/lib_uninstall.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* Copyright 2017 BCMI LABS SA (http://www.arduino.cc/)
2828
*/
2929

30-
package cmd
30+
package libCmd
3131

3232
import (
3333
"fmt"

cmd/lib_update.go renamed to cmd/lib/lib_update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14-
package cmd
14+
package libCmd
1515

1616
import (
1717
"fmt"

cmd/lib/lib_version.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package libCmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
const (
10+
// LibVersion represents the `arduino lib` package version number.
11+
LibVersion string = "0.0.1-pre-alpha"
12+
)
13+
14+
func init() {
15+
LibRoot.AddCommand(libVersionCmd)
16+
}
17+
18+
// LibVersionCmd represents the version command.
19+
var libVersionCmd = &cobra.Command{
20+
Use: "version",
21+
Short: "Shows version Number of arduino lib",
22+
Long: `Shows version Number of arduino lib which is installed on your system.`,
23+
Run: func(cmd *cobra.Command, args []string) {
24+
fmt.Printf("arduino lib V. %s\n", LibsVersion)
25+
},
26+
}

cmd/root.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ import (
3838
"github.com/spf13/viper"
3939
)
4040

41+
const (
42+
// ArduinoVersion represents Arduino CLI version number.
43+
ArduinoVersion string = "0.0.1-pre-alpha"
44+
)
45+
4146
// GlobalFlags represents flags available in all the program.
4247
var GlobalFlags struct {
4348
Verbose int

0 commit comments

Comments
 (0)