2020-02-29 00:59:54 +01:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-02-29 17:45:53 +01:00
|
|
|
"bufio"
|
2020-03-07 11:11:40 +01:00
|
|
|
"encoding/csv"
|
2020-02-29 13:00:40 +01:00
|
|
|
"fmt"
|
2020-02-29 17:45:53 +01:00
|
|
|
"os"
|
2020-02-29 13:00:40 +01:00
|
|
|
|
2020-02-29 00:59:54 +01:00
|
|
|
"github.com/spf13/cobra"
|
2020-02-29 01:20:46 +01:00
|
|
|
|
2020-04-29 18:52:15 +01:00
|
|
|
"git.ctrlz.es/mgdelacroix/campaigner/app"
|
2020-02-29 01:20:46 +01:00
|
|
|
"git.ctrlz.es/mgdelacroix/campaigner/model"
|
2020-04-27 16:47:13 +01:00
|
|
|
"git.ctrlz.es/mgdelacroix/campaigner/parsers"
|
2020-02-29 00:59:54 +01:00
|
|
|
)
|
|
|
|
|
2020-03-04 21:38:49 +01:00
|
|
|
func GrepAddCmd() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
2020-04-27 10:42:29 +01:00
|
|
|
Use: "grep",
|
|
|
|
Short: "Generates the tickets reading grep's output from stdin",
|
2020-04-27 16:47:13 +01:00
|
|
|
Long: `Generates tickets for the campaign reading the output of grep from the standard input. The grep command must be run with the -n flag. The generated ticket will contain three fields:
|
2020-04-27 10:42:29 +01:00
|
|
|
|
|
|
|
- filename: the filename yield by grep
|
|
|
|
- lineNo: the line number yield by grep
|
|
|
|
- text: the trimmed line that grep captured for the expression
|
|
|
|
`,
|
2020-03-04 21:38:49 +01:00
|
|
|
Example: ` grep -nriIF --include \*.go cobra.Command | campaigner add grep`,
|
2020-03-04 22:13:55 +01:00
|
|
|
Args: cobra.NoArgs,
|
2020-04-29 18:52:15 +01:00
|
|
|
Run: withApp(grepAddCmdF),
|
2020-03-04 21:38:49 +01:00
|
|
|
}
|
|
|
|
|
2020-03-05 20:42:25 +01:00
|
|
|
cmd.Flags().BoolP("file-only", "f", false, "Generates one ticket per file instead of per match")
|
2020-03-04 21:38:49 +01:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func AgAddCmd() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
2020-03-04 22:13:55 +01:00
|
|
|
Use: "ag",
|
2020-03-04 22:22:16 +01:00
|
|
|
Short: "Generates the tickets reading ag's output from stdin",
|
2020-03-04 22:13:55 +01:00
|
|
|
Long: "Generates tickets for the campaign reading from the standard input the output ag",
|
2020-03-04 21:38:49 +01:00
|
|
|
Example: ` ag cobra.Command | campaigner add ag`,
|
2020-03-04 22:13:55 +01:00
|
|
|
Args: cobra.NoArgs,
|
|
|
|
RunE: agAddCmdF,
|
2020-03-04 21:38:49 +01:00
|
|
|
}
|
|
|
|
|
2020-03-05 20:42:25 +01:00
|
|
|
cmd.Flags().BoolP("file-only", "f", false, "Generates one ticket per file instead of per match")
|
2020-03-04 21:38:49 +01:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func GovetAddCmd() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
2020-04-27 16:50:59 +01:00
|
|
|
Use: "govet",
|
|
|
|
Short: "Generates the tickets reading govet's output from stdin",
|
|
|
|
Long: `Generates tickets for the campaign reading the output of govet from the standard input. Govet usually writes to the standard error descriptor, so the output must be redirected. The generated ticket will contain three fields:
|
2020-04-27 16:47:13 +01:00
|
|
|
|
|
|
|
- filename: the filename yield by grep
|
|
|
|
- lineNo: the line number yield by grep
|
|
|
|
- text: the text containing the govet error
|
|
|
|
`,
|
|
|
|
Example: ` govet ./... 2>&1 | campaigner add govet`,
|
2020-03-04 22:13:55 +01:00
|
|
|
Args: cobra.NoArgs,
|
2020-04-29 18:52:15 +01:00
|
|
|
Run: withApp(govetAddCmdF),
|
2020-03-04 21:38:49 +01:00
|
|
|
}
|
|
|
|
|
2020-03-05 20:42:25 +01:00
|
|
|
cmd.Flags().BoolP("file-only", "f", false, "Generates one ticket per file instead of per match")
|
2020-03-04 21:38:49 +01:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func CsvAddCmd() *cobra.Command {
|
2020-03-07 11:11:40 +01:00
|
|
|
return &cobra.Command{
|
2020-03-04 22:13:55 +01:00
|
|
|
Use: "csv",
|
2020-03-04 22:22:16 +01:00
|
|
|
Short: "Generates the tickets reading a csv file",
|
2020-03-07 11:11:40 +01:00
|
|
|
Example: ` campaigner add csv tickets.csv`,
|
|
|
|
Args: cobra.ExactArgs(1),
|
2020-04-29 18:52:15 +01:00
|
|
|
Run: withApp(csvAddCmdF),
|
2020-03-04 21:38:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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",
|
2020-04-27 11:22:15 +01:00
|
|
|
Short: "Adds tickets to the campaign",
|
|
|
|
Long: "Adds tickets to the campaign from the output of grep/ag/govet",
|
2020-02-29 00:59:54 +01:00
|
|
|
}
|
|
|
|
|
2020-03-04 21:38:49 +01:00
|
|
|
cmd.AddCommand(
|
|
|
|
GrepAddCmd(),
|
|
|
|
AgAddCmd(),
|
|
|
|
GovetAddCmd(),
|
|
|
|
CsvAddCmd(),
|
|
|
|
)
|
2020-02-29 01:20:46 +01:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2020-04-29 18:52:15 +01:00
|
|
|
func grepAddCmdF(a *app.App, cmd *cobra.Command, _ []string) {
|
2020-03-01 13:21:10 +01:00
|
|
|
fileOnly, _ := cmd.Flags().GetBool("file-only")
|
2020-02-29 14:29:32 +01:00
|
|
|
|
2020-04-27 16:47:13 +01:00
|
|
|
tickets := parsers.ParseWith(parsers.GREP)
|
2020-04-29 18:52:15 +01:00
|
|
|
a.Campaign.AddTickets(tickets, fileOnly)
|
2020-02-29 13:00:40 +01:00
|
|
|
|
2020-04-29 18:52:15 +01:00
|
|
|
if err := a.Save(); err != nil {
|
2020-02-29 13:40:39 +01:00
|
|
|
ErrorAndExit(cmd, err)
|
2020-02-29 13:00:40 +01:00
|
|
|
}
|
2020-03-07 12:04:24 +01:00
|
|
|
cmd.Printf("%d tickets have been added\n", len(tickets))
|
2020-03-04 21:38:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func agAddCmdF(_ *cobra.Command, _ []string) error {
|
|
|
|
return fmt.Errorf("not implemented yet")
|
|
|
|
}
|
|
|
|
|
2020-04-29 18:52:15 +01:00
|
|
|
func govetAddCmdF(a *app.App, cmd *cobra.Command, _ []string) {
|
2020-04-27 16:47:13 +01:00
|
|
|
fileOnly, _ := cmd.Flags().GetBool("file-only")
|
|
|
|
|
|
|
|
tickets := parsers.ParseWith(parsers.GOVET)
|
2020-04-29 18:52:15 +01:00
|
|
|
a.Campaign.AddTickets(tickets, fileOnly)
|
2020-04-27 16:47:13 +01:00
|
|
|
|
2020-04-29 18:52:15 +01:00
|
|
|
if err := a.Save(); err != nil {
|
2020-04-27 16:47:13 +01:00
|
|
|
ErrorAndExit(cmd, err)
|
|
|
|
}
|
|
|
|
cmd.Printf("%d tickets have been added\n", len(tickets))
|
2020-03-04 21:38:49 +01:00
|
|
|
}
|
|
|
|
|
2020-04-29 18:52:15 +01:00
|
|
|
func csvAddCmdF(a *app.App, cmd *cobra.Command, args []string) {
|
2020-03-07 11:11:40 +01:00
|
|
|
file, err := os.Open(args[0])
|
|
|
|
if err != nil {
|
|
|
|
ErrorAndExit(cmd, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
csvReader := csv.NewReader(bufio.NewReader(file))
|
|
|
|
records, err := csvReader.ReadAll()
|
|
|
|
if err != nil {
|
|
|
|
ErrorAndExit(cmd, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
headers := records[0]
|
|
|
|
for _, line := range records[1:] {
|
|
|
|
data := map[string]interface{}{}
|
|
|
|
for i, header := range headers {
|
|
|
|
data[header] = line[i]
|
|
|
|
}
|
2020-04-29 18:52:15 +01:00
|
|
|
a.Campaign.Tickets = append(a.Campaign.Tickets, &model.Ticket{Data: data})
|
2020-03-07 11:11:40 +01:00
|
|
|
}
|
|
|
|
|
2020-04-29 18:52:15 +01:00
|
|
|
if err := a.Save(); err != nil {
|
2020-03-07 11:11:40 +01:00
|
|
|
ErrorAndExit(cmd, err)
|
|
|
|
}
|
2020-03-07 12:04:24 +01:00
|
|
|
cmd.Printf("%d tickets have been added\n", len(records[1:]))
|
2020-02-29 00:59:54 +01:00
|
|
|
}
|