Skip to content

Commit

Permalink
Implement prune
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosnils committed May 28, 2020
1 parent 9e69952 commit ea57285
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
17 changes: 15 additions & 2 deletions cmd/prune.go
@@ -1,6 +1,10 @@
package cmd

import (
"os"

"github.com/apex/log"
"github.com/marcosnils/bin/pkg/config"
"github.com/spf13/cobra"
)

Expand All @@ -21,8 +25,17 @@ func newPruneCmd() *pruneCmd {
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
//TODO implement prune
return nil
cfg := config.Get()

pathsToDel := []string{}
for _, b := range cfg.Bins {
if _, err := os.Stat(b.Path); os.IsNotExist(err) {
log.Infof("%s not found removing", b.Path)
pathsToDel = append(pathsToDel, b.Path)
}
}

return config.RemoveBinaries(pathsToDel)
},
}

Expand Down
22 changes: 22 additions & 0 deletions pkg/config/config.go
Expand Up @@ -59,6 +59,28 @@ func AddBinary(c *Binary) error {
return nil
}

// RemoveBinaries removes the specified paths
// from bin configuration. It doesn't care about the order
func RemoveBinaries(paths []string) error {
if len(paths) > 0 {
k := 0
for _, cb := range cfg.Bins {
for _, p := range paths {
if cb.Path != p {
cfg.Bins[k] = cb
k++
}
}
}

cfg.Bins = cfg.Bins[:k]

return write()
}

return nil
}

func write() error {
u, _ := user.Current()
f, err := os.OpenFile(filepath.Join(u.HomeDir, ".bin/config.json"), os.O_RDWR|os.O_CREATE, 0666)
Expand Down

0 comments on commit ea57285

Please sign in to comment.