aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--commands/patch/delete.go54
-rw-r--r--lib/pama/delete.go45
2 files changed, 99 insertions, 0 deletions
diff --git a/commands/patch/delete.go b/commands/patch/delete.go
new file mode 100644
index 00000000..2e51ab7c
--- /dev/null
+++ b/commands/patch/delete.go
@@ -0,0 +1,54 @@
+package patch
+
+import (
+ "fmt"
+ "time"
+
+ "git.sr.ht/~rjarry/aerc/app"
+ "git.sr.ht/~rjarry/aerc/commands"
+ "git.sr.ht/~rjarry/aerc/lib/pama"
+ "git.sr.ht/~rjarry/aerc/log"
+)
+
+type Delete struct {
+ Tag string `opt:"tag" required:"false" complete:"Complete"`
+}
+
+func init() {
+ register(Delete{})
+}
+
+func (Delete) Aliases() []string {
+ return []string{"delete"}
+}
+
+func (*Delete) Complete(arg string) []string {
+ names, err := pama.New().Names()
+ if err != nil {
+ log.Errorf("failed to get completion: %v", err)
+ return nil
+ }
+ return commands.FilterList(names, arg, nil)
+}
+
+func (d Delete) Execute(args []string) error {
+ m := pama.New()
+
+ name := d.Tag
+ if name == "" {
+ p, err := m.CurrentProject()
+ if err != nil {
+ return err
+ }
+ name = p.Name
+ }
+
+ err := m.Delete(name)
+ if err != nil {
+ return err
+ }
+
+ app.PushStatus(fmt.Sprintf("Project '%s' deleted.", name),
+ 10*time.Second)
+ return nil
+}
diff --git a/lib/pama/delete.go b/lib/pama/delete.go
new file mode 100644
index 00000000..18a93a27
--- /dev/null
+++ b/lib/pama/delete.go
@@ -0,0 +1,45 @@
+package pama
+
+import "fmt"
+
+// Delete removes provided project
+func (m PatchManager) Delete(name string) error {
+ store := m.store()
+ names, err := m.Names()
+ if err != nil {
+ return err
+ }
+
+ index := -1
+ for i, s := range names {
+ if s == name {
+ index = i
+ break
+ }
+ }
+ if index < 0 {
+ return fmt.Errorf("Project '%s' not found", name)
+ }
+
+ cur, err := m.CurrentProject()
+ if err == nil && cur.Name == name {
+ var next string
+ for _, s := range names {
+ if name != s {
+ next = s
+ break
+ }
+ }
+ err = store.SetCurrent(next)
+ if err != nil {
+ return storeErr(err)
+ }
+ }
+
+ return storeErr(m.store().DeleteProject(name))
+}
+
+func (m PatchManager) Names() ([]string, error) {
+ names, err := m.store().Names()
+ return names, storeErr(err)
+}