Assume the instance is down if the translation output is blank

Signed-off-by: Izuru Yakumo <yakumo.izuru@chaotic.ninja>

git-svn-id: file:///srv/svn/repo/suwako/trunk@30 0b558ee1-521d-8b46-a41b-40029c97c055
This commit is contained in:
yakumo.izuru 2024-02-10 11:39:49 +00:00
parent 949bcf3208
commit 7cece8686a

View File

@ -1,4 +1,4 @@
// $TheSupernovaDuo: suwako,v 1.5.4 2024/01/14 21:21:50 yakumo_izuru Exp $ // $TheSupernovaDuo: suwako,v 1.5.5 2024/01/20 21:07:30 yakumo_izuru Exp $
// Command line client for SimplyTranslate, a privacy friendly frontend to other translation engines // Command line client for SimplyTranslate, a privacy friendly frontend to other translation engines
package main package main
@ -42,8 +42,8 @@ func iniLoad(file string) error {
if err != nil { if err != nil {
return err return err
} }
conf.engine = cfg.Section("simplytranslate").Key("engine").String() conf.engine = cfg.Section("suwako").Key("engine").String()
conf.instance = cfg.Section("simplytranslate").Key("instance").String() conf.instance = cfg.Section("suwako").Key("instance").String()
return nil return nil
} }
@ -65,9 +65,9 @@ func main() {
flagParse() flagParse()
// Load configuration file // Load configuration file
home, err := os.UserHomeDir() config, err := os.UserConfigDir()
errCheck(err) errCheck(err)
cfgfile := home + "/.suwako/suwako.conf" cfgfile := config + "/suwako/suwako.ini"
iniLoad(cfgfile) iniLoad(cfgfile)
// Verify command-line inputs // Verify command-line inputs
@ -82,7 +82,7 @@ func main() {
var encInput = url.PathEscape(input) var encInput = url.PathEscape(input)
// Construct the final path to query // Construct the final path to query
var queryURL = conf.instance + "?engine=" + conf.engine + "&from=" + source + "&to=" + target + "&text=" + encInput var queryURL = conf.instance + "/api/translate/" + "?engine=" + conf.engine + "&from=" + source + "&to=" + target + "&text=" + encInput
// Shoot danmaku to path // Shoot danmaku to path
resp, err := http.Get(queryURL) resp, err := http.Get(queryURL)
@ -92,5 +92,9 @@ func main() {
// Decode JSON response, discard everything else, print to standard output // Decode JSON response, discard everything else, print to standard output
_ = json.NewDecoder(resp.Body).Decode(&translate) _ = json.NewDecoder(resp.Body).Decode(&translate)
errCheck(err) errCheck(err)
fmt.Printf("%v\n",translate.Output) if len(translate.Output) == 0 {
log.Fatal("There was no output, maybe the server was down?")
} else {
fmt.Printf("%v\n", translate.Output)
}
} }