feat: stop hardcoding default instance and engine, also other minor changes.

git-svn-id: file:///srv/svn/repo/suwako/trunk@12 0b558ee1-521d-8b46-a41b-40029c97c055
This commit is contained in:
koizumi.aoi 2023-03-25 16:43:22 +00:00
parent 13be100b46
commit 393a3db9bc
3 changed files with 18 additions and 71 deletions

View File

@ -1,7 +1,16 @@
PREFIX=/usr/local
PREFIX ?= /usr/local
GOARCH ?= amd64
GOFLAGS ?= -v -ldflags "-w -X `go list`.Version=$(VERSION) -X `go list`.Commit=$(COMMIT) -X `go list`.Build=$(BUILD)" -tags "static_build"
GOOS ?= linux
BRANCH = `git rev-parse --abbrev-ref HEAD`
BUILD = `git show -s --pretty=format:%cI`
COMMIT = `git rev-parse --short HEAD || echo "$COMMIT"`
VERSION = `git describe --abbrev=0 --tags 2>/dev/null || echo "$VERSION"`
build:
go build
go build $(GOFLAGS) ./cmd/stcli
strip stcli
clean:
rm -f stcli

17
README
View File

@ -4,14 +4,11 @@ Command-line client for SimplyTranslate in Go.
Usage
-----
-I string
Enter the text to be translated
-e string
Translation engine to use (default: google) (default "google")
-f string
Set the language to translate from. This can be skipped as it will autodetect the language you're translating from (default "auto")
-i string
Instance to use (default: https://simplytranslate.org/api/translate/) (default "https://simplytranslate.org/api/translate/")
-t string
Set the language to translate to (default: en) (default "en")
-f [lang] (default: auto)
-i [input]
-t [lang] (default: en)
Environment variables
---------------------
* STCLI_ENGINE
* STCLI_INSTANCE

59
main.go
View File

@ -1,59 +0,0 @@
// $TheSupernovaDuo: stcli,v 1.4.0 2023/03/13 08:14:55 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"
)
var (
engine string
instance string
input string
source string
target string
)
type Translate struct {
Output string `json:"translated-text"`
}
func init() {
flag.StringVar(&engine, "e", "google", "Translation engine to use")
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(&instance, "i", "https://translate.bus-hit.me", "Instance to use)")
flag.StringVar(&input, "I", "", "Enter the text to be translated")
flag.StringVar(&target, "t", "en", "Set the language to translate to")
}
func main() {
// Begin flag parsing
flag.Parse()
// Check if there's any input, otherwise bail out.
if len(input) == 0 {
log.Fatal("Missing input.")
}
// 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)
}
}