Skip to content

Commit 792e5cf

Browse files
committed
added uninstall first implementation
1 parent 7f6c254 commit 792e5cf

File tree

4 files changed

+129
-3
lines changed

4 files changed

+129
-3
lines changed

cmd/install.go renamed to cmd/lib_install.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,26 @@ 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. Try run arduino lib list update to update the index.")
71+
fmt.Println("Cannot parse index file, it may be corrupted. downloading from downloads.arduino.cc")
72+
73+
err = libraries.DownloadLibrariesFile()
74+
if err != nil {
75+
fmt.Println("ERROR")
76+
fmt.Println("Cannot download index file, please check your network connection.")
77+
return nil
78+
}
79+
fmt.Println("OK")
80+
81+
fmt.Print("Parsing downloaded index file ... ")
82+
83+
//after download, I retry.
84+
status, err = libraries.CreateStatusContextFromIndex(index, nil, nil)
85+
if err != nil {
86+
fmt.Println("ERROR")
87+
fmt.Println("Cannot parse downloaded index file")
88+
return nil
89+
}
90+
fmt.Println("OK")
7291
}
7392

7493
libraryOK := make([]string, 0, len(args))

cmd/lib_uninstall.go

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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 cmd
31+
32+
import (
33+
"fmt"
34+
"os"
35+
"path/filepath"
36+
"strings"
37+
38+
"github.com/bcmi-labs/arduino-cli/common"
39+
"github.com/bcmi-labs/arduino-cli/libraries"
40+
"github.com/spf13/cobra"
41+
)
42+
43+
// uninstallCmd represents the uninstall command
44+
var uninstallCmd = &cobra.Command{
45+
Use: "uninstall",
46+
Short: "Uninstalls one or more libraries",
47+
Long: `Uninstalls one or more libraries`,
48+
RunE: executeUninstallCommand,
49+
}
50+
51+
func init() {
52+
LibRoot.AddCommand(uninstallCmd)
53+
}
54+
55+
func executeUninstallCommand(cmd *cobra.Command, args []string) error {
56+
if len(args) < 1 {
57+
return fmt.Errorf("No library specified for install command")
58+
}
59+
60+
libFolder, err := common.GetDefaultLibFolder()
61+
if err != nil {
62+
return nil
63+
}
64+
65+
dir, err := os.Open(libFolder)
66+
if err != nil {
67+
fmt.Println("Cannot open libraries folder")
68+
return nil
69+
}
70+
71+
libraryFails := make([]string, 0, len(args))
72+
libraryOK := make([]string, 0, len(args))
73+
74+
dirFiles, err := dir.Readdirnames(0)
75+
if err != nil {
76+
fmt.Println("Cannot read into libraries folder")
77+
return nil
78+
}
79+
80+
for _, fileName := range dirFiles {
81+
for _, library := range args {
82+
if strings.Contains(fileName, library) {
83+
//found
84+
//TODO : remove only one version
85+
err = libraries.Uninstall(filepath.Join(libFolder, fileName))
86+
if err != nil {
87+
libraryFails = append(libraryFails, library)
88+
} else {
89+
libraryOK = append(libraryOK, library)
90+
}
91+
}
92+
}
93+
}
94+
95+
if len(libraryFails) > 0 {
96+
fmt.Println("The following libraries were succesfully uninstalled:")
97+
fmt.Println(strings.Join(libraryOK, " "))
98+
fmt.Println("However, the uninstall process failed on the following libraries:")
99+
fmt.Println(strings.Join(libraryFails, " "))
100+
} else {
101+
fmt.Println("All libraries successfully uninstalled")
102+
}
103+
104+
return nil
105+
}

common/common_helper.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"path/filepath"
3939
)
4040

41+
// GetFolder gets a folder on a path, and creates it if not found.
4142
func GetFolder(folder string, messageName string) (string, error) {
4243
_, err := os.Stat(folder)
4344
if os.IsNotExist(err) {
@@ -53,7 +54,7 @@ func GetFolder(folder string, messageName string) (string, error) {
5354
return folder, nil
5455
}
5556

56-
//Unzip extracts a zip file to a specified destination path.
57+
// Unzip extracts a zip file to a specified destination path.
5758
func Unzip(archive *zip.Reader, destination string) error {
5859
for _, file := range archive.File {
5960
path := filepath.Join(destination, file.Name)

libraries/install_uninstall_helpers.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
)
3939

4040
var install func(*zip.Reader, string) error = common.Unzip
41+
var Uninstall func(string) error = common.TruncateDir
4142

4243
func downloadLatest(library *Library) ([]byte, error) {
4344
return common.DownloadPackage(library.Latest.URL)
@@ -50,7 +51,7 @@ func getDownloadCacheFolder(library *Library) (string, error) {
5051
return "", err
5152
}
5253

53-
stagingFolder := filepath.Join(libFolder, ".cache")
54+
stagingFolder := filepath.Join(libFolder, ".download-cache")
5455
return common.GetFolder(stagingFolder, "libraries cache")
5556
}
5657

0 commit comments

Comments
 (0)