Skip to content

Commit

Permalink
readme almost done, added proper remove command handling for zip and …
Browse files Browse the repository at this point in the history
…git dependencies
  • Loading branch information
ksrichard committed Apr 22, 2021
1 parent 05e7372 commit 169bb47
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 24 deletions.
9 changes: 9 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
14 changes: 11 additions & 3 deletions arduino/arduino.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
}
76 changes: 55 additions & 21 deletions cmd/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,26 @@ import (
"github.com/ksrichard/apm/arduino"
"github.com/ksrichard/apm/project"
"github.com/ksrichard/apm/util"
"log"
"strings"

"github.com/spf13/cobra"
)

// 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)
Expand All @@ -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
}
Expand Down

0 comments on commit 169bb47

Please sign in to comment.