Skip to content

Commit a9b8848

Browse files
authored
Merge pull request #18 from bcmi-labs/lib-features
Lib features
2 parents 707d3bb + 90dfdbb commit a9b8848

File tree

8 files changed

+129
-41
lines changed

8 files changed

+129
-41
lines changed

cmd/arduino_lib.go

+24-17
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,11 @@ func executeDownloadCommand(cmd *cobra.Command, args []string) error {
130130
libraryOK := make([]string, 0, len(args))
131131
libraryFails := make(map[string]string, len(args))
132132

133-
for _, libraryName := range args {
133+
for i, libraryName := range args {
134134
library := status.Libraries[libraryName]
135135
if library != nil {
136136
//found
137-
_, err = libraries.DownloadAndCache(library)
137+
_, err = libraries.DownloadAndCache(library, i+1, len(args))
138138
if err != nil {
139139
libraryFails[libraryName] = err.Error()
140140
} else {
@@ -149,10 +149,10 @@ func executeDownloadCommand(cmd *cobra.Command, args []string) error {
149149
prettyPrintDownload(libraryOK, libraryFails)
150150
} else {
151151
for _, library := range libraryOK {
152-
fmt.Printf("%s - Downloaded\n", library)
152+
fmt.Printf("%-10s -Downloaded\n", library)
153153
}
154154
for library, failure := range libraryFails {
155-
fmt.Printf("%s - Error : %s\n", library, failure)
155+
fmt.Printf("%-10s -Error : %s\n", library, failure)
156156
}
157157
}
158158

@@ -184,11 +184,11 @@ func executeInstallCommand(cmd *cobra.Command, args []string) error {
184184
libraryOK := make([]string, 0, len(args))
185185
libraryFails := make(map[string]string, len(args))
186186

187-
for _, libraryName := range args {
187+
for i, libraryName := range args {
188188
library := status.Libraries[libraryName]
189189
if library != nil {
190190
//found
191-
err = libraries.DownloadAndInstall(library)
191+
err = libraries.DownloadAndInstall(library, i+1, len(args))
192192
if err != nil {
193193
libraryFails[libraryName] = err.Error()
194194
} else {
@@ -202,11 +202,13 @@ func executeInstallCommand(cmd *cobra.Command, args []string) error {
202202
if GlobalFlags.Verbose > 0 {
203203
prettyPrintInstall(libraryOK, libraryFails)
204204
} else {
205-
for _, library := range libraryOK {
206-
fmt.Printf("%s - Installed\n", library)
205+
for i, library := range libraryOK {
206+
fmt.Printf("%-10s - Installed (%d/%d)\n", library, i+1, len(libraryOK))
207207
}
208+
i := 1
208209
for library, failure := range libraryFails {
209-
fmt.Printf("%s - Error : %s\n", library, failure)
210+
i++
211+
fmt.Printf("%-10s - Error : %-10s (%d/%d)\n", library, failure, i, len(libraryFails))
210212
}
211213
}
212214

@@ -298,15 +300,20 @@ func executeUninstallCommand(cmd *cobra.Command, args []string) error {
298300
}
299301
}
300302

301-
if len(libraryFails) > 0 {
302-
fmt.Println("The following libraries were succesfully uninstalled:")
303-
fmt.Println(strings.Join(libraryOK, " "))
304-
fmt.Println("However, the uninstall process failed on the following libraries:")
305-
fmt.Println(strings.Join(libraryFails, " "))
306-
} else {
307-
fmt.Println("All libraries successfully uninstalled")
303+
if GlobalFlags.Verbose > 0 {
304+
if len(libraryFails) > 0 {
305+
fmt.Println("The following libraries were succesfully uninstalled:")
306+
fmt.Println(strings.Join(libraryOK, " "))
307+
fmt.Println("However, the uninstall process failed on the following libraries:")
308+
fmt.Println(strings.Join(libraryFails, " "))
309+
} else {
310+
fmt.Println("All libraries successfully uninstalled")
311+
}
312+
} else if len(libraryFails) > 0 {
313+
for _, failed := range libraryFails {
314+
fmt.Printf("%-10s - Failed\n", failed)
315+
}
308316
}
309-
310317
return nil
311318
}
312319

cmd/arduino_lib_list.go

+18-7
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
package cmd
3131

3232
import (
33-
"encoding/json"
3433
"fmt"
3534
"io/ioutil"
3635
"path/filepath"
@@ -39,8 +38,8 @@ import (
3938
"os"
4039

4140
"github.com/bcmi-labs/arduino-cli/common"
42-
"github.com/bcmi-labs/arduino-cli/libraries"
4341
"github.com/spf13/cobra"
42+
"github.com/zieckey/goini"
4443
)
4544

4645
// arduinoLibListCmd represents the list libraries command.
@@ -116,20 +115,32 @@ func executeListCommand(command *cobra.Command, args []string) {
116115
continue
117116
}
118117

119-
var jsonContent libraries.IndexRelease
120-
err = json.Unmarshal(content, &jsonContent)
118+
ini := goini.New()
119+
err = ini.Parse(content, "\n", "=")
121120
if err != nil {
122-
//I use library name
121+
fmt.Println(err)
122+
}
123+
Name, ok := ini.Get("name")
124+
if !ok {
125+
fileName := file.Name()
126+
//replacing underscore in foldernames with spaces.
127+
fileName = strings.Replace(fileName, "_", " ", -1)
128+
fileName = strings.Replace(fileName, "-", " v. ", -1)
129+
//I use folder name
130+
libs = append(libs, fileName)
131+
continue
132+
}
133+
Version, ok := ini.Get("version")
134+
if !ok {
123135
fileName := file.Name()
124136
//replacing underscore in foldernames with spaces.
125137
fileName = strings.Replace(fileName, "_", " ", -1)
126138
fileName = strings.Replace(fileName, "-", " v. ", -1)
127139
//I use folder name
128140
libs = append(libs, fileName)
129141
continue
130-
} else {
131-
libs = append(libs, fmt.Sprintf("%s v. %s", jsonContent.Name, jsonContent.Version))
132142
}
143+
libs = append(libs, fmt.Sprintf("%-10s v. %s", Name, Version))
133144
}
134145
}
135146
}

cmd/pretty_print.go

+32-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,32 @@
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+
130
package cmd
231

332
import (
@@ -7,8 +36,8 @@ import (
736
"github.com/bcmi-labs/arduino-cli/libraries"
837
)
938

39+
// prettyPrintStatus pretty prints libraries from index status.
1040
func prettyPrintStatus(status *libraries.StatusContext) {
11-
//Pretty print libraries from index.
1241
for _, name := range status.Names() {
1342
if GlobalFlags.Verbose > 0 {
1443
lib := status.Libraries[name]
@@ -57,7 +86,7 @@ func prettyPrintInstall(libraryOK []string, libraryFails map[string]string) {
5786
}
5887
fmt.Println("he installation process failed on the following libraries:")
5988
for library, failure := range libraryFails {
60-
fmt.Printf("%s - %s\n", library, failure)
89+
fmt.Printf("%-10s -%s\n", library, failure)
6190
}
6291
} else {
6392
fmt.Println("All libraries successfully installed")
@@ -76,7 +105,7 @@ func prettyPrintDownload(libraryOK []string, libraryFails map[string]string) {
76105
}
77106
fmt.Println("he download of the following libraries failed:")
78107
for library, failure := range libraryFails {
79-
fmt.Printf("%s - %s\n", library, failure)
108+
fmt.Printf("%-10s -%s\n", library, failure)
80109
}
81110
} else {
82111
fmt.Println("All libraries successfully downloaded")

common/common_helper.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ import (
6565
"net/http"
6666
"os"
6767
"path/filepath"
68+
69+
"github.com/mitchellh/ioprogress"
6870
)
6971

7072
// GetFolder gets a folder on a path, and creates it if not found.
@@ -136,8 +138,8 @@ func TruncateDir(dir string) error {
136138
return nil
137139
}
138140

139-
// DownloadPackage downloads a package from arduino repository.
140-
func DownloadPackage(URL string) ([]byte, error) {
141+
// DownloadPackage downloads a package from arduino repository, applying a label for the progress bar.
142+
func DownloadPackage(URL string, downloadLabel string, progressFile int, totalFiles int) ([]byte, error) {
141143
client := http.DefaultClient
142144

143145
request, err := http.NewRequest("GET", URL, nil)
@@ -146,6 +148,7 @@ func DownloadPackage(URL string) ([]byte, error) {
146148
}
147149

148150
response, err := client.Do(request)
151+
149152
if err != nil {
150153
return nil, fmt.Errorf("Cannot fetch library. Response creation error")
151154
} else if response.StatusCode != 200 {
@@ -155,7 +158,15 @@ func DownloadPackage(URL string) ([]byte, error) {
155158
defer response.Body.Close()
156159

157160
// Download completed, now move the archive to temp location and unpack it.
158-
body, err := ioutil.ReadAll(response.Body)
161+
rd := &ioprogress.Reader{
162+
Reader: response.Body,
163+
Size: response.ContentLength,
164+
DrawFunc: ioprogress.DrawTerminalf(os.Stdout, func(progress int64, size int64) string {
165+
return fmt.Sprintf("%s ... %s -%.2f %% (%d/%d)", downloadLabel, ioprogress.DrawTextFormatBytes(progress, size), float64(progress)/float64(size)*100, progressFile, totalFiles)
166+
}),
167+
}
168+
169+
body, err := ioutil.ReadAll(rd)
159170
if err != nil {
160171
return nil, fmt.Errorf("Cannot read response body")
161172
}

debug

-9.49 MB
Binary file not shown.

libraries/download.go

+35-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +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+
130
package libraries
231

332
import (
433
"archive/zip"
34+
"fmt"
535
"io/ioutil"
636
"net/http"
737
"path/filepath"
@@ -14,8 +44,8 @@ const (
1444
)
1545

1646
// DownloadAndCache downloads a library without installing it
17-
func DownloadAndCache(library *Library) (*zip.Reader, error) {
18-
zipContent, err := downloadLatest(library)
47+
func DownloadAndCache(library *Library, progressFiles int, totalFiles int) (*zip.Reader, error) {
48+
zipContent, err := downloadLatest(library, progressFiles, totalFiles)
1949
if err != nil {
2050
return nil, err
2151
}
@@ -29,11 +59,11 @@ func DownloadAndCache(library *Library) (*zip.Reader, error) {
2959
}
3060

3161
// DownloadLatest downloads Latest version of a library.
32-
func downloadLatest(library *Library) ([]byte, error) {
33-
return common.DownloadPackage(library.Latest.URL)
62+
func downloadLatest(library *Library, progressFiles int, totalFiles int) ([]byte, error) {
63+
return common.DownloadPackage(library.Latest.URL, fmt.Sprintf("library %s", library.Name), progressFiles, totalFiles)
3464
}
3565

36-
//DownloadLibrariesFile downloads the lib file from arduino repository.
66+
// DownloadLibrariesFile downloads the lib file from arduino repository.
3767
func DownloadLibrariesFile() error {
3868
libFile, err := IndexPath()
3969
if err != nil {

libraries/index.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (indexLib *IndexRelease) ExtractRelease() *Release {
103103
}
104104

105105
// ExtractLibrary create a new Library with the information contained
106-
// in this index element
106+
// in this index element.
107107
func (indexLib *IndexRelease) ExtractLibrary() *Library {
108108
release := indexLib.ExtractRelease()
109109
return &Library{

libraries/install_uninstall.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ import (
4040
"github.com/bcmi-labs/arduino-cli/common"
4141
)
4242

43-
var install func(*zip.Reader, string) error = common.Unzip
43+
var install = common.Unzip
4444

4545
// Uninstall a library means remove its directory.
46-
var Uninstall func(libraryFolder string) error = os.RemoveAll
46+
var Uninstall = os.RemoveAll
4747

4848
// DownloadAndInstall downloads a library and installs it to its specified location.
49-
func DownloadAndInstall(library *Library) error {
49+
func DownloadAndInstall(library *Library, progressFiles int, totalFiles int) error {
5050
libFolder, err := common.GetDefaultLibFolder()
5151
if err != nil {
5252
return fmt.Errorf("Cannot get Lib destination directory")
@@ -59,12 +59,12 @@ func DownloadAndInstall(library *Library) error {
5959

6060
_, err = os.Stat(cacheFilePath)
6161
if os.IsNotExist(err) {
62-
zipArchive, err = DownloadAndCache(library)
62+
zipArchive, err = DownloadAndCache(library, progressFiles, totalFiles)
6363
if err != nil {
6464
return err
6565
}
6666
} else {
67-
fmt.Printf("%s library found in cache downloads ... using cached zip archive\n", library.Name)
67+
fmt.Printf("%-10s - Cached (%d/%d)\n", library.Name, progressFiles, totalFiles)
6868
content, err := ioutil.ReadFile(cacheFilePath)
6969
if err != nil {
7070
return err

0 commit comments

Comments
 (0)