Ready for 1.1

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

git-svn-id: file:///srv/svn/repo/suwako/trunk@5 0b558ee1-521d-8b46-a41b-40029c97c055
This commit is contained in:
koizumi.aoi
2022-12-13 13:18:40 +00:00
parent 15ac19196e
commit eef6ce394a
3 changed files with 52 additions and 25 deletions

View File

@@ -1,4 +1,11 @@
PREFIX=/usr/local
build: build:
go build go build
clean: clean:
rm -f stcli-go rm -f stcli-go
install: build
install -Dm0755 stcli-go ${PREFIX}/bin/stcli-go
uninstall:
rm -f ${PREFIX}/bin/stcli-go
rm -f ${PREFIX}/share/man/man1/stcli-go.1

46
main.go
View File

@@ -1,3 +1,5 @@
// $KyokoNet: stcli-go,v 1.1 2022/12/13 10:07:00 akoizumi Exp
// Command line client for SimplyTranslate, a privacy friendly frontend to Google Translate
package main package main
import ( import (
@@ -9,42 +11,46 @@ import (
"net/http" "net/http"
"os" "os"
) )
var ( var (
instanceURL string
engine string engine string
fromLang string from string
toLang string instance string
text string input string
to string
) )
type Translate struct {
type TranslateAPI struct { Output string `json:"translated-text"`
OutText string `json:"translated-text"`
} }
func init() { func init() {
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 to use (default: google)")
flag.StringVar(&engine, "e", "google", "Translation engine (default: google)") flag.StringVar(&from, "f", "auto", "Set the language to translate from. This can be skipped as it will autodetect the language you're translating from")
flag.StringVar(&fromLang, "f", "auto", "Source language (default: auto)") flag.StringVar(&instance, "i", "https://simplytranslate.org/api/translate/", "Instance to use (default: https://simplytranslate.org/api/translate/)")
flag.StringVar(&toLang, "t", "", "Target language (default: unset)") flag.StringVar(&input, "I", "", "Enter the text to be translated")
flag.StringVar(&text, "T", "", "Text to translate (default: unset)") flag.StringVar(&to, "t", "en", "Set the language to translate to (default: en)")
} }
func main() { func main() {
// Begin flag parsing
flag.Parse() flag.Parse()
if len(text) == 0 || len(toLang) == 0 { // Check if any of those two variables is empty.
// It actually needs the two to have content.
if len(input) == 0 || len(to) == 0 {
log.Fatal("Missing either the text or the target language.") log.Fatal("Missing either the text or the target language.")
os.Exit(1) os.Exit(1)
} }
var o TranslateAPI // Map a variable to the struct
var queryURL = instanceURL + "?engine=" + engine + "&from=" + fromLang + "&to=" + toLang + "&text=" + text var translate Translate
// Build the full URL to query
var queryURL = instance + "?engine=" + engine + "&from=" + from + "&to=" + to + "&text=" + input
// Begin the request and process the response
req, err := http.Get(queryURL) req, err := http.Get(queryURL)
sanityCheck(err) sanityCheck(err)
defer req.Body.Close() defer req.Body.Close()
resp, err := io.ReadAll(req.Body) resp, err := io.ReadAll(req.Body)
_ = json.Unmarshal([]byte(resp), &o) _ = json.Unmarshal([]byte(resp), &translate)
sanityCheck(err) sanityCheck(err)
fmt.Printf("Input: %s (%s)\n",text,fromLang) // Pretty-print both the input and the output given.
fmt.Printf("Output: %s (%s)\n",o.OutText,toLang) fmt.Printf("Input: %v\n", input)
fmt.Printf("Output: %v\n",translate.Output)
} }
func sanityCheck(err error) { func sanityCheck(err error) {
if err != nil { if err != nil {

View File

@@ -1,4 +1,4 @@
.Dd Aftermath 55, 3188 .Dd Aftermath 56, 3188
.Dt STCLI-GO 1 .Dt STCLI-GO 1
.Os .Os
.Sh NAME .Sh NAME
@@ -9,20 +9,34 @@
.Fl e Ar engine .Fl e Ar engine
.Fl f Ar from .Fl f Ar from
.Fl i Ar instance .Fl i Ar instance
.Fl I Ar input
.Fl t Ar to .Fl t Ar to
.Fl T Ar text
.Sh DESCRIPTION .Sh DESCRIPTION
Self-explanatory, besides, this was made as Self-explanatory, besides, this was made as
a rewrite from a shell script that had curl a rewrite from a shell script that had curl
and awk for dependencies. It fully serves and awk for dependencies. It fully serves
as a drop-in replacement. as a drop-in replacement.
.Sh USAGE
.Bl -tag -width 11n -compact
.It -e
Translation engine to use
.It -f
Input language to translate from
.It -i
Instance to use
.It -I
Text to translate
.It -t
Target language to translate to
.El
.Sh AUTHORS .Sh AUTHORS
.An Aoi K. Aq Mt koizumi.aoi@kyoko-project.wer.ee .An Aoi K. Aq Mt koizumi.aoi@kyoko-project.wer.ee
.Pp .Pp
.An Czar of KST Aq Mt czar@kalli.st .An Czar of KST Aq Mt czar@kalli.st
.Pp .Pp
.An mutefall
.Pp
.An Shokara Kou Aq Mt kou@clearnet.fqdn .An Shokara Kou Aq Mt kou@clearnet.fqdn
.Pp .Pp
.An Baobab Aq Mt baobab@honeypot.im .An Baobab Aq Mt baobab@honeypot.im
.Sh BUGS
This utility cannot translate strings
separated by spaces.