Skip to content

Commit

Permalink
start refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
C-Loftus committed Jan 26, 2025
1 parent 09134b7 commit a158403
Show file tree
Hide file tree
Showing 17 changed files with 174 additions and 370 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/go_lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Go Lint

on: [push]

jobs:
golangci:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Run Golangci-lint
uses: golangci/[email protected]
11 changes: 9 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ module QuickPiperAudiobook

go 1.22.5

require github.com/alecthomas/kong v0.9.0
require (
github.com/alecthomas/kong v0.9.0
github.com/stretchr/testify v1.8.1
)

require gopkg.in/yaml.v3 v3.0.1 // indirect
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require (
github.com/alecthomas/kong-yaml v0.2.0
Expand Down
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
github.com/briandowns/spinner v1.23.1 h1:t5fDPmScwUjozhDj4FA46p5acZWIPXYE30qW2Ptu650=
github.com/briandowns/spinner v1.23.1/go.mod h1:LaZeM4wm2Ywy6vO571mvhQNRcWfRUnXOs0RcKV0wYKM=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
github.com/gen2brain/beeep v0.0.0-20240516210008-9c006672e7f4 h1:ygs9POGDQpQGLJPlq4+0LBUmMBNox1N4JSpw+OETcvI=
Expand All @@ -25,6 +28,15 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d h1:VhgPp6v9qf9Agr/56bj7Y/xa04UccTW04VP0Qed4vnQ=
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d/go.mod h1:YUTz3bUH2ZwIWBy3CJBeOBEugqcmXREj14T+iG/4k4U=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af h1:6yITBqGTE2lEeTPG04SN9W+iWHCRyHqlVYILiSXziwk=
github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af/go.mod h1:4F09kP5F+am0jAwlQLddpoMDM+iewkxxt6nxUQ5nq5o=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand All @@ -36,5 +48,6 @@ golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.1.0 h1:g6Z6vPFA9dYBAF7DWcH6sCcOntplXsDKcliusYijMlw=
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
50 changes: 50 additions & 0 deletions lib/binarymanagers/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package binarymanagers

import (
"fmt"
"io"
"os/exec"
)

type PipedOutput struct {
Handle *exec.Cmd
Stdout io.ReadCloser
Stderr io.ReadCloser
}

func RunPiped(cmd string, pipedInput io.Reader) (PipedOutput, error) {

fullCmd := exec.Command("sh", "-c", cmd)

stdout, err := fullCmd.StdoutPipe()
if err != nil {
return PipedOutput{}, fmt.Errorf("failed getting stdout: %v", err)
}

stderr, err := fullCmd.StderrPipe()
if err != nil {
return PipedOutput{}, fmt.Errorf("failed getting stderr: %v", err)
}

if pipedInput != nil {
fullCmd.Stdin = pipedInput
}

if err := fullCmd.Start(); err != nil {
return PipedOutput{}, fmt.Errorf("command failed: %v", err)
}

return PipedOutput{Handle: fullCmd, Stdout: stdout, Stderr: stderr}, nil
}

func Run(cmd string) (string, error) {

fullCmd := exec.Command("sh", "-c", cmd)

outputBytes, err := fullCmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("command failed: %v", err)
}
return string(outputBytes), nil

}
40 changes: 40 additions & 0 deletions lib/binarymanagers/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package binarymanagers

import (
"bytes"
"io"
"testing"

"github.com/stretchr/testify/require"
)

func TestRun(t *testing.T) {
output, err := Run("echo hello")
require.NoError(t, err)
require.Equal(t, "hello\n", output)
}

func FuzzRunPiped(f *testing.F) {
// Test echo to cat
f.Fuzz(func(t *testing.T, message string) {
echoCmd, err := RunPiped("echo "+message, nil)
require.NoError(t, err)

catCmd, err := RunPiped("cat", echoCmd.Stdout)
require.NoError(t, err)

catCmd2, err := RunPiped("cat", catCmd.Stdout)
require.NoError(t, err)

// Properly read from the result.Stdout before calling Wait
var out bytes.Buffer
_, err = io.Copy(&out, catCmd2.Stdout)
require.NoError(t, err)

// We only need to wait on the last command
require.NoError(t, catCmd2.Handle.Wait())

// Assert the final output
require.Equal(t, message+"\n", out.String())
})
}
37 changes: 37 additions & 0 deletions lib/binarymanagers/ffmpeg/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package ffmpeg

import (
"fmt"
"os"
"os/exec"
)

type FfmpegClient struct {
binary string
}

func NewFFmpegClient() (*FfmpegClient, error) {

// look for ffmpeg in path
if _, err := os.Stat("ffmpeg"); os.IsNotExist(err) {
return &FfmpegClient{binary: "ffmpeg"}, nil
} else {
return &FfmpegClient{}, fmt.Errorf("ffmpeg binary not found: %v", err)
}

}

func (c *FfmpegClient) Run(cmd String) error {
// construct the command
cmdList := fmt.Sprintf("%s %s", c.binary, cmd)

// run the command
fullCmd := exec.Command("sh", "-c", cmdList)
output, err := fullCmd.StdoutPipe()

if err != nil {
return fmt.Errorf("ffmpeg command failed: %v\n%s", err, string(output))
}

return nil
}
13 changes: 13 additions & 0 deletions lib/binarymanagers/ffmpeg/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ffmpeg

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestNewFFmpegClient(t *testing.T) {
client, err := NewFFmpegClient()
require.NotNil(t, client)
require.NoError(t, err)
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
43 changes: 0 additions & 43 deletions lib/models.go

This file was deleted.

51 changes: 0 additions & 51 deletions lib/models_test.go

This file was deleted.

Loading

0 comments on commit a158403

Please sign in to comment.