Skip to content

Commit

Permalink
Uninstall feature
Browse files Browse the repository at this point in the history
  • Loading branch information
kzaitsev committed Mar 20, 2016
1 parent 73c43e8 commit ce8164f
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 19 deletions.
8 changes: 7 additions & 1 deletion grvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <ruby_version>",
Action: GetUninstall,
},
{
Name: "upgrade",
Aliases: []string{"ug"},
Expand Down
48 changes: 30 additions & 18 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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()
}

Expand Down
59 changes: 59 additions & 0 deletions uninstall.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit ce8164f

Please sign in to comment.