Skip to content

Commit

Permalink
TestStdin passing
Browse files Browse the repository at this point in the history
  • Loading branch information
benleem committed Nov 16, 2024
1 parent 80a1f9a commit 6684e70
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 16 deletions.
42 changes: 41 additions & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ builds:
ldflags:
- -s -w -X cmd.version={{.Version}} -X cmd.commit={{.Commit}} -X cmd.date={{.Date}} -X cmd.builtBy=goreleaser

# binary_signs:
# - {}

# signs:
# - artifacts: checksum

universal_binaries:
- replace: true

Expand Down Expand Up @@ -68,7 +74,7 @@ brews:
chocolateys:
- title: prattl
authors: Ben Marshall, Ezra Klitsie
project_url: https://github.com/prattlOrg/prattl
project_url: https://prattl.co/
url_template: "https://github.com/prattlOrg/prattl/releases/download/{{ .Tag }}/{{ .ArtifactName }}"
copyright: 2024 Prattl Org
package_source_url: https://github.com/prattlOrg/prattl
Expand All @@ -90,8 +96,42 @@ chocolateys:
source_repo: "https://push.chocolatey.org/"
skip_publish: false

nfpms:
- package_name: prattl
file_name_template: >-
{{ .ProjectName }}_{{ .Os }}_
{{- if eq .Arch "amd64" }}x86_64
{{- else if eq .Arch "386" }}i386
{{- else }}{{ .Arch }}{{ end }}
{{- if .Arm }}{{ .Arm }}{{ end }}
vendor: Prattl Org.
homepage: https://prattl.co/
maintainer: Benjamin Marshall <[email protected]>
description: |-
Prattl installer package.
CLI tool for transcribing audio to text.
license: MIT
formats:
- apk
- deb
- rpm
- termux.deb
- archlinux
dependencies:
- ffmpeg
# Changelog YAML file, see: https://github.com/goreleaser/chglog
#
# You can use goreleaser/chglog to create the changelog for your project,
# pass that changelog yaml file to GoReleaser,
# and it should in turn setup it accordingly for the given available
# formats (deb and rpm at the moment).
#
# Experimental.
# changelog: changelog.yaml

changelog:
sort: asc
use: github
filters:
exclude:
- "^docs:"
Expand Down
24 changes: 13 additions & 11 deletions cmd/transcribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,22 @@ var transcribeCmd = &cobra.Command{
return 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)
// }
fileStat, err := os.Stdin.Stat()
if err != nil {
return false, fmt.Errorf("getting stdin stat failed: %v", err)
}
if fileStat.Size() == 0 {
return false, nil
}
return true, nil

// // check if stdin is pipe
// return fileStat.Mode()&os.ModeNamedPipe != 0, nil
return true, nil
}

func readStdin() ([]byte, error) {
Expand All @@ -82,18 +85,15 @@ func readStdin() ([]byte, error) {
}

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)
Expand All @@ -119,7 +119,6 @@ func transcribeFp(fps ...string) error {
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)
Expand All @@ -128,7 +127,7 @@ func transcribeFp(fps ...string) error {
}

func transcribe(file []byte) ([]string, error) {
fmt.Println(file)
fmt.Fprintln(os.Stderr, "Transcribing..")
program, err := pysrc.ReturnFile("transcribe.py")
if err != nil {
return nil, err
Expand Down Expand Up @@ -171,6 +170,9 @@ func transcribe(file []byte) ([]string, error) {
str = fmt.Sprintf("---%s---\n", str)
}

// not working frfr
clearLine()

return returnStrings, nil
}

Expand Down
24 changes: 20 additions & 4 deletions cmd/transcribe_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd_test

import (
"bytes"
"os"
"path/filepath"
"testing"
Expand All @@ -17,16 +16,33 @@ func TestStdin(t *testing.T) {
if err != nil {
t.Fatal(err)
}
// t.Log(dat)

tmpfile, err := os.CreateTemp("../test_audio", "*.mp3")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name()) // clean up
if _, err := tmpfile.Write(dat); err != nil {
t.Fatal(err)
}
if _, err := tmpfile.Seek(0, 0); err != nil {
t.Fatal(err)
}
oldStdin := os.Stdin
defer func() { os.Stdin = oldStdin }() // Restore original Stdin
os.Stdin = tmpfile

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

if err := tmpfile.Close(); err != nil {
t.Fatal(err)
}
}

func TestFp(t *testing.T) {
Expand Down

0 comments on commit 6684e70

Please sign in to comment.