From 36f300cb35b203310e923cf956310c7f20ed7406 Mon Sep 17 00:00:00 2001 From: vince Date: Thu, 16 Jul 2020 17:12:48 +0800 Subject: [PATCH] Add the 'rm' command This commit adds a command that removes a bug from the repository, given a prefix. --- cache/repo_cache_bug.go | 18 +++++++++++++++++ commands/rm.go | 45 +++++++++++++++++++++++++++++++++++++++++ commands/root.go | 1 + 3 files changed, 64 insertions(+) create mode 100644 commands/rm.go diff --git a/cache/repo_cache_bug.go b/cache/repo_cache_bug.go index 306923633a4e4f0772c651a15ced0ca4140cec16..0492e7f1237905885ef5d9972ae19f944df2c137 100644 --- a/cache/repo_cache_bug.go +++ b/cache/repo_cache_bug.go @@ -359,3 +359,21 @@ func (c *RepoCache) NewBugRaw(author *IdentityCache, unixTime int64, title strin return cached, op, nil } + +// RemoveBug removes a bug from the cache and repo +func (c *RepoCache) RemoveBug(prefix string) error { + b, err := c.ResolveBugPrefix(prefix) + if err != nil { + return err + } + + err = bug.RemoveLocalBug(c.repo, b.Id()) + if err != nil { + return err + } + + delete(c.bugs, b.Id()) + delete(c.bugExcerpts, b.Id()) + + return c.writeBugCache() +} diff --git a/commands/rm.go b/commands/rm.go new file mode 100644 index 0000000000000000000000000000000000000000..7b34a6299f8027092196210518fb0f697c971ff2 --- /dev/null +++ b/commands/rm.go @@ -0,0 +1,45 @@ +package commands + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +type rmOptions struct { +} + +func newRmCommand() *cobra.Command { + env := newEnv() + options := rmOptions{} + + cmd := &cobra.Command{ + Use: "rm ", + Short: "Remove an existing bug.", + PreRunE: loadBackendEnsureUser(env), + PostRunE: closeBackend(env), + RunE: func(cmd *cobra.Command, args []string) error { + return runRm(env, options, args) + }, + } + + flags := cmd.Flags() + flags.SortFlags = false + + return cmd +} + +func runRm(env *Env, opts rmOptions, args []string) error { + if len(args) == 0 { + return fmt.Errorf("you must provide a bug id prefix to remove") + } + + err := env.backend.RemoveBug(args[0]) + if err != nil { + return err + } + + env.out.Printf("bug %s removed\n", args[0]) + + return nil +} diff --git a/commands/root.go b/commands/root.go index a67fec1a3f69c4323dd3fedcee3d273ea03d6f3b..e7848363ba4f80edc69079ff9e51900f670e509e 100644 --- a/commands/root.go +++ b/commands/root.go @@ -71,6 +71,7 @@ _git_bug() { cmd.AddCommand(newLsLabelCommand()) cmd.AddCommand(newPullCommand()) cmd.AddCommand(newPushCommand()) + cmd.AddCommand(newRmCommand()) cmd.AddCommand(newSelectCommand()) cmd.AddCommand(newShowCommand()) cmd.AddCommand(newStatusCommand())