From 169bb475a2a7acd92dd0124dc0051d35fa1410a0 Mon Sep 17 00:00:00 2001 From: ksrichard Date: Thu, 22 Apr 2021 12:46:54 +0200 Subject: [PATCH] readme almost done, added proper remove command handling for zip and git dependencies --- README.MD | 9 ++++++ arduino/arduino.go | 14 +++++++-- cmd/remove.go | 76 +++++++++++++++++++++++++++++++++------------- 3 files changed, 75 insertions(+), 24 deletions(-) diff --git a/README.MD b/README.MD index ede5056..4f2b384 100644 --- a/README.MD +++ b/README.MD @@ -29,7 +29,16 @@ Use "apm [command] --help" for more information about a command. ``` ### Showcase +TODO +### Installation +#### Install on Linux/MacOS +Run the following in your terminal to install the latest version of `apm` +```shell script +curl -s https://raw.githubusercontent.com/ksrichard/apm/master/install.sh | sh +``` +#### Install on Windows +Download the latest version of `apm` from https://github.com/ksrichard/apm/releases/latest for Windows (`apm_windows_amd64.exe`), rename it to apm.exe and put it on path. ### How it works Every `apm` based project must have a file called `apm.json` in the project root (it can be create by running `apm init`) diff --git a/arduino/arduino.go b/arduino/arduino.go index debb7c0..0e2d557 100644 --- a/arduino/arduino.go +++ b/arduino/arduino.go @@ -261,7 +261,15 @@ func (c *ArduinoCli) InstallDependencies(details *project.ProjectDetails) error return nil } -func (c *ArduinoCli) UninstallDependency(dep string) error { - log.Printf("Removing dependency '%s'...\n", dep) - return RunCmdInteractive(c.cmd, []string{"lib", "uninstall", dep}) +func (c *ArduinoCli) UninstallDependency(dep *project.ProjectDependency) error { + depName := "" + if dep.Library != "" { + depName = dep.Library + } + if depName == "" { + log.Println("Dependency is a Git repository/Zip file based library, skipping uninstall...") + return nil + } + log.Printf("Uninstalling dependency '%s'...\n", depName) + return RunCmdInteractive(c.cmd, []string{"lib", "uninstall", depName}) } diff --git a/cmd/remove.go b/cmd/remove.go index 76e7ddc..2de0928 100644 --- a/cmd/remove.go +++ b/cmd/remove.go @@ -21,6 +21,7 @@ import ( "github.com/ksrichard/apm/arduino" "github.com/ksrichard/apm/project" "github.com/ksrichard/apm/util" + "log" "strings" "github.com/spf13/cobra" @@ -28,10 +29,18 @@ import ( // removeCmd represents the remove command var removeCmd = &cobra.Command{ - Use: "remove", - Example: "apm remove\napm remove OneWire\napm remove onewire\napm remove \"robot control\"\napm remove \"Robot Control\"", - Short: "Remove library from the project", - Long: `Remove library from the Arduino project`, + Use: "remove", + Example: "apm remove\n" + + "apm remove OneWire\n" + + "apm remove onewire\n" + + "apm remove \"robot control\"\n" + + "apm remove \"Robot Control\"\n" + + "apm remove \"https://github.com/jandrassy/ArduinoOTA\"\n" + + "apm remove https://github.com/jandrassy/ArduinoOTA\n" + + "apm remove ArduinoOTA.zip\n" + + "apm remove \"ArduinoOTA.zip\"", + Short: "Remove library from the project", + Long: `Remove library from the Arduino project`, RunE: func(cmd *cobra.Command, args []string) error { // project details details, err := project.GetProjectDetails(cmd) @@ -47,46 +56,71 @@ var removeCmd = &cobra.Command{ } defer cli.Destroy() - libToRemove := "" + var libToRemove *project.ProjectDependency = nil // no library provided if len(args) < 1 { fmt.Println("No library provided...") items := make(map[string]interface{}) for _, dep := range details.Dependencies { - // TODO: continue - //if dep.Library == "" && dep.Git != "" { - // items[dep.Git] = dep.Git - //} - //if dep.Library == "" && dep.Zip != "" { - // items[dep.Zip] = dep.Zip - //} + libTitle := "" + if dep.Library == "" && dep.Git != "" { + libTitle = dep.Git + } + if dep.Library == "" && dep.Zip != "" { + libTitle = dep.Zip + } if dep.Library != "" && dep.Version != "" { - items[fmt.Sprintf("%s (%s)", dep.Library, dep.Version)] = dep.Library + libTitle = fmt.Sprintf("%s (%s)", dep.Library, dep.Version) } + items[libTitle] = &dep } selectedLib, err := util.Select("Select library to remove", []string{"Cancel"}, items) if err != nil { return err } - if selectedLib.(string) == "Cancel" { - return errors.New("cancelled") + + // check if cancelled + switch selectedLib.(type) { + case string: + if selectedLib.(string) == "Cancel" { + return errors.New("cancelled") + } + break } - libToRemove = selectedLib.(string) + + libToRemove = selectedLib.(*project.ProjectDependency) } else { // library name provided libToRemoveArg := args[0] for _, dep := range details.Dependencies { - if strings.ToLower(dep.Library) == libToRemoveArg { - libToRemove = dep.Library + if strings.ToLower(dep.Library) == strings.ToLower(libToRemoveArg) || + dep.Git == libToRemoveArg || + dep.Zip == libToRemoveArg { + libToRemove = &dep } } - if libToRemove == "" { - return errors.New(fmt.Sprintf("failed to find '%s' in the project", libToRemoveArg)) + if libToRemove == nil { + return errors.New(fmt.Sprintf("failed to find '%s' library in the project", libToRemoveArg)) } } + // log removal + libName := "" + if libToRemove.Library != "" { + libName = libToRemove.Library + } + if libToRemove.Git != "" { + libName = libToRemove.Git + } + if libToRemove.Zip != "" { + libName = libToRemove.Zip + } + log.Printf("Removing '%s'...", libName) + // remove from project file for i, dep := range details.Dependencies { - if dep.Library == libToRemove { + if (dep.Library != "" && dep.Library == libToRemove.Library) || + (dep.Git != "" && dep.Git == libToRemove.Git) || + (dep.Zip != "" && dep.Zip == libToRemove.Zip) { details.Dependencies = removeFromDeps(details.Dependencies, i) break }