Skip to content

Commit 457e868

Browse files
committed
FEAT - add csv flag
1 parent a3fab19 commit 457e868

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
@@ -404,3 +404,47 @@ func CheckForUpdates(currentVersion string) {
404404
TelemetryCaptureEvent("CLI update available", properties)
405405
}
406406
}
407+
408+
func GenerateCsv(filename string, headers []string, data [][]string) {
409+
file, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
410+
if err != nil {
411+
printErrorProps := S.PrintErrorProps{
412+
Error: err,
413+
Message: "Error opening file",
414+
}
415+
PrintError(printErrorProps)
416+
}
417+
defer file.Close()
418+
419+
// if file is empty, write headers
420+
fileInfo, err := file.Stat()
421+
if err != nil {
422+
printErrorProps := S.PrintErrorProps{
423+
Error: err,
424+
Message: "Error getting file info",
425+
}
426+
PrintError(printErrorProps)
427+
}
428+
if fileInfo.Size() == 0 {
429+
_, err := file.WriteString(strings.Join(headers, ",") + "\n")
430+
if err != nil {
431+
printErrorProps := S.PrintErrorProps{
432+
Error: err,
433+
Message: "Error writing headers",
434+
}
435+
PrintError(printErrorProps)
436+
}
437+
}
438+
439+
for _, value := range data {
440+
_, err := file.WriteString(strings.Join(value, ",") + "\n")
441+
if err != nil {
442+
printErrorProps := S.PrintErrorProps{
443+
Error: err,
444+
Message: "Error writing to file",
445+
}
446+
PrintError(printErrorProps)
447+
}
448+
449+
}
450+
}

0 commit comments

Comments
 (0)