Add file-only mode to the add command

This commit is contained in:
Miguel de la Cruz 2020-03-01 13:21:10 +01:00
parent d5778abbd6
commit 61091e08e5
3 changed files with 12 additions and 4 deletions

View file

@ -47,7 +47,7 @@ Use "campaigner [command] --help" for more information about a command.
input. input.
- [x] Add `standalone` group of commands. - [x] Add `standalone` group of commands.
- [ ] Parametrise the atlassian API url. - [ ] Parametrise the atlassian API url.
- [ ] Add file only mode to the `add` command. - [x] Add file only mode to the `add` command.
- [ ] Add file path normalisation to the `add` command. - [ ] Add file path normalisation to the `add` command.
- [ ] Add `--ag` to the `add` command. - [ ] Add `--ag` to the `add` command.
- [ ] Add the `publish` command. - [ ] Add the `publish` command.

View file

@ -31,6 +31,7 @@ func AddCmd() *cobra.Command {
cmd.Flags().BoolP("ag", "a", false, "generates the tickets reading ag's output from stdin") cmd.Flags().BoolP("ag", "a", false, "generates the tickets reading ag's output from stdin")
cmd.Flags().BoolP("grep", "g", false, "generates the tickets reading grep's output from stdin") cmd.Flags().BoolP("grep", "g", false, "generates the tickets reading grep's output from stdin")
cmd.Flags().BoolP("govet", "v", false, "generates the tickets reading govet's output from stdin") cmd.Flags().BoolP("govet", "v", false, "generates the tickets reading govet's output from stdin")
cmd.Flags().BoolP("file-only", "f", false, "generates one ticket per file instead of per match")
return cmd return cmd
} }
@ -73,6 +74,7 @@ func addCmdF(cmd *cobra.Command, _ []string) error {
grep, _ := cmd.Flags().GetBool("grep") grep, _ := cmd.Flags().GetBool("grep")
ag, _ := cmd.Flags().GetBool("ag") ag, _ := cmd.Flags().GetBool("ag")
govet, _ := cmd.Flags().GetBool("govet") govet, _ := cmd.Flags().GetBool("govet")
fileOnly, _ := cmd.Flags().GetBool("file-only")
if !grep && !ag && !govet { if !grep && !ag && !govet {
return fmt.Errorf("one of --grep --ag --govet flags should be active") return fmt.Errorf("one of --grep --ag --govet flags should be active")
@ -92,7 +94,7 @@ func addCmdF(cmd *cobra.Command, _ []string) error {
} }
cmp.Tickets = append(cmp.Tickets, tickets...) cmp.Tickets = append(cmp.Tickets, tickets...)
cmp.Tickets = model.RemoveDuplicateTickets(cmp.Tickets) cmp.Tickets = model.RemoveDuplicateTickets(cmp.Tickets, fileOnly)
if err := campaign.Save(cmp); err != nil { if err := campaign.Save(cmp); err != nil {
ErrorAndExit(cmd, err) ErrorAndExit(cmd, err)

View file

@ -10,11 +10,17 @@ type Ticket struct {
Text string `json:"text"` Text string `json:"text"`
} }
func RemoveDuplicateTickets(tickets []*Ticket) []*Ticket { func RemoveDuplicateTickets(tickets []*Ticket, fileOnly bool) []*Ticket {
ticketMap := map[string]*Ticket{} ticketMap := map[string]*Ticket{}
for _, t := range tickets { for _, t := range tickets {
if fileOnly {
t.Text = ""
t.LineNo = 0
ticketMap[t.Filename] = t
} else {
ticketMap[fmt.Sprintf("%s:%d", t.Filename, t.LineNo)] = t ticketMap[fmt.Sprintf("%s:%d", t.Filename, t.LineNo)] = t
} }
}
cleanTickets := []*Ticket{} cleanTickets := []*Ticket{}
for _, t := range ticketMap { for _, t := range ticketMap {