|
| 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 | +} |
0 commit comments