campaigner/cmd/add.go

127 lines
3.1 KiB
Go
Raw Normal View History

2020-02-29 00:59:54 +01:00
package cmd
import (
"fmt"
"os/exec"
"strconv"
"strings"
2020-02-29 00:59:54 +01:00
"github.com/spf13/cobra"
2020-02-29 01:20:46 +01:00
2020-02-29 13:40:39 +01:00
"git.ctrlz.es/mgdelacroix/campaigner/campaign"
2020-02-29 01:20:46 +01:00
"git.ctrlz.es/mgdelacroix/campaigner/model"
2020-02-29 00:59:54 +01:00
)
const defaultGrepOpts = "-nrFI"
2020-02-29 00:59:54 +01:00
func AddCmd() *cobra.Command {
2020-02-29 01:20:46 +01:00
cmd := &cobra.Command{
2020-02-29 00:59:54 +01:00
Use: "add",
Short: "Adds tickets to the campaign",
2020-02-29 01:20:46 +01:00
Args: cobra.NoArgs,
Run: addCmdF,
2020-02-29 00:59:54 +01:00
}
2020-02-29 01:20:46 +01:00
cmd.Flags().StringP("dir", "d", "", "directory containing the source code")
_ = cmd.MarkFlagRequired("dir")
cmd.Flags().StringSliceP("grep", "g", []string{}, "runs a grep command to generate the tickets")
cmd.Flags().BoolP("case-insensitive", "i", false, "makes the search case insensitive")
cmd.Flags().StringSliceP("ext", "e", []string{}, "limits the grep to files with certain extensions")
2020-02-29 01:20:46 +01:00
// cmd.Flags().StringP("govet", "v", "", "runs a govet command to generate the tickets")
// govet bin path?
return cmd
}
func parseLine(line string) (*model.Ticket, error) {
2020-02-29 13:11:36 +01:00
// ToDo: it would be great to be able to relate a line with its
// parent method, at least for JS and Golang
parts := strings.Split(line, ":")
if len(parts) < 3 {
return nil, fmt.Errorf("cannot parse line: %s", line)
}
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 RunGrep(dir, str string, exts []string, caseInsensitive bool) ([]*model.Ticket, error) {
opts := defaultGrepOpts
if caseInsensitive {
opts = opts + "i"
}
includes := []string{}
for _, ext := range exts {
if strings.HasPrefix(ext, ".") {
ext = ext[1:]
}
includes = append(includes, []string{"--include", "*." + ext}...)
}
args := append([]string{opts}, includes...)
args = append(args, str, dir)
out, err := exec.Command("grep", args...).Output()
if err != nil {
return nil, fmt.Errorf("execution of grep failed: %w", 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, exts []string, caseInsensitive bool) ([]*model.Ticket, error) {
tickets := []*model.Ticket{}
for _, str := range strs {
results, err := RunGrep(dir, str, exts, caseInsensitive)
if err != nil {
return nil, err
}
tickets = append(tickets, results...)
}
2020-02-29 01:20:46 +01:00
2020-02-29 14:27:36 +01:00
tickets = model.RemoveDuplicateTickets(tickets)
fmt.Printf("%d matches found\n", len(tickets))
return tickets, nil
2020-02-29 00:59:54 +01:00
}
func addCmdF(cmd *cobra.Command, _ []string) {
dir, _ := cmd.Flags().GetString("dir")
grepStrs, _ := cmd.Flags().GetStringSlice("grep")
extStrs, _ := cmd.Flags().GetStringSlice("ext")
caseInsensitive, _ := cmd.Flags().GetBool("case-insensitive")
2020-02-29 01:20:46 +01:00
tickets, err := RunGreps(dir, grepStrs, extStrs, caseInsensitive)
if err != nil {
ErrorAndExit(cmd, err)
}
2020-02-29 13:40:39 +01:00
cmp, err := campaign.Read()
if err != nil {
ErrorAndExit(cmd, err)
}
cmp.Tickets = append(cmp.Tickets, tickets...)
2020-02-29 14:17:34 +01:00
cmp.Tickets = model.RemoveDuplicateTickets(cmp.Tickets)
2020-02-29 13:40:39 +01:00
if err := campaign.Save(cmp); err != nil {
ErrorAndExit(cmd, err)
}
2020-02-29 00:59:54 +01:00
}