From 4e4ca106aea25da74f1df49d33f4eaa272a6e8f0 Mon Sep 17 00:00:00 2001 From: vince Date: Mon, 20 Jul 2020 09:55:14 +0800 Subject: [PATCH] Allow user to delete remote bugs --- bug/bug.go | 7 +++++++ cache/repo_cache_bug.go | 17 ++++++++++++++--- commands/rm.go | 22 +++++++--------------- 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/bug/bug.go b/bug/bug.go index 04bd599602375e0457edf4e96faf6e594002fddc..95c4325fb929e3c3d948036c00896aab6b169980 100644 --- a/bug/bug.go +++ b/bug/bug.go @@ -242,11 +242,18 @@ func readBug(repo repository.ClockedRepo, ref string) (*Bug, error) { return &bug, nil } +// RemoveLocalBug will remove a local bug from its hash func RemoveLocalBug(repo repository.ClockedRepo, id entity.Id) error { ref := bugsRefPattern + id.String() return repo.RemoveRef(ref) } +// RemoveRemoteBug will remove a remote bug locally from its hash +func RemoveRemoteBug(repo repository.ClockedRepo, remote string, id entity.Id) error { + ref := fmt.Sprintf(bugsRemoteRefPattern, remote) + id.String() + return repo.RemoveRef(ref) +} + type StreamedBug struct { Bug *Bug Err error diff --git a/cache/repo_cache_bug.go b/cache/repo_cache_bug.go index 0492e7f1237905885ef5d9972ae19f944df2c137..0c26cb389ec40bf012047edcc1d74cc3df441c1e 100644 --- a/cache/repo_cache_bug.go +++ b/cache/repo_cache_bug.go @@ -361,13 +361,24 @@ func (c *RepoCache) NewBugRaw(author *IdentityCache, unixTime int64, title strin } // RemoveBug removes a bug from the cache and repo -func (c *RepoCache) RemoveBug(prefix string) error { - b, err := c.ResolveBugPrefix(prefix) +// args[0] specifies the bug prefix to remove +// args[1] (if present) specifies the remote the bug was imported from +func (c *RepoCache) RemoveBug(args []string) error { + if len(args) == 0 { + return fmt.Errorf("you must provide a bug prefix to remove") + } + + b, err := c.ResolveBugPrefix(args[0]) + if err != nil { return err } - err = bug.RemoveLocalBug(c.repo, b.Id()) + if len(args) == 1 { + err = bug.RemoveLocalBug(c.repo, b.Id()) + } else { + err = bug.RemoveRemoteBug(c.repo, args[1], b.Id()) + } if err != nil { return err } diff --git a/commands/rm.go b/commands/rm.go index 7b34a6299f8027092196210518fb0f697c971ff2..718fb4a34adfdd764404f10ec8e227cd356ae27d 100644 --- a/commands/rm.go +++ b/commands/rm.go @@ -1,25 +1,20 @@ package commands import ( - "fmt" - "github.com/spf13/cobra" ) -type rmOptions struct { -} - func newRmCommand() *cobra.Command { env := newEnv() - options := rmOptions{} cmd := &cobra.Command{ - Use: "rm ", + Use: "rm []", Short: "Remove an existing bug.", + Long: "Remove an existing bug in the local repository. If the bug was imported from a bridge, specify the remote name to remove it from. Note removing bugs that were imported from bridges will not remove the bug remote, and will only remove the local copy of the bug.", PreRunE: loadBackendEnsureUser(env), PostRunE: closeBackend(env), RunE: func(cmd *cobra.Command, args []string) error { - return runRm(env, options, args) + return runRm(env, args) }, } @@ -29,17 +24,14 @@ func newRmCommand() *cobra.Command { 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") - } +func runRm(env *Env, args []string) (err error) { + err = env.backend.RemoveBug(args) - err := env.backend.RemoveBug(args[0]) if err != nil { - return err + return } env.out.Printf("bug %s removed\n", args[0]) - return nil + return }