diff --git a/grvm.go b/grvm.go index cd5145d..f591100 100644 --- a/grvm.go +++ b/grvm.go @@ -84,9 +84,15 @@ func main() { { Name: "install", Aliases: []string{"i"}, - Usage: "Instqalls ruby", + Usage: "Installs ruby", Action: GetInstall, }, + { + Name: "uninstall", + Aliases: []string{"ui"}, + Usage: "grvm uninstall ", + Action: GetUninstall, + }, { Name: "upgrade", Aliases: []string{"ug"}, diff --git a/helpers.go b/helpers.go index 8a11a8a..7906910 100644 --- a/helpers.go +++ b/helpers.go @@ -63,24 +63,7 @@ func rebuildPaths() string { return strings.Join(newPaths, ":") } -func updateAvailableRubies() error { - db, err := getDB() - if err != nil { - return err - } - defer db.Close() - - tx, err := db.Begin(true) - if err != nil { - return err - } - defer tx.Rollback() - - b, err := getBucket(tx, []byte("rubies")) - if err != nil { - return err - } - +func updateRubiesBucket(b *bolt.Bucket) error { buffer := bytes.NewBuffer(make([]byte, 0)) cmd := exec.Command(rubyBuildExecutable, "--definitions") @@ -104,6 +87,35 @@ func updateAvailableRubies() error { } } + return nil +} + +func updateAvailableRubiesWithTx(tx *bolt.Tx) error { + b, err := getBucket(tx, []byte("rubies")) + if err != nil { + return err + } + + return updateRubiesBucket(b) +} + +func updateAvailableRubies() error { + db, err := getDB() + if err != nil { + return err + } + defer db.Close() + + tx, err := db.Begin(true) + if err != nil { + return err + } + defer tx.Rollback() + + if err := updateAvailableRubiesWithTx(tx); err != nil { + return err + } + return tx.Commit() } diff --git a/uninstall.go b/uninstall.go new file mode 100644 index 0000000..fbc4a48 --- /dev/null +++ b/uninstall.go @@ -0,0 +1,59 @@ +package main + +import ( + "fmt" + "os" + "os/exec" + + "github.com/codegangsta/cli" +) + +func GetUninstall(c *cli.Context) { + db, err := getDB() + if err != nil { + Print(c, "Cannot open database file:", dbPath) + os.Exit(1) + } + defer db.Close() + + tx, err := db.Begin(true) + if err != nil { + Print(c, err.Error()) + os.Exit(1) + } + defer tx.Rollback() + + candidate := c.Args().Get(0) + + if len(candidate) == 0 { + Print(c, "No version given") + os.Exit(1) + } + + if err := checkCandidate(tx, candidate); err != nil { + Print(c, err.Error()) + os.Exit(1) + } + + if err := exec.Command("rm", "-rf", fmt.Sprintf("%s/%s", rubiesDirectory, candidate)).Run(); err != nil { + Print(c, err.Error()) + os.Exit(1) + } + + if err := exec.Command("rm", "-rf", fmt.Sprintf("%s/%s", gemsDirectory, candidate)).Run(); err != nil { + Print(c, err.Error()) + os.Exit(1) + } + + if err := updateAvailableRubiesWithTx(tx); err != nil { + Print(c, err.Error()) + os.Exit(1) + } + + if err := tx.Commit(); err != nil { + Print(c, err.Error()) + os.Exit(1) + } + + os.Exit(0) +}