From 6684e7028c4263cf65f35eea9a2dfe603576e1ca Mon Sep 17 00:00:00 2001 From: Ben Marshall Date: Sat, 16 Nov 2024 15:45:02 -0500 Subject: [PATCH] TestStdin passing --- .goreleaser.yaml | 42 +++++++++++++++++++++++++++++++++++++++++- cmd/transcribe.go | 24 +++++++++++++----------- cmd/transcribe_test.go | 24 ++++++++++++++++++++---- 3 files changed, 74 insertions(+), 16 deletions(-) diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 1b12e30a..3998ba64 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -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 @@ -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 @@ -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 + 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:" diff --git a/cmd/transcribe.go b/cmd/transcribe.go index a3ecaac1..510ec0ac 100644 --- a/cmd/transcribe.go +++ b/cmd/transcribe.go @@ -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) { @@ -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) @@ -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) @@ -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 @@ -171,6 +170,9 @@ func transcribe(file []byte) ([]string, error) { str = fmt.Sprintf("---%s---\n", str) } + // not working frfr + clearLine() + return returnStrings, nil } diff --git a/cmd/transcribe_test.go b/cmd/transcribe_test.go index 0d381bc8..c2cb8f37 100644 --- a/cmd/transcribe_test.go +++ b/cmd/transcribe_test.go @@ -1,7 +1,6 @@ package cmd_test import ( - "bytes" "os" "path/filepath" "testing" @@ -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) {