From 18953f93725c20ca6190ff22fb2708f042464573 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Sat, 29 Feb 2020 13:00:40 +0100 Subject: [PATCH] Adds the grep parsing and ticket creation --- cmd/add.go | 77 ++++++++++++++++++++++++++++++++++++++++++----- model/campaign.go | 3 +- model/ticket.go | 2 +- 3 files changed, 72 insertions(+), 10 deletions(-) diff --git a/cmd/add.go b/cmd/add.go index 4cbf311..d3b7dc4 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -1,17 +1,24 @@ package cmd import ( + "fmt" + "os/exec" + "strconv" + "strings" + "github.com/spf13/cobra" "git.ctrlz.es/mgdelacroix/campaigner/model" ) +const defaultGrepOpts = "-nrFI" + func AddCmd() *cobra.Command { cmd := &cobra.Command{ Use: "add", Short: "Adds tickets to the campaign", Args: cobra.NoArgs, - RunE: addCmdF, + Run: addCmdF, } cmd.Flags().StringP("dir", "d", "", "directory containing the source code") @@ -24,15 +31,69 @@ func AddCmd() *cobra.Command { return cmd } -func RunGrep(dir string, strs []string, caseInsensitive bool) ([]model.Ticket, error) { - // grep -nrI TEXT . - // -i as well with case insensitive +func parseLine(line string) (*model.Ticket, error) { + parts := strings.Split(line, ":") + if len(parts) < 3 { + return nil, fmt.Errorf("cannot parse line: %s", line) + } - return nil, nil + filename := parts[0] + lineNo, err := strconv.Atoi(parts[1]) + if err != nil { + return nil, err + } + text := strings.Join(parts[2:], "") + + return &model.Ticket{filename, lineNo, text}, nil } -func addCmdF(cmd *cobra.Command, _ []string) error { - // either govet or grep +func RunGrep(dir, str string, caseInsensitive bool) ([]*model.Ticket, error) { + opts := defaultGrepOpts + if caseInsensitive { + opts = opts + "i" + } - return nil + out, err := exec.Command("grep", opts, str, dir).Output() + if err != nil { + return nil, err + } + + tickets := []*model.Ticket{} + for _, line := range strings.Split(string(out), "\n") { + // ToDo: get and check error + ticket, _ := parseLine(line) + if ticket != nil { + tickets = append(tickets, ticket) + } + } + + return tickets, nil +} + +func RunGreps(dir string, strs []string, caseInsensitive bool) ([]*model.Ticket, error) { + tickets := []*model.Ticket{} + for _, str := range strs { + results, err := RunGrep(dir, str, caseInsensitive) + if err != nil { + return nil, err + } + tickets = append(tickets, results...) + } + + return tickets, nil +} + +func addCmdF(cmd *cobra.Command, _ []string) { + dir, _ := cmd.Flags().GetString("dir") + grepStrs, _ := cmd.Flags().GetStringSlice("grep") + caseInsensitive, _ := cmd.Flags().GetBool("case-insensitive") + + tickets, err := RunGreps(dir, grepStrs, caseInsensitive) + if err != nil { + ErrorAndExit(cmd, err) + } + + for _, ticket := range tickets { + fmt.Printf("%+v\n", ticket) + } } diff --git a/model/campaign.go b/model/campaign.go index 024281f..369f4dd 100644 --- a/model/campaign.go +++ b/model/campaign.go @@ -1,5 +1,6 @@ package model type Campaign struct { - Epic string `json:"epic"` + Epic string `json:"epic"` + Tickets []*Ticket `json:"tickets"` } diff --git a/model/ticket.go b/model/ticket.go index 06c32b5..3516bb2 100644 --- a/model/ticket.go +++ b/model/ticket.go @@ -2,6 +2,6 @@ package model type Ticket struct { Filename string `json:"filename"` - Line int `json:"line"` + LineNo int `json:"line_no"` Text string `json:"text"` // needed?? }