diff --git a/Cli/promptui.go b/Cli/promptui.go new file mode 100644 index 0000000..c8be470 --- /dev/null +++ b/Cli/promptui.go @@ -0,0 +1,29 @@ +package cli + +import ( + "errors" + + "github.com/manifoldco/promptui" +) + +func GetUserInput(label string) (*string, error) { + if label == "" { + return nil, errors.New("label cannot be empty") + } + + prompt := promptui.Prompt{ + Label: label, + } + + animeName, err := prompt.Run() + + if err != nil { + return nil, err + } + + if animeName == "" { + return nil, errors.New("user input cannot be empty") + } + + return &animeName, nil +} diff --git a/go.mod b/go.mod index 777c684..ff6e51e 100644 --- a/go.mod +++ b/go.mod @@ -2,10 +2,16 @@ module Animatic go 1.22.1 -require github.com/stretchr/testify v1.9.0 +require ( + github.com/manifoldco/promptui v0.9.0 + github.com/stretchr/testify v1.9.0 +) require ( + github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/objx v0.5.2 // indirect + golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 60ce688..4f5908b 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,21 @@ +github.com/chzyer/logex v1.1.10 h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE= +github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= +github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8= +github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= +github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8= +github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= 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/manifoldco/promptui v0.9.0 h1:3V4HzJk1TtXW1MTZMP7mdlwbBpIinw3HztaIlYthEiA= +github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg= 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.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b h1:MQE+LT/ABUuuvEZ+YQAMSXindAdUh7slEmAkup74op4= +golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/main.go b/main.go index 20f0d0f..950bcb0 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,16 @@ package main import ( - "fmt" + cli "Animatic/Cli" + "log" ) func main() { - fmt.Println("Hello World!") + animeName, err := cli.GetUserInput("Enter the name of the anime you want to download") + + if err != nil { + log.Println("Occurred an unknown error: ", err.Error()) + } + + log.Println(*animeName) } diff --git a/tests/cli/promptui_test.go b/tests/cli/promptui_test.go new file mode 100644 index 0000000..810b526 --- /dev/null +++ b/tests/cli/promptui_test.go @@ -0,0 +1,52 @@ +package cli + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" + + Cli "Animatic/Cli" +) + +type MockCli struct { + mock.Mock +} + +func (m *MockCli) GetUserInput(label string) (string, error) { + args := m.Called(label) + return args.String(0), args.Error(1) +} + +func TestGetUserInput_ValidInput(t *testing.T) { + mockCli := new(MockCli) + + input := "Test Anime" + label := "Anime Name" + mockCli.On("GetUserInput", label).Return(input, nil) + + result, err := mockCli.GetUserInput(label) + + require.NoError(t, err) + + assert.Equal(t, input, result) +} + +func TestGetUserInput(t *testing.T) { + t.Run("empty label", func(t *testing.T) { + label := "" + + _, err := Cli.GetUserInput(label) + + assert.Error(t, err) + }) + + t.Run("empty input", func(t *testing.T) { + label := "Anime Name" + + _, err := Cli.GetUserInput(label) + + assert.Error(t, err) + }) +}