From 2360320cf67710a042e530e19c1cb83b2484aea1 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Sat, 29 Feb 2020 14:17:34 +0100 Subject: [PATCH] Removes ticket duplicates --- cmd/add.go | 1 + model/ticket.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/cmd/add.go b/cmd/add.go index 9e3681d..4bcbe89 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -116,6 +116,7 @@ func addCmdF(cmd *cobra.Command, _ []string) { // ToDo: make this skip duplicates cmp.Tickets = append(cmp.Tickets, tickets...) + cmp.Tickets = model.RemoveDuplicateTickets(cmp.Tickets) if err := campaign.Save(cmp); err != nil { ErrorAndExit(cmd, err) diff --git a/model/ticket.go b/model/ticket.go index 3516bb2..24d155a 100644 --- a/model/ticket.go +++ b/model/ticket.go @@ -1,7 +1,25 @@ package model +import ( + "fmt" +) + type Ticket struct { Filename string `json:"filename"` LineNo int `json:"line_no"` Text string `json:"text"` // needed?? } + +func RemoveDuplicateTickets(tickets []*Ticket) []*Ticket { + ticketMap := map[string]*Ticket{} + for _, t := range tickets { + ticketMap[fmt.Sprintf("%s:%d", t.Filename, t.LineNo)] = t + } + + cleanTickets := []*Ticket{} + for _, t := range ticketMap { + cleanTickets = append(cleanTickets, t) + } + + return cleanTickets +}