Skip to content

Commit

Permalink
fixed harcoded version number in rootcmd with goreleaser, added stdin…
Browse files Browse the repository at this point in the history
… file read for transcription; tests failing
  • Loading branch information
benleem committed Nov 12, 2024
1 parent 224f684 commit 80a1f9a
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 77 deletions.
2 changes: 2 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ builds:
goarch: 386
- goos: windows
goarch: arm64
ldflags:
- -s -w -X cmd.version={{.Version}} -X cmd.commit={{.Commit}} -X cmd.date={{.Date}} -X cmd.builtBy=goreleaser

universal_binaries:
- replace: true
Expand Down
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
local:
goreleaser release --snapshot --clean
goreleaser release --snapshot --clean
test-stdin:
go test ./cmd -run TestStdin -count=1 -v
test-fp:
go test ./cmd -run TestFp -count=1 -v
2 changes: 1 addition & 1 deletion cmd/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var Confirm bool

func init() {
cleanCmd.Flags().BoolVarP(&Confirm, "confirm", "y", false, "skips confirmation prompt")
rootCmd.AddCommand(cleanCmd)
RootCmd.AddCommand(cleanCmd)
}

var cleanCmd = &cobra.Command{
Expand Down
4 changes: 2 additions & 2 deletions cmd/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
)

func init() {
rootCmd.AddCommand(compressCommand)
rootCmd.AddCommand(decompressCommand)
RootCmd.AddCommand(compressCommand)
RootCmd.AddCommand(decompressCommand)
}

