Re-add the command. What a mistake I did
Signed-off-by: Izuru Yakumo <yakumo.izuru@chaotic.ninja> git-svn-id: file:///srv/svn/repo/suwako/trunk@15 0b558ee1-521d-8b46-a41b-40029c97c055
This commit is contained in:
parent
5ca8bab3ad
commit
e5f73bfde0
67
cmd/suwako/main.go
Normal file
67
cmd/suwako/main.go
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
// $TheSupernovaDuo: suwako,v 1.5.0 2023/03/25 10:41:00 akoizumi Exp
|
||||||
|
// Command line client for SimplyTranslate, a privacy friendly frontend to other translation engines
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"marisa.chaotic.ninja/suwako"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
engine string
|
||||||
|
instance string
|
||||||
|
input string
|
||||||
|
source string
|
||||||
|
target string
|
||||||
|
)
|
||||||
|
type Translate struct {
|
||||||
|
Output string `json:"translated-text"`
|
||||||
|
}
|
||||||
|
func init() {
|
||||||
|
flag.StringVar(&source, "f", "auto", "Set the language to translate from. This can be skipped as it will autodetect the language you're translating from")
|
||||||
|
flag.StringVar(&input, "i", "", "Enter the text to be translated")
|
||||||
|
flag.StringVar(&target, "t", "en", "Set the language to translate to")
|
||||||
|
flag.Usage = func() {
|
||||||
|
fmt.Printf("usage: suwako -f [lang] -i [text] -t [lang]\nversion: %v\n", suwako.FullVersion())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func main() {
|
||||||
|
engine = os.Getenv("SUWAKO_ENGINE")
|
||||||
|
instance = os.Getenv("SUWAKO_INSTANCE")
|
||||||
|
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
if len(engine) == 0 || len(instance) == 0 {
|
||||||
|
log.Fatal("SUWAKO_ENGINE and/or SUWAKO_INSTANCE are unset")
|
||||||
|
}
|
||||||
|
if len(input) == 0 {
|
||||||
|
log.Fatal("Missing input text.")
|
||||||
|
}
|
||||||
|
// Map a variable to the struct
|
||||||
|
var translate Translate
|
||||||
|
// Build the full URL to query
|
||||||
|
var encInput = url.PathEscape(input)
|
||||||
|
var apiEndpoint = "/api/translate"
|
||||||
|
var queryURL = instance + apiEndpoint + "?engine=" + engine + "&from=" + source + "&to=" + target + "&text=" + encInput
|
||||||
|
// Begin the request and process the response
|
||||||
|
resp, err := http.Get(queryURL)
|
||||||
|
sanityCheck(err)
|
||||||
|
defer resp.Body.Close()
|
||||||
|
// JSON decoding
|
||||||
|
_ = json.NewDecoder(resp.Body).Decode(&translate)
|
||||||
|
sanityCheck(err)
|
||||||
|
// Pretty-print both the input and the output given.
|
||||||
|
fmt.Printf("Input: %v\n", input)
|
||||||
|
fmt.Printf("Output: %v\n",translate.Output)
|
||||||
|
}
|
||||||
|
func sanityCheck(err error) {
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user