Skip to content

Commit

Permalink
Fix config
Browse files Browse the repository at this point in the history
  • Loading branch information
candy12t committed Mar 20, 2022
1 parent fa34e35 commit 7285f88
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 210 deletions.
4 changes: 2 additions & 2 deletions cmd/deepl-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
)

func main() {
cfg := config.LoadConfig()
cli := cmd.NewCLI(os.Stdin, os.Stdout, os.Stderr, cfg)
conf := config.NewDeepLCLIConfig()
cli := cmd.NewCLI(os.Stdin, os.Stdout, os.Stderr, conf)
code := cli.Run(os.Args)
os.Exit(int(code))
}
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ go 1.17

require (
github.com/golang/mock v1.6.0
github.com/stretchr/testify v1.7.1
github.com/urfave/cli/v2 v2.3.0
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
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/davecgh/go-spew v1.1.0/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/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=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
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.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=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
Expand Down Expand Up @@ -39,3 +45,5 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
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=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
20 changes: 8 additions & 12 deletions internal/cmd/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,21 @@ type CLI struct {
InStream io.Reader
OutStream io.Writer
ErrStream io.Writer

cfg *config.Config
conf *config.DeepLCLIConfig
}

func NewCLI(inStream io.Reader, outStream, errStream io.Writer, cfg *config.Config) *CLI {
func NewCLI(inStream io.Reader, outStream, errStream io.Writer, conf *config.DeepLCLIConfig) *CLI {
return &CLI{
InStream: inStream,
OutStream: outStream,
ErrStream: errStream,
cfg: cfg,
conf: conf,
}
}

func (c *CLI) Run(args []string) exitCode {
defaultSourceLang, defaultTargetLang := c.cfg.DefaultLangs()

var sourceLang, targetLang string
defaultSourceLanguage, defaultTargetLanguage := c.conf.DefaultLanguage.SourceLanguage, c.conf.DefaultLanguage.TargetLanguage

cli.VersionFlag = &cli.BoolFlag{
Name: "version",
Expand Down Expand Up @@ -85,15 +83,13 @@ func (c *CLI) Run(args []string) exitCode {
&cli.StringFlag{
Name: "source",
Aliases: []string{"s"},
Value: defaultSourceLang,
DefaultText: defaultSourceLang,
Value: defaultSourceLanguage,
Destination: &sourceLang,
},
&cli.StringFlag{
Name: "target",
Aliases: []string{"t"},
Value: defaultTargetLang,
DefaultText: defaultTargetLang,
Value: defaultTargetLanguage,
Destination: &targetLang,
},
},
Expand All @@ -102,7 +98,7 @@ func (c *CLI) Run(args []string) exitCode {
return err
}
fmt.Fprintf(ctx.App.Writer, "Translate text from %s to %s\n", sourceLang, targetLang)
client, err := deepl.NewClient(c.cfg.GetAuthKey())
client, err := deepl.NewClient(c.conf.Auth.AuthKey)
if err != nil {
return err
}
Expand All @@ -122,7 +118,7 @@ func (c *CLI) Run(args []string) exitCode {
}

func (c *CLI) checkAuthKey() error {
if len(c.cfg.GetAuthKey()) == 0 {
if len(c.conf.Auth.AuthKey) == 0 {
return fmt.Errorf("To setup, please run `deepl-cli setup`.")
}
return nil
Expand Down
14 changes: 8 additions & 6 deletions internal/cmd/cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,17 @@ func TestRun(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := &config.Config{
AuthKey: "test-auth-key",
DefaultLang: config.DefaultLang{
SourceLang: "EN",
TargetLang: "JA",
conf := &config.DeepLCLIConfig{
Auth: config.Auth{
AuthKey: "test-auth-key",
},
DefaultLanguage: config.DefaultLanguage{
SourceLanguage: "EN",
TargetLanguage: "JA",
},
}
inStream, outStream, errStream := new(bytes.Buffer), new(bytes.Buffer), new(bytes.Buffer)
cli := NewCLI(inStream, outStream, errStream, cfg)
cli := NewCLI(inStream, outStream, errStream, conf)
code := cli.Run(tt.args)

if outStream.String() != tt.wantOut {
Expand Down
18 changes: 10 additions & 8 deletions internal/cmd/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,28 @@ const (
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 {
cfg := PromptSetup(inStream, outStream)
conf := PromptSetup(inStream, outStream)

if err := cfg.Write(); err != nil {
if err := conf.WriteDeepLCLIConfig(); err != nil {
return err
}

return nil
}

func PromptSetup(inStream io.Reader, outStream io.Writer) *config.Config {
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.Config{
AuthKey: authKey,
DefaultLang: config.DefaultLang{
SourceLang: sourceLanguage,
TargetLang: targetLanguage,
return &config.DeepLCLIConfig{
Auth: config.Auth{
AuthKey: authKey,
},
DefaultLanguage: config.DefaultLanguage{
SourceLanguage: sourceLanguage,
TargetLanguage: targetLanguage,
},
}
}
Expand Down
28 changes: 16 additions & 12 deletions internal/cmd/setup/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,31 @@ func TestSetup(t *testing.T) {
tests := []struct {
name string
input []string
want *config.Config
want *config.DeepLCLIConfig
}{
{
name: "setup",
input: []string{"test-auth-key", "free", "EN", "JA"},
want: &config.Config{
AuthKey: "test-auth-key",
DefaultLang: config.DefaultLang{
SourceLang: "EN",
TargetLang: "JA",
input: []string{"test-auth-key", "fred", "EN", "JA"},
want: &config.DeepLCLIConfig{
Auth: config.Auth{
AuthKey: "test-auth-key",
},
DefaultLanguage: config.DefaultLanguage{
SourceLanguage: "EN",
TargetLanguage: "JA",
},
},
},
{
name: "setup validate",
input: []string{"test-auth-key", "hoge", "free", "EN", "JA"},
want: &config.Config{
AuthKey: "test-auth-key",
DefaultLang: config.DefaultLang{
SourceLang: "EN",
TargetLang: "JA",
want: &config.DeepLCLIConfig{
Auth: config.Auth{
AuthKey: "test-auth-key",
},
DefaultLanguage: config.DefaultLanguage{
SourceLanguage: "EN",
TargetLanguage: "JA",
},
},
},
Expand Down
91 changes: 46 additions & 45 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,91 +8,92 @@ import (
"gopkg.in/yaml.v2"
)

type Config struct {
AuthKey string `yaml:"auth_key"`
DefaultLang DefaultLang `yaml:"default_lang"`
type DeepLCLIConfig struct {
Auth Auth `yaml:"auth"`
DefaultLanguage DefaultLanguage `yaml:"default_language"`
}

type DefaultLang struct {
SourceLang string `yaml:"source_lang"`
TargetLang string `yaml:"target_lang"`
type Auth struct {
AuthKey string `yaml:"auth_key"`
}

func ConfigFile() string {
type DefaultLanguage struct {
SourceLanguage string `yaml:"source_language"`
TargetLanguage string `yaml:"target_language"`
}

func configFile() string {
d, _ := os.UserHomeDir()
return filepath.Join(d, ".config", "deepl-cli", "config.yaml")
}

func ParseConfig(filepath string) (*Config, error) {
data, err := ioutil.ReadFile(filepath)
func NewDeepLCLIConfig() *DeepLCLIConfig {
conf, err := parseDeepLCLIConfigFile(configFile())
if err != nil {
return nil, ErrNotReadFile
return &DeepLCLIConfig{
DefaultLanguage: setDefaultLanguage(),
}
}

config := new(Config)
if err := yaml.Unmarshal(data, config); err != nil {
return nil, ErrNotUnmarshal
if len(conf.DefaultLanguage.SourceLanguage) == 0 || len(conf.DefaultLanguage.TargetLanguage) == 0 {
conf.DefaultLanguage = setDefaultLanguage()
}
return conf
}

return config, nil
func parseDeepLCLIConfigFile(filename string) (*DeepLCLIConfig, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return parseDeepLCLIConfigData(data)
}

func LoadConfig() *Config {
cfg, err := ParseConfig(ConfigFile())
func parseDeepLCLIConfigData(data []byte) (*DeepLCLIConfig, error) {
conf := new(DeepLCLIConfig)
err := yaml.Unmarshal(data, conf)
if err != nil {
return &Config{
DefaultLang: setDefaultLang(),
}
return nil, err
}
return conf, nil
}

if sourceLang, targetLang := cfg.DefaultLangs(); sourceLang == "" || targetLang == "" {
cfg.DefaultLang = setDefaultLang()
func setDefaultLanguage() DefaultLanguage {
return DefaultLanguage{
SourceLanguage: "JA",
TargetLanguage: "EN",
}
}

return cfg
func (c *DeepLCLIConfig) WriteDeepLCLIConfig() error {
return c.writeDeepLCLIConfig(configFile())
}

func (c *Config) Write() error {
cfgBytes, err := yaml.Marshal(c)
func (c *DeepLCLIConfig) writeDeepLCLIConfig(filename string) error {
conf, err := yaml.Marshal(c)
if err != nil {
return err
}

if err := WriteConfigFile(ConfigFile(), cfgBytes); err != nil {
if err := writeConfigFile(filename, conf); err != nil {
return err
}

return nil
}

func WriteConfigFile(filename string, data []byte) error {
func writeConfigFile(filename string, data []byte) error {
err := os.MkdirAll(filepath.Dir(filename), 0644)
if err != nil {
return err
}

cfg, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer cfg.Close()
defer f.Close()

_, err = cfg.Write(data)
_, err = f.Write(data)

return err
}

func (c *Config) DefaultLangs() (string, string) {
return c.DefaultLang.SourceLang, c.DefaultLang.TargetLang
}

func (c *Config) GetAuthKey() string {
return c.AuthKey
}

func setDefaultLang() DefaultLang {
return DefaultLang{
SourceLang: "EN",
TargetLang: "JA",
}
}
12 changes: 0 additions & 12 deletions internal/config/config_error.go

This file was deleted.

Loading

0 comments on commit 7285f88

Please sign in to comment.