var compressCommand = &cobra.Command{
Expand Down
2 changes: 1 addition & 1 deletion cmd/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func init() {
rootCmd.AddCommand(prepareCmd)
RootCmd.AddCommand(prepareCmd)
}

var prepareCmd = &cobra.Command{
Expand Down
2 changes: 1 addition & 1 deletion cmd/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func init() {
rootCmd.AddCommand(reportCommand)
RootCmd.AddCommand(reportCommand)
}

// ripped straight from stack overflow: https://stackoverflow.com/questions/32482673/how-to-get-directory-total-size
Expand Down
12 changes: 9 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@ import (
"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
var (
version = "dev"
// commit = "none"
// date = "unknown"
)

var RootCmd = &cobra.Command{
Use: "prattl",
Short: "Prattl is a transcription tool",
Long: "A transcription tool built with Go and Python.\nComplete documentation is available at https://github.com/prattlOrg/prattl",
Version: "v0.3.0",
Version: version,
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
if err := RootCmd.Execute(); err != nil {
os.Exit(1)
}
}
148 changes: 80 additions & 68 deletions cmd/transcribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package cmd
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"strings"

Expand All @@ -14,7 +17,7 @@ import (
)

func init() {
rootCmd.AddCommand(transcribeCmd)
RootCmd.AddCommand(transcribeCmd)
}

var transcribeCmd = &cobra.Command{
Expand All @@ -32,82 +35,70 @@ var transcribeCmd = &cobra.Command{
if err != nil {
return err
}
fmt.Println(fileBytes)
fmt.Println(string(fileBytes))
err = transcribeStdin(fileBytes)
if err != nil {
return err
}
} else {
if len(args) == 0 {
return fmt.Errorf("requires at least 1 arg(s), only received 0")
}
fmt.Println("using filepath")
err = transcribeFp(args...)
if err != nil {
return err
}
}

return nil
// fmt.Printf("Transcribing...")
// transcriptionMap := make(map[string]string)
// transcriptions, err := transcribe(args)
// if err != nil {
// return err
// }

// for i, trans := range transcriptions {
// transcriptionMap[args[i]] = trans
// // fmt.Printf("%v\n", trans)
// // _, err := io.WriteString(os.Stdout, trans+"\n")
// // if err != nil {
// // return fmt.Errorf("error writing to stdout: %v", err)
// // }
// }

// jsonOutput, err := json.Marshal(transcriptionMap)
// if err != nil {
// return fmt.Errorf("error marshaling to JSON: %v", err)
// }

// clearLine()
// _, err = io.WriteString(os.Stdout, string(jsonOutput)+"\n")
// if err != nil {
// return fmt.Errorf("error writing to stdout: %v", err)
// }

// return nil
},
}

func checkStdin() (bool, error) {
fileStat, err := os.Stdin.Stat()
if err != nil {
return false, fmt.Errorf("getting stdin stat failed: %v", err)
}
// check if stdin is pipe
if fileStat.Mode()&os.ModeNamedPipe == 0 {
return false, nil
} else {
return true, nil
}
// fileStat, err := os.Stdin.Stat()
// if err != nil {
// return false, fmt.Errorf("getting stdin stat failed: %v", err)
// }
// // check if stdin is pipe
// return fileStat.Mode()&os.ModeNamedPipe != 0, nil
return true, nil
}

func readStdin() ([]byte, error) {
scanner := bufio.NewScanner(os.Stdin)
scannerBytes := make([]byte, 0)
for scanner.Scan() {
if len(scanner.Bytes()) == 0 {
return nil, fmt.Errorf("stdin is empty")
reader := bufio.NewReader(os.Stdin)
var fileBytes []byte
for {
b, err := reader.ReadByte()
if err != nil && !errors.Is(err, io.EOF) {
return nil, fmt.Errorf("failed to read file byte: %v", err)
}
scannerBytes = append(scannerBytes, scanner.Bytes()...)
}
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("reading from stdin failed: %v", err)
// process the one byte b
if err != nil {
// end of file
break
}
fileBytes = append(fileBytes, b)
}
return scannerBytes, nil
return fileBytes, nil
}

func transcribe(fps []string) ([]string, error) {
returnStrings := []string{}
func transcribeStdin(fileBytes []byte) error {
fmt.Fprintln(os.Stderr, "Transcribing..")
transcription, err := transcribe(fileBytes)
if err != nil {
return err
}
clearLine()
fmt.Println(transcription[0])
return nil
}

func transcribeFp(fps ...string) error {
fmt.Fprintln(os.Stderr, "Transcribing..")
var allBytes []byte
for i, fp := range fps {
fileBytes, err := os.ReadFile(fp)
if err != nil {
return returnStrings, fmt.Errorf("error reading file: %v", err)
return fmt.Errorf("error reading file: %v", err)
}
allBytes = append(allBytes, fileBytes...)

Expand All @@ -116,44 +107,65 @@ func transcribe(fps []string) ([]string, error) {
}

}
transcriptionMap := make(map[string]string)
transcriptions, err := transcribe(allBytes)
if err != nil {
return err
}
for i, trans := range transcriptions {
transcriptionMap[fps[i]] = trans
}
jsonOutput, err := json.Marshal(transcriptionMap)
if err != nil {
return fmt.Errorf("marshaling to JSON failed: %v", err)
}
clearLine()
_, err = io.WriteString(os.Stdout, string(jsonOutput)+"\n")
if err != nil {
return fmt.Errorf("writing to stdout failed: %v", err)
}
return nil
}

func transcribe(file []byte) ([]string, error) {
fmt.Println(file)
program, err := pysrc.ReturnFile("transcribe.py")
if err != nil {
return returnStrings, err
return nil, err
}

env, err := pysrc.GetPrattlEnv()
if err != nil {
return returnStrings, err
return nil, err
}

cmd, err := env.ExecutePython("-c", program)
if err != nil {
return returnStrings, err
return nil, err
}

var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
stdin, err := cmd.StdinPipe()
if err != nil {
return returnStrings, fmt.Errorf("error instantiating pipe: %v", err)
return nil, fmt.Errorf("error instantiating pipe: %v", stderr.String())
}
cmd.Stdout = &out
cmd.Stderr = &stderr
if err = cmd.Start(); err != nil {
return returnStrings, fmt.Errorf("error starting command: %v", err)
return nil, fmt.Errorf("error starting command: %v", stderr.String())
}
_, err = stdin.Write(allBytes)
_, err = stdin.Write(file)
if err != nil {
return returnStrings, fmt.Errorf("error writing to stdin: %v", err)
return nil, fmt.Errorf("error writing to stdin: %v", stderr.String())
}
stdin.Close()
if err = cmd.Wait(); err != nil {
return returnStrings, fmt.Errorf("error waiting for command: %v", err)
return nil, fmt.Errorf("error waiting for command: %v", stderr.String())
}

output := out.String()
// fmt.Println(output)

returnStrings = strings.Split(strings.ToLower(output), embed.SeparatorExpectedString)
returnStrings := strings.Split(strings.ToLower(output), embed.SeparatorExpectedString)

for _, str := range returnStrings {
str = fmt.Sprintf("---%s---\n", str)
Expand Down
39 changes: 39 additions & 0 deletions cmd/transcribe_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cmd_test

import (
"bytes"
"os"
"path/filepath"
"testing"

"github.com/prattlOrg/prattl/cmd"
)

var audio00 = filepath.Clean("../test_audio/test.mp3")
var audio01 = filepath.Clean("../test_audio/transcribing_1.mp3")

func TestStdin(t *testing.T) {
dat, err := os.ReadFile(audio00)
if err != nil {
t.Fatal(err)
}
// t.Log(dat)

root := cmd.RootCmd
in := bytes.NewBuffer(dat)
root.SetIn(in)
root.SetArgs([]string{"transcribe"})
err = root.Execute()
if err != nil {
t.FailNow()
}
}

func TestFp(t *testing.T) {
root := cmd.RootCmd
root.SetArgs([]string{"transcribe", audio00, audio01})
err := root.Execute()
if err != nil {
t.FailNow()
}
}

0 comments on commit 80a1f9a

Please sign in to comment.