Skip to content

Commit

Permalink
#1 JSON出力の実装
Browse files Browse the repository at this point in the history
#1 JSON出力の実装
  • Loading branch information
Raisan authored Jun 18, 2022
2 parents 9c4a59e + 6a0dbd3 commit 864f976
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 20 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@

# Build
translate
*.exe
*.exe

# API
config.dev.json
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,21 @@

# ⭐ 使い方

```powershell
- ヘルプを表示
```bash
# ヘルプを表示
translate help

- 翻訳先の言語を指定して翻訳
# 翻訳先の言語を指定して翻訳
translate -t en こんにちは

- 空白のあるテキストを翻訳
# 空白のあるテキストを翻訳
translate -t en "こんにちは 世界"

- 翻訳元の言語も指定する
# 翻訳元の言語も指定する
translate -t en -f ja こんにちは

# 結果をJSONで出力
translate -t en こんにちは --json
```

対応している言語の言語コード一覧は [言語サポート | Cloud Translation | Google Cloud](https://cloud.google.com/translate/docs/languages) を参照
Expand Down Expand Up @@ -52,7 +55,9 @@ go mod tidy

**開発モードに切り替える** *(これを行わないと`go run`が使用できません)*

以下のように書き換えてください
`config.json``config.dev.json` にリネームし

`main.go` を以下のように書き換えてください

```go
// main.go [Line: 13~19]
Expand Down
47 changes: 36 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ func main() {
fmt.Printf("%s: 設定ファイルの読み込みに失敗\n設定は `%s` を参照してください", gocolor.Red("Error"), gocolor.Purple(git_repo))
os.Exit(0)
}
to, to_fi := getFlag("-t", "--to", os.Args)
from, from_fi := getFlag("-f", "--from", os.Args)
flags := []int{to_fi, to_fi + 1, from_fi, from_fi + 1}
to, to_fi := getFlag("-t", "--to", os.Args, false)
from, from_fi := getFlag("-f", "--from", os.Args, false)
out_json, json_fi := getFlag("-j", "--json", os.Args, true)
flags := []int{to_fi, to_fi + 1, from_fi, from_fi + 1, json_fi}
var args []string
for i, v := range os.Args {
if i != 0 {
Expand All @@ -47,7 +48,11 @@ func main() {
}
}
if to == "" {
fmt.Printf("%s: 必要な引数がありません\n詳細は `%s` を参照してください。", gocolor.Red("Error"), gocolor.Cayn(command+" help"))
if out_json == "" {
fmt.Printf("%s: 必要な引数がありません\n詳細は `%s` を参照してください。", gocolor.Red("Error"), gocolor.Cayn(command+" help"))
} else {
fmt.Print(`{"code": 400, "msg": "arguments_error"}`)
}
os.Exit(0)
}

Expand All @@ -59,23 +64,39 @@ func main() {

resp, err := http.Get(urlGen(to, from, args[0], conf.Api))
if err != nil {
fmt.Printf("%s: リクエストに失敗しました\nインターネットの接続、APIの設定等を確認してください\n[Log]%s", gocolor.Red("Error"), err)
if out_json == "" {
fmt.Printf("%s: リクエストに失敗しました\nインターネットの接続、APIの設定等を確認してください\n[Log]%s", gocolor.Red("Error"), err)
} else {
fmt.Print(`{"code": 400, "msg": "request_error"}`)
}
os.Exit(0)
}
defer resp.Body.Close()

if resp.StatusCode != 200 {
fmt.Printf("%s: リクエストに失敗しました\n[Log]HTTP Status: `%s`", gocolor.Red("Error"), resp.Status)
if out_json == "" {
fmt.Printf("%s: リクエストに失敗しました\n[Log]HTTP Status: `%s`", gocolor.Red("Error"), resp.Status)
} else {
fmt.Print(`{"code": 400, "msg": "respose_error"}`)
}
os.Exit(0)
}

body, _ := io.ReadAll(resp.Body)
if err := json.Unmarshal(body, response); err != nil {
fmt.Printf("%s: リクエストの解析に失敗しました\n[Log]%s", gocolor.Red("Error"), err)
if out_json == "" {
fmt.Printf("%s: リクエストの解析に失敗しました\n[Log]%s", gocolor.Red("Error"), err)
} else {
fmt.Print(`{"code": 400, "msg": "parse_error"}`)
}
os.Exit(0)
}
if response.Msg == "unexpected" {
fmt.Printf("%s: 翻訳に失敗しました\n翻訳に対応している言語は `%s` を参照してください。\n[Log]API Error", gocolor.Red("Error"), gocolor.Purple("https://cloud.google.com/translate/docs/languages"))
if out_json == "" {
fmt.Printf("%s: 翻訳に失敗しました\n翻訳に対応している言語は `%s` を参照してください。\n[Log]API Error", gocolor.Red("Error"), gocolor.Purple("https://cloud.google.com/translate/docs/languages"))
} else {
fmt.Print(`{"code": 500, "msg": "translation_error"}`)
}
os.Exit(0)
}

Expand All @@ -85,9 +106,13 @@ func main() {
} else {
lang_info = from
}
fmt.Printf("%s\n %s", gocolor.Purple("[Before: "+lang_info+"]"), args[0])
fmt.Print("\n\n")
fmt.Printf("%s\n %s", gocolor.Green("[After: "+to+"]"), response.Text)
if out_json == "" {
fmt.Printf("%s\n %s", gocolor.Purple("[Before: "+lang_info+"]"), args[0])
fmt.Print("\n\n")
fmt.Printf("%s\n %s", gocolor.Green("[After: "+to+"]"), response.Text)
} else {
fmt.Printf(`{"code": 200, "msg": "succes", "to": "%s", from: "%s", "text": "%s"}`, to, lang_info, response.Text)
}
os.Exit(0)
}
}
10 changes: 8 additions & 2 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@ import (
"path/filepath"
)

func getFlag(k string, kf string, s []string) (string, int) {
func getFlag(k string, kf string, s []string, oneflag bool) (string, int) {
for i, v := range s {
if k == v {
if oneflag {
return s[i], i
}
return s[i+1], i
}
}
for i, v := range s {
if kf == v {
if oneflag {
return s[i], i
}
return s[i+1], i
}
}
Expand Down Expand Up @@ -50,7 +56,7 @@ func loadConfig(dev bool) (*config, error) {
rund := filepath.Dir(runp)
configfp = rund + "/config.json"
} else {
configfp = "/config.json"
configfp = "./config.dev.json"
}
f, err := os.Open(configfp)
if err != nil {
Expand Down

0 comments on commit 864f976

Please sign in to comment.