Skip to content

Commit b8d11a3

Browse files
committed
FEAT - add csv flag
1 parent f7d61a5 commit b8d11a3

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed

cmd/get.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ var getCmd = &cobra.Command{
3737
return
3838
}
3939

40+
if flags.Csv != "" && !flags.Poll {
41+
printErrorProps := S.PrintErrorProps{
42+
Error: errors.New("CSV output is only supported with polling"),
43+
Message: "CSV output is only supported with polling.",
44+
}
45+
U.PrintError(printErrorProps)
46+
return
47+
}
48+
4049
U.PollTranscription(id, flags)
4150
},
4251
}

utils/transcribe.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
)
2323

2424
var width int
25+
var Flags S.TranscribeFlags
2526

2627
func Transcribe(params S.TranscribeParams, flags S.TranscribeFlags) {
2728
Token = GetStoredToken()
@@ -223,8 +224,8 @@ func UploadFile(path string) string {
223224
}
224225

225226
func PollTranscription(id string, flags S.TranscribeFlags) {
227+
Flags = flags
226228
fmt.Fprintln(os.Stdin, "Transcribing file with id "+id)
227-
228229
s := CallSpinner(" Processing time is usually 20% of the file's duration.")
229230

230231
for {
@@ -277,14 +278,24 @@ func PollTranscription(id string, flags S.TranscribeFlags) {
277278
fmt.Println(string(print))
278279
return
279280
}
280-
getFormattedOutput(transcript, flags)
281+
if flags.Csv != "" {
282+
if filepath.Ext(flags.Csv) == "" {
283+
flags.Csv = flags.Csv + ".csv"
284+
}
285+
286+
row := [][]string{}
287+
row = append(row, []string{"\"" + *transcript.Text + "\""})
288+
GenerateCsv(flags.Csv, []string{"text"}, row)
289+
}
290+
291+
getFormattedOutput(transcript)
281292
return
282293
}
283294
time.Sleep(3 * time.Second)
284295
}
285296
}
286297

287-
func getFormattedOutput(transcript S.TranscriptResponse, flags S.TranscribeFlags) {
298+
func getFormattedOutput(transcript S.TranscriptResponse) {
288299
getWidth, _, err := term.GetSize(0)
289300
if err != nil {
290301
width = 512

utils/utils.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,3 +468,47 @@ func Contains(s []string, e string) bool {
468468
}
469469
return false
470470
}
471+
472+
func GenerateCsv(filename string, headers []string, data [][]string) {
473+
file, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
474+
if err != nil {
475+
printErrorProps := S.PrintErrorProps{
476+
Error: err,
477+
Message: "Error opening file",
478+
}
479+
PrintError(printErrorProps)
480+
}
481+
defer file.Close()
482+
483+
// if file is empty, write headers
484+
fileInfo, err := file.Stat()
485+
if err != nil {
486+
printErrorProps := S.PrintErrorProps{
487+
Error: err,
488+
Message: "Error getting file info",
489+
}
490+
PrintError(printErrorProps)
491+
}
492+
if fileInfo.Size() == 0 {
493+
_, err := file.WriteString(strings.Join(headers, ",") + "\n")
494+
if err != nil {
495+
printErrorProps := S.PrintErrorProps{
496+
Error: err,
497+
Message: "Error writing headers",
498+
}
499+
PrintError(printErrorProps)
500+
}
501+
}
502+
503+
for _, value := range data {
504+
_, err := file.WriteString(strings.Join(value, ",") + "\n")
505+
if err != nil {
506+
printErrorProps := S.PrintErrorProps{
507+
Error: err,
508+
Message: "Error writing to file",
509+
}
510+
PrintError(printErrorProps)
511+
}
512+
513+
}
514+
}

0 commit comments

Comments
 (0)