From 846d260cb6fbeed10b43b25810f9de59f6c56e69 Mon Sep 17 00:00:00 2001 From: Takumi Kanada Date: Thu, 24 Nov 2022 00:44:40 +0900 Subject: [PATCH] Update --- README.md | 2 +- cmd/deepl-cli/main.go | 4 +- go.mod | 12 +- go.sum | 39 ++++++- internal/cmd/configure.go | 14 +++ internal/cmd/repl.go | 31 +++++ internal/cmd/root.go | 106 +++++------------- internal/cmd/root_test.go | 58 ++++++---- internal/cmd/subcmd/setup/setup.go | 88 --------------- internal/cmd/subcmd/setup/setup_test.go | 41 ------- internal/config/config.go | 16 +-- internal/config/config_test.go | 26 ++--- internal/controller/configure.go | 61 ++++++++++ internal/controller/repl.go | 85 ++++++-------- internal/deepl/deepl.go | 38 +++++-- internal/deepl/translate.go | 40 ++++--- internal/deepl/translate_test.go | 16 ++- internal/deepl/webapi.go | 17 --- internal/entity/translation.go | 32 ------ internal/entity/translation_test.go | 15 --- internal/model/translate.go | 8 ++ .../mock_repository}/translate.go | 20 ++-- internal/repository/translate.go | 8 ++ internal/usecase/translate.go | 20 ---- internal/webapi/translate.go | 8 -- 25 files changed, 369 insertions(+), 436 deletions(-) create mode 100644 internal/cmd/configure.go create mode 100644 internal/cmd/repl.go delete mode 100644 internal/cmd/subcmd/setup/setup.go delete mode 100644 internal/cmd/subcmd/setup/setup_test.go create mode 100644 internal/controller/configure.go delete mode 100644 internal/deepl/webapi.go delete mode 100644 internal/entity/translation.go delete mode 100644 internal/entity/translation_test.go create mode 100644 internal/model/translate.go rename internal/{webapi/mock_webapi => repository/mock_repository}/translate.go (59%) create mode 100644 internal/repository/translate.go delete mode 100644 internal/usecase/translate.go delete mode 100644 internal/webapi/translate.go diff --git a/README.md b/README.md index 64f1e4b..3ffd8ec 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ go install: ```shell -go install github.com/candy12t/deepl-cli/cmd/deepl-cli@v0.3.0 +go install github.com/candy12t/deepl-cli/cmd/deepl-cli@v0.4.0 ``` ## setup diff --git a/cmd/deepl-cli/main.go b/cmd/deepl-cli/main.go index fdb2a32..6bde42c 100644 --- a/cmd/deepl-cli/main.go +++ b/cmd/deepl-cli/main.go @@ -9,7 +9,7 @@ import ( func main() { conf := config.NewDeepLCLIConfig() - cli := cmd.NewCLI(os.Stdin, os.Stdout, os.Stderr, conf) - code := cli.Run(os.Args) + cli := cmd.NewCLI(os.Stdin, os.Stdout, os.Stderr) + code := cli.Run(os.Args, conf) os.Exit(int(code)) } diff --git a/go.mod b/go.mod index 2ecbb3a..6665bd6 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/candy12t/deepl-cli go 1.17 require ( + github.com/AlecAivazis/survey/v2 v2.3.6 github.com/golang/mock v1.6.0 github.com/stretchr/testify v1.7.1 github.com/urfave/cli/v2 v2.3.0 @@ -11,8 +12,17 @@ require ( require ( github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect - github.com/davecgh/go-spew v1.1.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect + github.com/kr/pretty v0.1.0 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.16 // indirect + github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect + golang.org/x/sys v0.2.0 // indirect + golang.org/x/term v0.0.0-20210503060354-a79de5458b56 // indirect + golang.org/x/text v0.3.3 // indirect + gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect ) diff --git a/go.sum b/go.sum index 1d1e002..3448580 100644 --- a/go.sum +++ b/go.sum @@ -1,11 +1,36 @@ +github.com/AlecAivazis/survey/v2 v2.3.6 h1:NvTuVHISgTHEHeBFqt6BHOe4Ny/NwGZr7w+F8S9ziyw= +github.com/AlecAivazis/survey/v2 v2.3.6/go.mod h1:4AuI9b7RjAR+G7v9+C4YSlX/YL3K3cWNXgWXOhllqvI= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63nhn5WAunQHLTznkw5W8b1Xc0dNjp83s= +github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2/go.mod h1:HBCaDeC1lPdgDeDbhX8XFpy1jqjK0IBG8W5K+xYqA0w= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/cpuguy83/go-md2man/v2 v2.0.1 h1:r/myEWzV9lfsM1tFLgDyu0atFtJ1fXn261LKYj/3DxU= github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/creack/pty v1.1.17 h1:QeVUsEDNrLBW4tMgZHvxy18sKtr6VI492kBhUfhDJNI= +github.com/creack/pty v1.1.17/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4= 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/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc= github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs= +github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog= +github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec/go.mod h1:Q48J4R4DvxnHolD5P8pOtXigYlRuPLGl6moFx3ulM68= +github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs= +github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= +github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d h1:5PJl274Y63IEHC+7izoQE9x6ikvDFZS2mDVS3drnohI= +github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= 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/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= @@ -13,6 +38,7 @@ github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M= @@ -27,12 +53,20 @@ golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96b golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A= +golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210503060354-a79de5458b56 h1:b8jxX3zqjpqb2LklXPzKSGJhzyxCOZSz8ncv8Nv+y7w= +golang.org/x/term v0.0.0-20210503060354-a79de5458b56/go.mod h1:tfny5GFUkzUvx4ps4ajbZsCe5lw1metzhBm9T3x7oIY= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= @@ -40,8 +74,9 @@ golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -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/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= diff --git a/internal/cmd/configure.go b/internal/cmd/configure.go new file mode 100644 index 0000000..e51bcc2 --- /dev/null +++ b/internal/cmd/configure.go @@ -0,0 +1,14 @@ +package cmd + +import ( + "github.com/candy12t/deepl-cli/internal/controller" + "github.com/urfave/cli/v2" +) + +func NewCmdConfigure() *cli.Command { + return &cli.Command{ + Name: "configure", + Usage: "Configure deepl-cli options.", + Action: controller.ConfigureAction, + } +} diff --git a/internal/cmd/repl.go b/internal/cmd/repl.go new file mode 100644 index 0000000..2bce51d --- /dev/null +++ b/internal/cmd/repl.go @@ -0,0 +1,31 @@ +package cmd + +import ( + "github.com/candy12t/deepl-cli/internal/config" + "github.com/candy12t/deepl-cli/internal/controller" + "github.com/candy12t/deepl-cli/internal/deepl" + "github.com/urfave/cli/v2" +) + +func NewCmdRepl(conf *config.DeepLCLIConfig) *cli.Command { + client := deepl.NewClient(conf.Credential.DeepLAuthKey) + + return &cli.Command{ + Name: "repl", + Usage: "Translate with REPL.", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "source", + Aliases: []string{"s"}, + Value: conf.DefaultLanguage.Source, + }, + &cli.StringFlag{ + Name: "target", + Aliases: []string{"t"}, + Value: conf.DefaultLanguage.Target, + }, + }, + Before: CheckAuthKeyAction(conf.Credential.DeepLAuthKey), + Action: controller.ReplAction(client), + } +} diff --git a/internal/cmd/root.go b/internal/cmd/root.go index ffe1f51..0b59c3a 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -5,11 +5,7 @@ import ( "io" "github.com/candy12t/deepl-cli/internal/build" - "github.com/candy12t/deepl-cli/internal/cmd/subcmd/setup" "github.com/candy12t/deepl-cli/internal/config" - "github.com/candy12t/deepl-cli/internal/controller" - "github.com/candy12t/deepl-cli/internal/deepl" - "github.com/candy12t/deepl-cli/internal/usecase" "github.com/urfave/cli/v2" ) @@ -21,104 +17,62 @@ const ( ) type CLI struct { - InStream io.Reader - OutStream io.Writer - ErrStream io.Writer - conf *config.DeepLCLIConfig + Reader io.Reader + Writer io.Writer + ErrWriter io.Writer } -func NewCLI(inStream io.Reader, outStream, errStream io.Writer, conf *config.DeepLCLIConfig) *CLI { +func NewCLI(reader io.Reader, writer, errWriter io.Writer) *CLI { return &CLI{ - InStream: inStream, - OutStream: outStream, - ErrStream: errStream, - conf: conf, + Reader: reader, + Writer: writer, + ErrWriter: errWriter, } } -func (c *CLI) Run(args []string) exitCode { - var sourceLang, targetLang string - defaultSourceLanguage, defaultTargetLanguage := c.conf.DefaultLanguage.SourceLanguage, c.conf.DefaultLanguage.TargetLanguage - +func (c *CLI) Run(args []string, conf *config.DeepLCLIConfig) exitCode { cli.VersionFlag = &cli.BoolFlag{ Name: "version", Aliases: []string{"v"}, Usage: "show deepl-cli version", } + cli.HelpFlag = &cli.BoolFlag{ + Name: "help", + Aliases: []string{"h"}, + Usage: "Show help for command", + } + app := &cli.App{ - Name: "deepl-cli", - Usage: "unofficial DeepL command line tool", - Version: build.Version, - Reader: c.InStream, - Writer: c.OutStream, - ErrWriter: c.ErrStream, - EnableBashCompletion: true, - HideHelp: true, - Flags: []cli.Flag{ - &cli.BoolFlag{ - Name: "help", - Aliases: []string{"h"}, - Usage: "Show help for command", - }, - }, + Name: "deepl-cli", + Usage: "unofficial DeepL command line tool.", + Version: build.Version, + Reader: c.Reader, + Writer: c.Writer, + ErrWriter: c.ErrWriter, + HideHelpCommand: true, CommandNotFound: func(ctx *cli.Context, command string) { fmt.Fprintf(ctx.App.Writer, "unknown command %q for %q\n", command, "deepl-cli") }, Commands: []*cli.Command{ - { - Name: "setup", - Usage: "Setup for using this cli", - Action: func(ctx *cli.Context) error { - err := setup.Setup(ctx.App.Reader, ctx.App.Writer) - if err != nil { - return err - } - return nil - }, - }, - { - Name: "repl", - Usage: "Translate with REPL", - Flags: []cli.Flag{ - &cli.StringFlag{ - Name: "source", - Aliases: []string{"s"}, - Value: defaultSourceLanguage, - Destination: &sourceLang, - }, - &cli.StringFlag{ - Name: "target", - Aliases: []string{"t"}, - Value: defaultTargetLanguage, - Destination: &targetLang, - }, - }, - Action: func(ctx *cli.Context) error { - if err := c.checkAuthKey(); err != nil { - return err - } - deeplCli := deepl.NewClient(c.conf.Auth.AuthKey) - uc := usecase.NewTranslation(deeplCli) - replCtrl := controller.NewRepl(uc, sourceLang, targetLang, ctx.App.Reader, ctx.App.Writer) - replCtrl.Apply() - return nil - }, - }, + NewCmdRepl(conf), + NewCmdConfigure(), }, } if err := app.Run(args); err != nil { - fmt.Fprintln(c.ErrStream, err) + fmt.Fprintln(app.ErrWriter, err) return exitErr } return exitOK } -func (c *CLI) checkAuthKey() error { - if len(c.conf.Auth.AuthKey) == 0 { - return fmt.Errorf("To setup, please run `deepl-cli setup`.") +func CheckAuthKeyAction(authKey string) cli.BeforeFunc { + return func(ctx *cli.Context) error { + if len(authKey) == 0 { + return fmt.Errorf("To get started with deepl-cli, please run: `deepl-cli configure`") + } + return nil } - return nil } diff --git a/internal/cmd/root_test.go b/internal/cmd/root_test.go index 0604493..6b96155 100644 --- a/internal/cmd/root_test.go +++ b/internal/cmd/root_test.go @@ -11,7 +11,7 @@ import ( "github.com/stretchr/testify/assert" ) -func TestRun(t *testing.T) { +func TestRun_Success(t *testing.T) { tests := []struct { name string args []string @@ -36,36 +36,52 @@ func TestRun(t *testing.T) { wantOut: fmt.Sprintf("unknown command %q for \"deepl-cli\"\n", "hoge"), wantExitCode: exitOK, }, - { - name: "repl", - args: strings.Split("deepl-cli repl", " "), - wantOut: fmt.Sprintf("Translate text from %s to %s\n>> ", "EN", "JA"), - wantExitCode: exitOK, - }, - { - name: "repl with options", - args: strings.Split("deepl-cli repl -s JA -t EN", " "), - wantOut: fmt.Sprintf("Translate text from %s to %s\n>> ", "JA", "EN"), - wantExitCode: exitOK, - }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { conf := &config.DeepLCLIConfig{ - Auth: config.Auth{ - AuthKey: "test-auth-key", + Credential: config.Credential{ + DeepLAuthKey: "test-auth-key", }, DefaultLanguage: config.DefaultLanguage{ - SourceLanguage: "EN", - TargetLanguage: "JA", + Source: "EN", + Target: "JA", }, } - inStream, outStream, errStream := new(bytes.Buffer), new(bytes.Buffer), new(bytes.Buffer) - cli := NewCLI(inStream, outStream, errStream, conf) - code := cli.Run(tt.args) + reader, writer, errWriter := new(bytes.Buffer), new(bytes.Buffer), new(bytes.Buffer) + cli := NewCLI(reader, writer, errWriter) + code := cli.Run(tt.args, conf) + + assert.Equal(t, tt.wantOut, writer.String()) + assert.Equal(t, tt.wantExitCode, code) + }) + } +} + +func TestRun_Failed(t *testing.T) { + tests := []struct { + name string + args []string + wantOut string + wantExitCode exitCode + }{ + { + name: "check auth key", + args: strings.Split("deepl-cli repl", " "), + wantOut: fmt.Sprintf("To get started with deepl-cli, please run: `deepl-cli configure`\n"), + wantExitCode: exitErr, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + conf := &config.DeepLCLIConfig{} + reader, writer, errWriter := new(bytes.Buffer), new(bytes.Buffer), new(bytes.Buffer) + cli := NewCLI(reader, writer, errWriter) + code := cli.Run(tt.args, conf) - assert.Equal(t, tt.wantOut, outStream.String()) + assert.Equal(t, tt.wantOut, errWriter.String()) assert.Equal(t, tt.wantExitCode, code) }) } diff --git a/internal/cmd/subcmd/setup/setup.go b/internal/cmd/subcmd/setup/setup.go deleted file mode 100644 index 6d70e53..0000000 --- a/internal/cmd/subcmd/setup/setup.go +++ /dev/null @@ -1,88 +0,0 @@ -package setup - -import ( - "fmt" - "io" - - "github.com/candy12t/deepl-cli/internal/config" -) - -const ( - authKeyQuestion = "your DeepL auth key" - sourceLanguageQuestion = "set default `source` language" - targetLanguageQuestion = "set default `target` language" -) - -var languageList = []string{"BG", "CS", "DA", "DE", "EL", "EN", "ES", "ET", "FI", "FR", "HU", "IT", "JA", "LT", "LV", "NL", "PL", "PT", "RO", "RU", "SK", "SL", "SV", "ZH"} - -func Setup(inStream io.Reader, outStream io.Writer) error { - conf := PromptSetup(inStream, outStream) - - if err := conf.WriteDeepLCLIConfig(); err != nil { - return err - } - - return nil -} - -func PromptSetup(inStream io.Reader, outStream io.Writer) *config.DeepLCLIConfig { - - authKey := promptForLine(inStream, outStream, authKeyQuestion) - sourceLanguage := promptForSelect(inStream, outStream, sourceLanguageQuestion, languageList) - targetLanguage := promptForSelect(inStream, outStream, targetLanguageQuestion, languageList) - - return &config.DeepLCLIConfig{ - Auth: config.Auth{ - AuthKey: authKey, - }, - DefaultLanguage: config.DefaultLanguage{ - SourceLanguage: sourceLanguage, - TargetLanguage: targetLanguage, - }, - } -} - -func promptForLine(inStream io.Reader, outStream io.Writer, msg string) string { - var answer string - - fmt.Fprintln(outStream, msg) - fmt.Fprintf(outStream, ">> ") - fmt.Fscanf(inStream, "%s\n", &answer) - - return answer -} - -func promptForSelect(inStream io.Reader, outStream io.Writer, msg string, selectList []string) string { - var answer string - - fmt.Fprintln(outStream, msg) - fmt.Fprintln(outStream, joinSelectList(selectList)) - fmt.Fprintf(outStream, ">> ") - fmt.Fscanf(inStream, "%s\n", &answer) - - if ok := isValueInStringSlice(answer, selectList); !ok { - fmt.Fprintln(outStream, "##############################################") - fmt.Fprintln(outStream, "### no correct input value!!! try again!!! ###") - fmt.Fprintln(outStream, "##############################################") - return promptForSelect(inStream, outStream, msg, selectList) - } - - return answer -} - -func joinSelectList(selectList []string) string { - var str string - for _, v := range selectList { - str += fmt.Sprintf("%q ", v) - } - return str -} - -func isValueInStringSlice(value string, strList []string) bool { - for _, v := range strList { - if v == value { - return true - } - } - return false -} diff --git a/internal/cmd/subcmd/setup/setup_test.go b/internal/cmd/subcmd/setup/setup_test.go deleted file mode 100644 index 19f8b4e..0000000 --- a/internal/cmd/subcmd/setup/setup_test.go +++ /dev/null @@ -1,41 +0,0 @@ -package setup - -import ( - "bytes" - "strings" - "testing" - - "github.com/candy12t/deepl-cli/internal/config" - "github.com/stretchr/testify/assert" -) - -func TestSetup(t *testing.T) { - tests := []struct { - name string - input []string - want config.DeepLCLIConfig - }{ - { - name: "setup", - input: []string{"test-auth-key", "EN", "JA"}, - want: config.DeepLCLIConfig{ - Auth: config.Auth{ - AuthKey: "test-auth-key", - }, - DefaultLanguage: config.DefaultLanguage{ - SourceLanguage: "EN", - TargetLanguage: "JA", - }, - }, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - in := bytes.NewBufferString(strings.Join(tt.input, "\n")) - out := new(bytes.Buffer) - got := PromptSetup(in, out) - assert.Equal(t, tt.want, *got) - }) - } -} diff --git a/internal/config/config.go b/internal/config/config.go index bca25d3..de42a1f 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -9,17 +9,17 @@ import ( ) type DeepLCLIConfig struct { - Auth Auth `yaml:"auth"` + Credential Credential `yaml:"credential"` DefaultLanguage DefaultLanguage `yaml:"default_language"` } -type Auth struct { - AuthKey string `yaml:"auth_key"` +type Credential struct { + DeepLAuthKey string `yaml:"deepl_auth_key"` } type DefaultLanguage struct { - SourceLanguage string `yaml:"source_language"` - TargetLanguage string `yaml:"target_language"` + Source string `yaml:"source"` + Target string `yaml:"target"` } func configFile() string { @@ -34,7 +34,7 @@ func NewDeepLCLIConfig() *DeepLCLIConfig { DefaultLanguage: setDefaultLanguage(), } } - if len(conf.DefaultLanguage.SourceLanguage) == 0 || len(conf.DefaultLanguage.TargetLanguage) == 0 { + if len(conf.DefaultLanguage.Source) == 0 || len(conf.DefaultLanguage.Target) == 0 { conf.DefaultLanguage = setDefaultLanguage() } return conf @@ -59,8 +59,8 @@ func parseDeepLCLIConfigData(data []byte) (*DeepLCLIConfig, error) { func setDefaultLanguage() DefaultLanguage { return DefaultLanguage{ - SourceLanguage: "JA", - TargetLanguage: "EN", + Source: "JA", + Target: "EN", } } diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 7cb2630..10ba2a8 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -1,7 +1,6 @@ package config import ( - "fmt" "path/filepath" "testing" @@ -10,34 +9,33 @@ import ( func TestParseConfig(t *testing.T) { input := ` -auth: - auth_key: "test-auth-key" +credential: + deepl_auth_key: "test-auth-key" default_language: - source_language: "JA" - target_language: "EN" + source: "JA" + target: "EN" ` conf, err := parseDeepLCLIConfigData([]byte(input)) if assert.NoError(t, err) { - assert.Equal(t, "test-auth-key", conf.Auth.AuthKey) - assert.Equal(t, "JA", conf.DefaultLanguage.SourceLanguage) - assert.Equal(t, "EN", conf.DefaultLanguage.TargetLanguage) + assert.Equal(t, "test-auth-key", conf.Credential.DeepLAuthKey) + assert.Equal(t, "JA", conf.DefaultLanguage.Source) + assert.Equal(t, "EN", conf.DefaultLanguage.Target) } } func TestWriteConfig(t *testing.T) { conf := DeepLCLIConfig{ - Auth: Auth{ - AuthKey: "test-auth-key", + Credential: Credential{ + DeepLAuthKey: "test-auth-key", }, DefaultLanguage: DefaultLanguage{ - SourceLanguage: "JA", - TargetLanguage: "EN", + Source: "JA", + Target: "EN", }, } filename := filepath.Join(t.TempDir(), "config.yaml") err := conf.writeDeepLCLIConfig(filename) assert.NoError(t, err) - data, _ := parseDeepLCLIConfigFile(filename) - fmt.Println(data) + parseDeepLCLIConfigFile(filename) } diff --git a/internal/controller/configure.go b/internal/controller/configure.go new file mode 100644 index 0000000..f1084f5 --- /dev/null +++ b/internal/controller/configure.go @@ -0,0 +1,61 @@ +package controller + +import ( + "github.com/AlecAivazis/survey/v2" + "github.com/candy12t/deepl-cli/internal/config" + "github.com/urfave/cli/v2" +) + +var languages = []string{"BG", "CS", "DA", "DE", "EL", "EN", "ES", "ET", "FI", "FR", "HU", "IT", "JA", "LT", "LV", "NL", "PL", "PT", "RO", "RU", "SK", "SL", "SV", "ZH"} + +var questions = []*survey.Question{ + { + Name: "deepl_auth_key", + Prompt: &survey.Password{ + Message: "Paste your auth key", + }, + }, + { + Name: "source_language", + Prompt: &survey.Select{ + Message: "Choose a default source language:", + Options: languages, + Default: "JA", + }, + }, + { + Name: "target_language", + Prompt: &survey.Select{ + Message: "Choose a default target language:", + Options: languages, + Default: "EN", + }, + }, +} + +func ConfigureAction(ctx *cli.Context) error { + answers := struct { + DeepLAuthKey string `survey:"deepl_auth_key"` + SourceLanguage string `survey:"source_language"` + TargetLanguage string `survey:"target_language"` + }{} + + if err := survey.Ask(questions, &answers); err != nil { + return err + } + + conf := config.DeepLCLIConfig{ + Credential: config.Credential{ + DeepLAuthKey: answers.DeepLAuthKey, + }, + DefaultLanguage: config.DefaultLanguage{ + Source: answers.SourceLanguage, + Target: answers.TargetLanguage, + }, + } + + if err := conf.WriteDeepLCLIConfig(); err != nil { + return err + } + return nil +} diff --git a/internal/controller/repl.go b/internal/controller/repl.go index cb7b23e..f0beac8 100644 --- a/internal/controller/repl.go +++ b/internal/controller/repl.go @@ -1,58 +1,47 @@ package controller import ( - "bufio" + "errors" "fmt" - "io" + "strings" - "github.com/candy12t/deepl-cli/internal/entity" - "github.com/candy12t/deepl-cli/internal/usecase" + "github.com/AlecAivazis/survey/v2" + "github.com/candy12t/deepl-cli/internal/repository" + "github.com/urfave/cli/v2" ) -const PROMPT = ">> " - -type Repl struct { - uc *usecase.Translation - *entity.Languages - inStream io.Reader - outStream io.Writer -} - -func NewRepl(uc *usecase.Translation, sourceLanguage, targetLanguage string, inStream io.Reader, outStream io.Writer) *Repl { - return &Repl{ - uc: uc, - Languages: &entity.Languages{ - SourceLanguage: sourceLanguage, - TargetLanguage: targetLanguage, - }, - inStream: inStream, - outStream: outStream, - } -} - -func (r *Repl) Apply() { - fmt.Fprintf(r.outStream, "Translate text from %s to %s\n", r.SourceLanguage, r.TargetLanguage) - scanner := bufio.NewScanner(r.inStream) - - for { - fmt.Fprint(r.outStream, PROMPT) - if !scanner.Scan() { - return - } - text := scanner.Text() - - translation, err := entity.NewTranslation(text, r.Languages) - if err != nil { - fmt.Fprintln(r.outStream, err) - continue +var ErrTextLength = errors.New("Error: input text length is 0") + +func ReplAction(client repository.Translater) cli.ActionFunc { + return func(ctx *cli.Context) error { + sourceLanguage := ctx.String("source") + targetLanguage := ctx.String("target") + message := fmt.Sprintf("(%s->%s) >>", sourceLanguage, targetLanguage) + + for { + var text string + prompt := &survey.Input{ + Message: message, + } + if err := survey.AskOne(prompt, &text, survey.WithIcons(func(icons *survey.IconSet) { + icons.Question.Text = "deepl-cli" + icons.Question.Format = "default+hb" + })); err != nil { + return err + } + + trimedSpaceText := strings.TrimSpace(text) + if len(trimedSpaceText) == 0 { + fmt.Fprintln(ctx.App.ErrWriter, ErrTextLength.Error()) + continue + } + + tr, err := client.TranslateText(trimedSpaceText, sourceLanguage, targetLanguage) + if err != nil { + return err + } + + fmt.Fprintln(ctx.App.Writer, tr.TranslateText) } - - resultTranlation, err := r.uc.Translate(translation) - if err != nil { - fmt.Fprintln(r.outStream, err) - break - } - - fmt.Fprintln(r.outStream, resultTranlation.TranslatedText) } } diff --git a/internal/deepl/deepl.go b/internal/deepl/deepl.go index 5d8ef99..dfa289a 100644 --- a/internal/deepl/deepl.go +++ b/internal/deepl/deepl.go @@ -1,24 +1,28 @@ -//go:generate mockgen -source=$GOFILE -destination=./mock_$GOPACKAGE/$GOFILE - package deepl import ( "encoding/json" + "errors" "fmt" "io" "net/http" "net/url" "path" "strings" + + "github.com/candy12t/deepl-cli/internal/repository" ) const ( - FreeHost = "https://api-free.deepl.com" - ProHost = "https://api.deepl.com" - APIVersion = "v2" - EndpointDetermineKey = ":fx" + FreeHost = "https://api-free.deepl.com" + ProHost = "https://api.deepl.com" + APIVersion = "v2" + + AccountPlanIdentificationKey = ":fx" ) +var _ repository.Translater = &Client{} + type Client struct { BaseURL *url.URL HTTPClient *http.Client @@ -37,13 +41,17 @@ func NewClient(authKey string) *Client { } func defaultHost(authKey string) string { - if !strings.HasSuffix(authKey, EndpointDetermineKey) { + if isPro(authKey) { return ProHost } return FreeHost } -func (c *Client) newRequest(method, _path string, body io.Reader) (*http.Request, error) { +func isPro(authKey string) bool { + return !strings.HasSuffix(authKey, AccountPlanIdentificationKey) +} + +func (c *Client) NewRequest(method, _path string, body io.Reader) (*http.Request, error) { u := *c.BaseURL u.Path = path.Join(c.BaseURL.Path, _path) @@ -57,12 +65,20 @@ func (c *Client) newRequest(method, _path string, body io.Reader) (*http.Request return req, nil } -func decodeBody(resp *http.Response, out interface{}) error { +func (c *Client) Do(req *http.Request, v interface{}) error { + resp, err := c.HTTPClient.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() + success := resp.StatusCode >= 200 && resp.StatusCode < 300 if !success { return HandleHTTPError(resp) } - decoder := json.NewDecoder(resp.Body) - return decoder.Decode(out) + if err := json.NewDecoder(resp.Body).Decode(v); !errors.Is(err, io.EOF) { + return err + } + return nil } diff --git a/internal/deepl/translate.go b/internal/deepl/translate.go index 1285082..8772aab 100644 --- a/internal/deepl/translate.go +++ b/internal/deepl/translate.go @@ -3,40 +3,48 @@ package deepl import ( "net/url" "strings" + + "github.com/candy12t/deepl-cli/internal/model" ) -type TranslateList struct { - Translations []Translation `json:"translations"` +type TranslationResponse struct { + Translations []struct { + DetectedSourceLanguage string `json:"detected_source_language"` + Text string `json:"text"` + } `json:"translations"` } -type Translation struct { - DetectedSourceLanguage string `json:"detected_source_language"` - Text string `json:"text"` +func (c *Client) TranslateText(text, sourceLang, targetLang string) (*model.TranslateText, error) { + tr, err := c.translateText(text, sourceLang, targetLang) + if err != nil { + return nil, err + } + tt := &model.TranslateText{ + OriginalText: text, + TranslateText: tr.Translations[0].Text, + SourceLanguage: sourceLang, + TargetLanguage: targetLang, + } + return tt, nil } // DeepL API docs: https://www.deepl.com/ja/docs-api/translating-text -func (c *Client) translate(text, sourceLang, targetLang string) (*TranslateList, error) { +func (c *Client) translateText(text, sourceLang, targetLang string) (*TranslationResponse, error) { values := url.Values{} values.Add("text", text) values.Add("source_lang", sourceLang) values.Add("target_lang", targetLang) - req, err := c.newRequest("POST", "/translate", strings.NewReader(values.Encode())) + req, err := c.NewRequest("POST", "/translate", strings.NewReader(values.Encode())) if err != nil { return nil, err } req.Header.Set("Content-Type", "application/x-www-form-urlencoded") - resp, err := c.HTTPClient.Do(req) - if err != nil { - return nil, err - } - defer resp.Body.Close() - - translateList := new(TranslateList) - if err := decodeBody(resp, translateList); err != nil { + tr := new(TranslationResponse) + if err := c.Do(req, tr); err != nil { return nil, err } - return translateList, nil + return tr, nil } diff --git a/internal/deepl/translate_test.go b/internal/deepl/translate_test.go index a9dde20..85f7f9c 100644 --- a/internal/deepl/translate_test.go +++ b/internal/deepl/translate_test.go @@ -6,6 +6,7 @@ import ( "path" "testing" + "github.com/candy12t/deepl-cli/internal/model" "github.com/stretchr/testify/assert" ) @@ -22,12 +23,17 @@ func TestTranslate(t *testing.T) { testBody(t, r, "source_lang=EN&target_lang=JA&text=hello") w.WriteHeader(http.StatusOK) - w.Write([]byte(`{"translations":[{"detected_source_language":"EN","text":"こんにちわ"}]}`)) + w.Write([]byte(`{"translations":[{"detected_source_language":"EN","text":"こんにちは"}]}`)) }) - want := &TranslateList{Translations: []Translation{{DetectedSourceLanguage: "EN", Text: "こんにちわ"}}} + want := &model.TranslateText{ + OriginalText: "hello", + TranslateText: "こんにちは", + SourceLanguage: "EN", + TargetLanguage: "JA", + } - got, err := client.translate("hello", "EN", "JA") + got, err := client.TranslateText("hello", "EN", "JA") if assert.NoError(t, err) { assert.Equal(t, got, want) @@ -53,7 +59,7 @@ func TestTranslate(t *testing.T) { u.Path = path.Join(client.BaseURL.Path, "/translate") want := HTTPError{StatusCode: http.StatusBadRequest, RequestURL: u.String(), Message: `"Value for 'target_lang' not supported."`} - _, err := client.translate("hello", "EN", "") + _, err := client.TranslateText("hello", "EN", "") assert.EqualError(t, err, want.Error()) }) @@ -75,7 +81,7 @@ func TestTranslate(t *testing.T) { u.Path = path.Join(client.BaseURL.Path, "/translate") want := HTTPError{StatusCode: http.StatusForbidden, RequestURL: u.String(), Message: "403 Forbidden"} - _, err := client.translate("hello", "EN", "JA") + _, err := client.TranslateText("hello", "EN", "JA") assert.EqualError(t, err, want.Error()) }) diff --git a/internal/deepl/webapi.go b/internal/deepl/webapi.go deleted file mode 100644 index 219ac09..0000000 --- a/internal/deepl/webapi.go +++ /dev/null @@ -1,17 +0,0 @@ -package deepl - -import ( - "github.com/candy12t/deepl-cli/internal/entity" - "github.com/candy12t/deepl-cli/internal/webapi" -) - -var _ webapi.Translater = &Client{} - -func (c *Client) Translate(translation *entity.Translation) (*entity.Translation, error) { - translateList, err := c.translate(translation.OriginalText, translation.SourceLanguage, translation.TargetLanguage) - if err != nil { - return nil, err - } - translation.TranslatedText = translateList.Translations[0].Text - return translation, nil -} diff --git a/internal/entity/translation.go b/internal/entity/translation.go deleted file mode 100644 index 4f11232..0000000 --- a/internal/entity/translation.go +++ /dev/null @@ -1,32 +0,0 @@ -package entity - -import ( - "errors" - "strings" -) - -type Translation struct { - Languages - OriginalText string - TranslatedText string -} - -type Languages struct { - SourceLanguage string - TargetLanguage string -} - -var ErrTextLength = errors.New("Error: text length is 0") - -func NewTranslation(originalText string, languages *Languages) (*Translation, error) { - trimedspace := strings.TrimSpace(originalText) - if len(trimedspace) == 0 { - return nil, ErrTextLength - } - - translation := &Translation{ - Languages: *languages, - OriginalText: originalText, - } - return translation, nil -} diff --git a/internal/entity/translation_test.go b/internal/entity/translation_test.go deleted file mode 100644 index 54b58e9..0000000 --- a/internal/entity/translation_test.go +++ /dev/null @@ -1,15 +0,0 @@ -package entity - -import ( - "testing" -) - -func TestNewTranslationErr(t *testing.T) { - original := "" - _, err := NewTranslation(original, &Languages{}) - got := err.Error() - want := "Error: text length is 0" - if got != want { - t.Fatalf("got error is %q, want error is %q\n", got, want) - } -} diff --git a/internal/model/translate.go b/internal/model/translate.go new file mode 100644 index 0000000..bf15b8a --- /dev/null +++ b/internal/model/translate.go @@ -0,0 +1,8 @@ +package model + +type TranslateText struct { + OriginalText string + TranslateText string + SourceLanguage string + TargetLanguage string +} diff --git a/internal/webapi/mock_webapi/translate.go b/internal/repository/mock_repository/translate.go similarity index 59% rename from internal/webapi/mock_webapi/translate.go rename to internal/repository/mock_repository/translate.go index 2830677..2033459 100644 --- a/internal/webapi/mock_webapi/translate.go +++ b/internal/repository/mock_repository/translate.go @@ -1,13 +1,13 @@ // Code generated by MockGen. DO NOT EDIT. // Source: translate.go -// Package mock_webapi is a generated GoMock package. -package mock_webapi +// Package mock_repository is a generated GoMock package. +package mock_repository import ( reflect "reflect" - entity "github.com/candy12t/deepl-cli/internal/entity" + model "github.com/candy12t/deepl-cli/internal/model" gomock "github.com/golang/mock/gomock" ) @@ -34,17 +34,17 @@ func (m *MockTranslater) EXPECT() *MockTranslaterMockRecorder { return m.recorder } -// Translate mocks base method. -func (m *MockTranslater) Translate(arg0 *entity.Translation) (*entity.Translation, error) { +// TranslateText mocks base method. +func (m *MockTranslater) TranslateText(text, sourceLang, targetLang string) (*model.TranslateText, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Translate", arg0) - ret0, _ := ret[0].(*entity.Translation) + ret := m.ctrl.Call(m, "TranslateText", text, sourceLang, targetLang) + ret0, _ := ret[0].(*model.TranslateText) ret1, _ := ret[1].(error) return ret0, ret1 } -// Translate indicates an expected call of Translate. -func (mr *MockTranslaterMockRecorder) Translate(arg0 interface{}) *gomock.Call { +// TranslateText indicates an expected call of TranslateText. +func (mr *MockTranslaterMockRecorder) TranslateText(text, sourceLang, targetLang interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Translate", reflect.TypeOf((*MockTranslater)(nil).Translate), arg0) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "TranslateText", reflect.TypeOf((*MockTranslater)(nil).TranslateText), text, sourceLang, targetLang) } diff --git a/internal/repository/translate.go b/internal/repository/translate.go new file mode 100644 index 0000000..baf3592 --- /dev/null +++ b/internal/repository/translate.go @@ -0,0 +1,8 @@ +//go:generate mockgen -source=$GOFILE -destination=./mock_$GOPACKAGE/$GOFILE +package repository + +import "github.com/candy12t/deepl-cli/internal/model" + +type Translater interface { + TranslateText(text, sourceLang, targetLang string) (*model.TranslateText, error) +} diff --git a/internal/usecase/translate.go b/internal/usecase/translate.go deleted file mode 100644 index 4e72239..0000000 --- a/internal/usecase/translate.go +++ /dev/null @@ -1,20 +0,0 @@ -package usecase - -import ( - "github.com/candy12t/deepl-cli/internal/entity" - "github.com/candy12t/deepl-cli/internal/webapi" -) - -type Translation struct { - webapi webapi.Translater -} - -func NewTranslation(webapi webapi.Translater) *Translation { - return &Translation{ - webapi: webapi, - } -} - -func (t *Translation) Translate(translation *entity.Translation) (*entity.Translation, error) { - return t.webapi.Translate(translation) -} diff --git a/internal/webapi/translate.go b/internal/webapi/translate.go deleted file mode 100644 index 4f8833a..0000000 --- a/internal/webapi/translate.go +++ /dev/null @@ -1,8 +0,0 @@ -//go:generate mockgen -source=$GOFILE -destination=./mock_$GOPACKAGE/$GOFILE -package webapi - -import "github.com/candy12t/deepl-cli/internal/entity" - -type Translater interface { - Translate(*entity.Translation) (*entity.Translation, error) -}