Skip to content

Commit

Permalink
Fix: config
Browse files Browse the repository at this point in the history
add BaseURL to Config struct
  • Loading branch information
candy12t committed Nov 13, 2021
1 parent 1202094 commit c6ee559
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 56 deletions.
4 changes: 2 additions & 2 deletions api/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"net/url"
"strings"

"github.com/candy12t/deepl-cli/internal/host"
"github.com/candy12t/deepl-cli/internal/config"
)

type TranslateResponse struct {
Expand All @@ -19,7 +19,7 @@ func Translate(text, sourceLang, TargetLang string) (*TranslateResponse, error)

translate := &TranslateResponse{}
err := Request(
host.TranslateEndpoint(),
config.BaseURL()+"/translate",
"POST",
strings.NewReader(values.Encode()),
translate,
Expand Down
28 changes: 10 additions & 18 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,10 @@ import (
"gopkg.in/yaml.v2"
)

const (
ErrNotReadFile = ConfigErr("can not read config file")
ErrNotUnmarshal = ConfigErr("can not unmarshal config data")
)

type ConfigErr string

func (c ConfigErr) Error() string {
return string(c)
}

type Config struct {
Account Account `yaml:"account"`
DefaultLang DefaultLang `yaml:"default_lang"`
BaseURL string
}

type Account struct {
Expand All @@ -36,6 +26,10 @@ type DefaultLang struct {

var config Config

func CachedConfig() Config {
return config
}

func ConfigFile() string {
d, _ := os.UserHomeDir()
return filepath.Join(d, ".config", "deepl-cli", "config.yaml")
Expand All @@ -46,19 +40,17 @@ func ParseConfig(filepath string) error {
if err != nil {
return ErrNotReadFile
}

if err := yaml.Unmarshal(data, &config); err != nil {
return ErrNotUnmarshal
}
return nil
}
config.BaseURL = baseURL()

func CachedConfig() Config {
return config
return nil
}

func DefaultLangs() (string, string) {
return config.DefaultLang.SourceLang, config.DefaultLang.TargetLang
}

func AccountPlan() string { return CachedConfig().Account.AccountPlan }
func AuthKey() string { return CachedConfig().Account.AuthKey }
func AuthKey() string { return CachedConfig().Account.AuthKey }
func BaseURL() string { return CachedConfig().BaseURL }
12 changes: 12 additions & 0 deletions internal/config/config_error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package config

const (
ErrNotReadFile = ConfigErr("can not read config file")
ErrNotUnmarshal = ConfigErr("can not unmarshal config data")
)

type ConfigErr string

func (c ConfigErr) Error() string {
return string(c)
}
22 changes: 22 additions & 0 deletions internal/config/config_host.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package config

import "fmt"

const (
baseHost = "deepl.com"
apiVersion = "v2"
pro = "api"
free = "api-free"
)

func baseURL() string {
if isPro() {
return fmt.Sprintf("https://%s.%s/%s", pro, baseHost, apiVersion)
} else {
return fmt.Sprintf("https://%s.%s/%s", free, baseHost, apiVersion)
}
}

func isPro() bool {
return CachedConfig().Account.AccountPlan == "pro"
}
39 changes: 35 additions & 4 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package config

import (
"os"
"path/filepath"
"testing"
)

func TestParseConfig(t *testing.T) {
t.Run("parse config yml file", func(t *testing.T) {
t.Run("parse config yml file, accoutn plan is `free`", func(t *testing.T) {
wantConfig := Config{
Account: Account{
AuthKey: "test-auth-key",
Expand All @@ -16,9 +17,33 @@ func TestParseConfig(t *testing.T) {
SourceLang: "EN",
TargetLang: "JA",
},
BaseURL: "https://api-free.deepl.com/v2",
}

configPath := filepath.Join("testdata", "config.yaml")
configPath := filepath.Join(projectDirPath(), "testdata", "config", "free.yaml")
err := ParseConfig(configPath)
gotConfig := CachedConfig()

assertNoError(t, err)
assertConfig(t, gotConfig, wantConfig)

cleanCachedConfig(t)
})

t.Run("parse config yml file, accoutn plan is `pro`", func(t *testing.T) {
wantConfig := Config{
Account: Account{
AuthKey: "test-auth-key",
AccountPlan: "pro",
},
DefaultLang: DefaultLang{
SourceLang: "EN",
TargetLang: "JA",
},
BaseURL: "https://api.deepl.com/v2",
}

configPath := filepath.Join(projectDirPath(), "testdata", "config", "pro.yaml")
err := ParseConfig(configPath)
gotConfig := CachedConfig()

Expand All @@ -31,7 +56,7 @@ func TestParseConfig(t *testing.T) {
t.Run("can not read config file", func(t *testing.T) {
wantConfig := Config{}

configPath := filepath.Join("testdata", "not_read.yaml")
configPath := filepath.Join(projectDirPath(), "testdata", "config", "not_read.yaml")
err := ParseConfig(configPath)
gotConfig := CachedConfig()

Expand All @@ -44,7 +69,7 @@ func TestParseConfig(t *testing.T) {
t.Run("can not unmarshal config file", func(t *testing.T) {
wantConfig := Config{}

configPath := filepath.Join("testdata", "not_unmarshal.yaml")
configPath := filepath.Join(projectDirPath(), "testdata", "config", "not_unmarshal.yaml")
err := ParseConfig(configPath)
gotConfig := CachedConfig()

Expand All @@ -56,6 +81,7 @@ func TestParseConfig(t *testing.T) {
}

func assertConfig(t *testing.T, got, want Config) {
t.Helper()
if got != want {
t.Fatalf("got %v, want %v", got, want)
}
Expand Down Expand Up @@ -88,6 +114,11 @@ func assertUnmarshalError(t *testing.T, got, want error) {
}
}

func projectDirPath() string {
currentDirPath, _ := os.Getwd()
return filepath.Join(currentDirPath, "..", "..")
}

func cleanCachedConfig(t *testing.T) {
t.Cleanup(func() {
config = Config{}
Expand Down
32 changes: 0 additions & 32 deletions internal/host/host.go

This file was deleted.

File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions testdata/config/pro.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
account:
auth_key: "test-auth-key"
account_plan: "pro"
default_lang:
source_lang: "EN"
target_lang: "JA"

0 comments on commit c6ee559

Please sign in to comment.