Skip to content

Commit

Permalink
Fix lints
Browse files Browse the repository at this point in the history
  • Loading branch information
sgerhardt committed Jul 24, 2024
1 parent e346922 commit 5954dc5
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 47 deletions.
2 changes: 1 addition & 1 deletion cmd/chatter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func main() {
if err := setup.RootCmd.Execute(); err != nil {
if err := setup.NewRootCmd().Execute(); err != nil {
log.Fatal(err)
}
}
4 changes: 3 additions & 1 deletion internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ func (c *ElevenLabs) ProcessSite() error {
return tErr
}
_, err = c.write(fromText)
return err
if err != nil {
return err
}
}
return nil
}
Expand Down
77 changes: 39 additions & 38 deletions internal/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,55 +15,56 @@ import (
"time"
)

var (
voiceID string
textInput string
siteInput string
)
func NewRootCmd() *cobra.Command {

var voiceID string
var textInput string
var siteInput string

var RootCmd = &cobra.Command{
Use: "chatter -v <voiceID> {-t <text> | -s <url>}",
Short: "An Eleven Labs client for text to voice",
Long: `Chatter is a command-line client for Eleven Labs text-to-voice service.
cmd := &cobra.Command{
Use: "chatter -v <voiceID> {-t <text> | -s <url>}",
Short: "An Eleven Labs client for text to voice",
Long: `Chatter is a command-line client for Eleven Labs text-to-voice service.
Usage:
chatter -v <voiceID> -t <text> (Provide text to convert to voice)
chatter -v <voiceID> -s <url> (Provide a URL to read text from)
Either --text or --site is required, but not both.`,
PreRunE: func(cmd *cobra.Command, args []string) error {
if voiceID == "" {
return errors.New("voice is required")
}
if textInput == "" && siteInput == "" {
PreRunE: func(_ *cobra.Command, _ []string) error {
if voiceID == "" {
return errors.New("voice is required")
}
if textInput == "" && siteInput == "" {
return errors.New("text or site is required")
}
if textInput != "" && siteInput != "" {
return errors.New("only one of text or site can be provided")
}
return nil
},
RunE: func(_ *cobra.Command, _ []string) error {
cfg, c, err := New(".env", voiceID, textInput, siteInput)
if err != nil {
return err
}
if textInput != "" {
return client.New(cfg, c).ProcessText()
} else if siteInput != "" {
return client.New(cfg, c).ProcessSite()
}
return errors.New("text or site is required")
}
if textInput != "" && siteInput != "" {
return errors.New("only one of text or site can be provided")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
cfg, c, err := New(".env", voiceID, textInput, siteInput)
if err != nil {
return err
}
if textInput != "" {
return client.New(cfg, c).ProcessText()
} else if siteInput != "" {
return client.New(cfg, c).ProcessSite()
}
return errors.New("text or site is required")
},
}
},
}

func init() {
RootCmd.Flags().StringVarP(&textInput, "text", "t", "", "Text to convert to voice")
RootCmd.Flags().StringVarP(&siteInput, "site", "s", "", "Website to read text from!!")
RootCmd.Flags().StringVarP(&voiceID, "voice", "v", "", "Voice ID to use")
if err := RootCmd.MarkFlagRequired("voice"); err != nil {
cmd.Flags().StringVarP(&textInput, "text", "t", "", "Text to convert to voice")
cmd.Flags().StringVarP(&siteInput, "site", "s", "", "Website to read text from")
cmd.Flags().StringVarP(&voiceID, "voice", "v", "", "Voice ID to use")
if err := cmd.MarkFlagRequired("voice"); err != nil {
log.Fatal(err)
}

return cmd
}

func readEnvFile(filename string) (string, string, error) {
Expand Down
18 changes: 11 additions & 7 deletions internal/setup/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"testing"
)

func TestRootCmdErrors(t *testing.T) {
t.Parallel()
func TestRootCmdErrors(t *testing.T) { // nolint:paralleltest
// Save the original command-line arguments and restore them after the test
originalArgs := os.Args

tests := []struct {
name string
args []string
Expand All @@ -32,17 +34,19 @@ func TestRootCmdErrors(t *testing.T) {
},
}

for _, tt := range tests {
// nolint:paralleltest
for _, tt := range tests { // nolint:paralleltest
t.Run(tt.name, func(t *testing.T) {
t.Cleanup(func() { os.Args = originalArgs })

// Set the command-line arguments for the test
os.Args = tt.args
os.Args = append([]string{"chatter"}, tt.args...)

// Execute the command
err := RootCmd.Execute()
err := NewRootCmd().Execute()

// Verify the error
assert.Error(t, err)
assert.ErrorContains(t, err, tt.errorMsg)
assert.Contains(t, err.Error(), tt.errorMsg)
})
}
}
Expand Down

0 comments on commit 5954dc5

Please sign in to comment.