Skip to content

Commit 2776528

Browse files
committed
implement first pass at a csv output flag
1 parent 6743ae5 commit 2776528

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

cmd/commands/todos.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package commands
22

33
import (
44
"context"
5+
"encoding/csv"
56
"fmt"
67
"os"
78
"path/filepath"
89
"sort"
10+
"strconv"
911
"time"
1012

1113
"github.com/augmentable-dev/tickgit/pkg/comments"
@@ -14,7 +16,11 @@ import (
1416
"github.com/spf13/cobra"
1517
)
1618

19+
var csvOutput bool
20+
1721
func init() {
22+
todosCmd.Flags().BoolVar(&csvOutput, "csv-output", false, "specify whether or not output should be CSV")
23+
1824
rootCmd.AddCommand(todosCmd)
1925
}
2026

@@ -63,6 +69,35 @@ var todosCmd = &cobra.Command{
6369

6470
s.Stop()
6571

66-
todos.WriteTodos(foundToDos, os.Stdout)
72+
if csvOutput {
73+
w := csv.NewWriter(os.Stdout)
74+
err := w.Write([]string{
75+
"text", "start_line", "start_position", "end_line", "end_position", "author", "author_email", "author_sha", "author_time",
76+
})
77+
handleError(err, s)
78+
for _, todo := range foundToDos {
79+
err := w.Write([]string{
80+
todo.String,
81+
strconv.Itoa(todo.StartLocation.Line),
82+
strconv.Itoa(todo.StartLocation.Pos),
83+
strconv.Itoa(todo.EndLocation.Line),
84+
strconv.Itoa(todo.EndLocation.Pos),
85+
todo.Blame.Author.Name,
86+
todo.Blame.Author.Email,
87+
todo.Blame.SHA,
88+
todo.Blame.Author.When.Format(time.RFC3339),
89+
})
90+
handleError(err, s)
91+
}
92+
// Write any buffered data to the underlying writer (standard output).
93+
w.Flush()
94+
95+
err = w.Error()
96+
handleError(err, s)
97+
98+
} else {
99+
todos.WriteTodos(foundToDos, os.Stdout)
100+
}
101+
67102
},
68103
}

0 commit comments

Comments
 (0)