From 51c8c920a3c53ed8b3938b9aa44becad55876620 Mon Sep 17 00:00:00 2001 From: stephen mcquay Date: Tue, 6 Mar 2018 21:46:25 -0800 Subject: [PATCH] Adds pm rm --- cs.go | 22 ++++++++++++++++++++++ db/installed.go | 10 ++++++++++ pkg/remove.go | 43 ++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 cs.go diff --git a/cs.go b/cs.go new file mode 100644 index 0000000..daf6289 --- /dev/null +++ b/cs.go @@ -0,0 +1,22 @@ +package pm + +import ( + "bufio" + "fmt" + "io" + "strings" +) + +// ParseCS returns a parsed checksum file. +func ParseCS(f io.Reader) (map[string]string, error) { + cs := map[string]string{} + s := bufio.NewScanner(f) + for s.Scan() { + elems := strings.Split(s.Text(), "\t") + if len(elems) != 2 { + return nil, fmt.Errorf("manifest format error; got %d elements, want 2", len(elems)) + } + cs[elems[1]] = elems[0] + } + return cs, nil +} diff --git a/db/installed.go b/db/installed.go index c3045b3..c412ce3 100644 --- a/db/installed.go +++ b/db/installed.go @@ -24,6 +24,16 @@ func AddInstalled(root string, m pm.Meta) error { return savei(root, db) } +// RemoveInstalled adds m to the installed package database. +func RemoveInstalled(root string, m pm.Meta) error { + db, err := loadi(root) + if err != nil { + return errors.Wrap(err, "loading installed db") + } + delete(db, m.Name) + return savei(root, db) +} + // IsInstalled checks if m is in the installed package database. func IsInstalled(root string, m pm.Meta) (bool, error) { db, err := loadi(root) diff --git a/pkg/remove.go b/pkg/remove.go index 9be913d..a7840c5 100644 --- a/pkg/remove.go +++ b/pkg/remove.go @@ -1,9 +1,11 @@ package pkg import ( - "log" + "os" + "path/filepath" "github.com/pkg/errors" + "mcquay.me/pm" "mcquay.me/pm/db" ) @@ -18,7 +20,42 @@ func Remove(root string, pkgs []string) error { if err != nil { return errors.Wrap(err, "checking ability to remove") } - log.Printf("%+v", ms) - return errors.New("NYI") + for _, m := range ms { + if err := script(root, m, "pre-remove"); err != nil { + return errors.Wrap(err, "pre-remove") + } + + mdir := filepath.Join(root, installed, string(m.Name)) + bom := filepath.Join(mdir, "bom.sha256") + bf, err := os.Open(bom) + if err != nil { + return errors.Wrapf(err, "%q: opening bom", m.Name) + } + + cs, err := pm.ParseCS(bf) + if err != nil { + return errors.Wrapf(err, "%q: parsing bom", m.Name) + } + + for n := range cs { + if err := os.Remove(filepath.Join(root, n)); err != nil { + return errors.Wrapf(err, "pkg %q", m.Name) + } + } + + if err := script(root, m, "post-remove"); err != nil { + return errors.Wrap(err, "post-remove") + } + + if err := db.RemoveInstalled(root, m); err != nil { + return errors.Wrapf(err, "removing %q", m.Name) + } + + if err := os.RemoveAll(mdir); err != nil { + return errors.Wrapf(err, "%q: removing pm install dir", m.Name) + } + } + + return nil }