Achieved some more progress

Signed-off-by: Aoi K <koizumi.aoi@kyoko-project.wer.ee>

git-svn-id: file:///srv/svn/repo/suwako/trunk@2 0b558ee1-521d-8b46-a41b-40029c97c055
This commit is contained in:
koizumi.aoi
2022-12-12 02:13:07 +00:00
parent e3f7b5b83b
commit 82dd32fba9
2 changed files with 25 additions and 19 deletions

2
go.mod
View File

@@ -1,3 +1,3 @@
module git.076.ne.jp/novaburst/stcli module git.kyoko-project.wer.ee/koizumi.aoi/stcli-go
go 1.18 go 1.18

42
main.go
View File

@@ -5,10 +5,10 @@ package main
import ( import (
"fmt" "fmt"
"net/http"
"flag" "flag"
"io" "io"
"path/filepath" "log"
"net/http"
"os" "os"
) )
@@ -25,35 +25,41 @@ func init() {
flag.StringVar(&instanceURL, "i", "https://translate.bus-hit.me/api/translate/", "Instance for SimplyTranslate (default: https://translate.bus-hit.me)") flag.StringVar(&instanceURL, "i", "https://translate.bus-hit.me/api/translate/", "Instance for SimplyTranslate (default: https://translate.bus-hit.me)")
flag.StringVar(&engine, "e", "google", "Translation engine (default: google)") flag.StringVar(&engine, "e", "google", "Translation engine (default: google)")
flag.StringVar(&fromLang, "f", "auto", "Source language (default: auto)") flag.StringVar(&fromLang, "f", "auto", "Source language (default: auto)")
flag.StringVar(&toLang, "t", "", "Target language") flag.StringVar(&toLang, "t", "", "Target language (default: unset)")
flag.StringVar(&text, "T", "", "Text to translate") flag.StringVar(&text, "T", "", "Text to translate (default: unset)")
} }
func main() { func main() {
// Start parsing // Start parsing
flag.Parse() flag.Parse()
// I assume this works?
if len(text) == 0 {
log.Printf("Text to translate is required. \n")
os.Exit(1)
} else if len(toLang) == 0 {
log.Printf("Target language is missing. \n")
os.Exit(1)
}
// Hand-craft the request URL // Hand-craft the request URL
queryURL := instanceURL + "?engine=" + engine + "&from=" + fromLang + "&to=" + toLang + "&text=" + text queryURL := instanceURL + "?engine=" + engine + "&from=" + fromLang + "&to=" + toLang + "&text=" + text
// Make a request to said URL // Make a request to said URL
resp, err := http.Get(queryURL) resp, err1 := http.Get(queryURL)
if err != nil { if err1 != nil {
panic(err) log.Printf("Request could not be processed due to %s\n", err1)
} }
defer resp.Body.Close() defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
output := body
output, err2 := io.ReadAll(resp.Body)
if err2 != nil {
panic(errno)
}
// Well...
fmt.Printf("Input: %s \n", text) fmt.Printf("Input: %s \n", text)
fmt.Printf("Output: %s \n", output) fmt.Printf("Output: %s \n", output)
} }
func printUsage() {
fmt.Printf("Usage for %s : \n", filepath.Base(os.Args[0]))
fmt.Printf("-e \t Use the specified translation engine (default: google)\n")
fmt.Printf("-f \t (default: auto)\n")
fmt.Printf("-t \t (default: none)\n")
fmt.Printf("-T \t Text to translate\n")
os.Exit(1)
}