Compare commits
10 Commits
e644ccc52d
...
a31a857422
Author | SHA1 | Date | |
---|---|---|---|
![]() |
a31a857422 | ||
![]() |
3ee59b76b3 | ||
![]() |
bf7dd2f338 | ||
![]() |
490002645d | ||
![]() |
014b3db174 | ||
![]() |
6e1896e1ab | ||
![]() |
55398bd96d | ||
![]() |
088e21de84 | ||
![]() |
1fb03b04a2 | ||
![]() |
16598cc10c |
3
.gitignore
vendored
3
.gitignore
vendored
@ -1 +1,2 @@
|
||||
*
|
||||
!/cmd/suwako
|
||||
suwako
|
||||
|
12
COPYING
12
COPYING
@ -1,4 +1,8 @@
|
||||
Freedom License v1 (2021年08月17日)
|
||||
|
||||
全く無限的自由です。
|
||||
It's infinite freedom.
|
||||
/*
|
||||
* ----------------------------------------------------------------------------
|
||||
* "THE BEER-WARE LICENSE" (Revision 42.1):
|
||||
* <yakumo.izuru@chaotic.ninja> wrote this file. As long as you retain this notice you
|
||||
* can do whatever you want with this stuff. If we meet some day, and you think
|
||||
* this stuff is worth it, you can buy me a bottle of sake in return Izuru Yakumo
|
||||
* ----------------------------------------------------------------------------
|
||||
*/
|
||||
|
3
Makefile
3
Makefile
@ -1,8 +1,6 @@
|
||||
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`
|
||||
@ -18,6 +16,7 @@ clean:
|
||||
install:
|
||||
install -Dm0755 suwako ${PREFIX}/bin/suwako
|
||||
install -Dm0644 suwako.1 ${PREFIX}/share/man/man1/suwako.1
|
||||
install -Dm0644 suwako.conf.5 ${PREFIX}/share/man/man5/suwako.conf.5
|
||||
uninstall:
|
||||
rm -f ${PREFIX}/bin/suwako
|
||||
rm -f ${PREFIX}/share/man/man1/suwako.1
|
||||
|
57
README.md
57
README.md
@ -1,11 +1,50 @@
|
||||
# suwako
|
||||
Command-line client for privacy-friendly translation frontends.
|
||||
SUWAKO(1) - FreeBSD General Commands Manual
|
||||
|
||||
## Usage
|
||||
-f [lang] (default: auto)
|
||||
-i [input]
|
||||
-t [lang] (default: en)
|
||||
# NAME
|
||||
|
||||
## Environment variables
|
||||
* SUWAKO\_ENGINE
|
||||
* SUWAKO\_INSTANCE
|
||||
**suwako** - Command-line client for SimplyTranslate
|
||||
|
||||
# SYNOPSIS
|
||||
|
||||
**suwako**
|
||||
\[**-f** *from*]
|
||||
\[**-t** *to*]
|
||||
\[*input*]
|
||||
|
||||
# DESCRIPTION
|
||||
|
||||
Self-explanatory, besides, this was made as
|
||||
a rewrite from a shell script that had curl
|
||||
and awk for dependencies.
|
||||
It fully serves
|
||||
as a drop-in replacement.
|
||||
|
||||
# USAGE
|
||||
|
||||
**-f**
|
||||
|
||||
> Input language to translate from.
|
||||
> Default is 'auto'
|
||||
|
||||
**-t**
|
||||
|
||||
> Target language to translate to
|
||||
|
||||
<input>
|
||||
|
||||
> Text to be translated
|
||||
|
||||
# SEE ALSO
|
||||
|
||||
suwako.conf(5)
|
||||
|
||||
# AUTHORS
|
||||
|
||||
Izuru Yakumo <[yakumo.izuru@chaotic.ninja](mailto:yakumo.izuru@chaotic.ninja)>
|
||||
|
||||
# BUGS
|
||||
|
||||
You cannot translate the string "version", this is
|
||||
a direct consequence of using flaggy.
|
||||
|
||||
FreeBSD 13.2-RELEASE-p4 - December 16, 2023
|
||||
|
@ -1,68 +1,100 @@
|
||||
// $TheSupernovaDuo: suwako,v 1.5.1 2023/4/15 23:9:28 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
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"marisa.chaotic.ninja/suwako"
|
||||
"os"
|
||||
|
||||
"github.com/integrii/flaggy"
|
||||
"gopkg.in/ini.v1"
|
||||
"marisa.chaotic.ninja/suwako"
|
||||
)
|
||||
|
||||
var (
|
||||
var conf struct {
|
||||
engine string
|
||||
instance string
|
||||
}
|
||||
|
||||
var (
|
||||
input string
|
||||
source string
|
||||
source string = "auto"
|
||||
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.Println("SUWAKO_ENGINE and/or SUWAKO_INSTANCE are unset")
|
||||
log.Println("Defaulting to simplytranslate.org with engine 'google'")
|
||||
engine = "google"
|
||||
instance = "https://simplytranslate.org"
|
||||
}
|
||||
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)
|
||||
fmt.Printf("%v\n",translate.Output)
|
||||
}
|
||||
func sanityCheck(err error) {
|
||||
func errCheck(err error) {
|
||||
if err != nil {
|
||||
log.Println("Something happened :(")
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func iniLoad(file string) error {
|
||||
cfg, err := ini.Load(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
conf.engine = cfg.Section("suwako").Key("engine").String()
|
||||
conf.instance = cfg.Section("suwako").Key("instance").String()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func flagParse() {
|
||||
flaggy.SetName("suwako")
|
||||
flaggy.SetDescription("Command line client for SimplyTranslate")
|
||||
flaggy.SetVersion(suwako.FullVersion())
|
||||
|
||||
flaggy.String(&source, "f", "from", "Source language")
|
||||
flaggy.String(&target, "t", "to", "Target language")
|
||||
flaggy.AddPositionalValue(&input, "input", 1, true, "Text to translate")
|
||||
|
||||
flaggy.Parse()
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Flag parsing
|
||||
flagParse()
|
||||
|
||||
// Load configuration file
|
||||
config, err := os.UserConfigDir()
|
||||
errCheck(err)
|
||||
cfgfile := config + "/suwako/suwako.ini"
|
||||
iniLoad(cfgfile)
|
||||
|
||||
// Verify command-line inputs
|
||||
if len(target) == 0 {
|
||||
log.Fatal("No target language")
|
||||
}
|
||||
|
||||
// Map variable to struct
|
||||
var translate Translate
|
||||
|
||||
// Encode input just in case
|
||||
var encInput = url.PathEscape(input)
|
||||
|
||||
// Construct the final path to query
|
||||
var queryURL = conf.instance + "/api/translate/" + "?engine=" + conf.engine + "&from=" + source + "&to=" + target + "&text=" + encInput
|
||||
|
||||
// Shoot danmaku to path
|
||||
resp, err := http.Get(queryURL)
|
||||
errCheck(err)
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Decode JSON response, discard everything else, print to standard output
|
||||
_ = json.NewDecoder(resp.Body).Decode(&translate)
|
||||
errCheck(err)
|
||||
if len(translate.Output) == 0 {
|
||||
log.Fatal("There was no output, maybe the server was down?")
|
||||
} else {
|
||||
fmt.Printf("%v\n", translate.Output)
|
||||
}
|
||||
}
|
||||
|
2
doc.go
Normal file
2
doc.go
Normal file
@ -0,0 +1,2 @@
|
||||
// package suwako is yet another client for SimplyTranslate https://simple-web.org/projects/simplytranslate.html written in Go.
|
||||
package suwako
|
7
go.mod
7
go.mod
@ -1,3 +1,10 @@
|
||||
module marisa.chaotic.ninja/suwako
|
||||
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/integrii/flaggy v1.5.2
|
||||
gopkg.in/ini.v1 v1.67.0
|
||||
)
|
||||
|
||||
require github.com/stretchr/testify v1.8.4 // indirect
|
||||
|
12
go.sum
12
go.sum
@ -0,0 +1,12 @@
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
|
||||
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/integrii/flaggy v1.5.2 h1:bWV20MQEngo4hWhno3i5Z9ISPxLPKj9NOGNwTWb/8IQ=
|
||||
github.com/integrii/flaggy v1.5.2/go.mod h1:dO13u7SYuhk910nayCJ+s1DeAAGC1THCMj1uSFmwtQ8=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
|
||||
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
38
suwako.1
38
suwako.1
@ -1,16 +1,14 @@
|
||||
.Dd $Mdocdate$
|
||||
.Dt STCLI 1
|
||||
.Dt SUWAKO 1
|
||||
.Os
|
||||
.Sh NAME
|
||||
.Nm suwako
|
||||
.Nd Command-line client for privacy friendly translation frontends
|
||||
.Nd Command-line client for SimplyTranslate
|
||||
.Sh SYNOPSIS
|
||||
.Nm
|
||||
.Fl e Ar engine
|
||||
.Fl f Ar from
|
||||
.Fl i Ar instance
|
||||
.Fl I Ar input
|
||||
.Fl t Ar to
|
||||
.Op Fl f Ar from
|
||||
.Op Fl t Ar to
|
||||
.Op Ar input
|
||||
.Sh DESCRIPTION
|
||||
Self-explanatory, besides, this was made as
|
||||
a rewrite from a shell script that had curl
|
||||
@ -19,24 +17,18 @@ It fully serves
|
||||
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
|
||||
.It Fl f
|
||||
Input language to translate from.
|
||||
Default is 'auto'
|
||||
.It Fl t
|
||||
Target language to translate to
|
||||
.It <input>
|
||||
Text to be translated
|
||||
.El
|
||||
.Sh SEE ALSO
|
||||
.Xr suwako.conf 5
|
||||
.Sh AUTHORS
|
||||
.An Izuru Yakumo Aq Mt yakumo.izuru@chaotic.ninja
|
||||
.Pp
|
||||
.An Czar of KST Aq Mt czar@kalli.st
|
||||
.Pp
|
||||
.An Shokara Kou Aq Mt kou@clearnet.fqdn
|
||||
.Sh BUGS
|
||||
None so far.
|
||||
If otherwise, please report them to
|
||||
.Mt devel@chaotic.ninja
|
||||
You cannot translate the string "version", this is
|
||||
a direct consequence of using flaggy.
|
||||
|
36
suwako.conf.5
Normal file
36
suwako.conf.5
Normal file
@ -0,0 +1,36 @@
|
||||
.Dd $Mdocdate$
|
||||
.Dt SUWAKO.CONF 5
|
||||
.Os
|
||||
.Sh NAME
|
||||
.Nm suwako.conf
|
||||
.Nd INI-style configuration file for
|
||||
.Xr suwako 1
|
||||
.Sh DESCRIPTION
|
||||
The
|
||||
.Nm
|
||||
file specifies the instance address,
|
||||
including the API path and the
|
||||
translation engine to be used on
|
||||
the
|
||||
.Xr suwako 1
|
||||
command.
|
||||
.Sh OPTIONS
|
||||
.Bl -tag -width 11n -compact
|
||||
.It instance
|
||||
Contains the HTTPS URI to the
|
||||
server's up to the
|
||||
.Sy /api/translate
|
||||
endpoint.
|
||||
.It engine
|
||||
For most use cases, the
|
||||
.Em google
|
||||
engine should be enough,
|
||||
though some endpoints
|
||||
support more translation
|
||||
engines.
|
||||
.El
|
||||
.Sh FILES
|
||||
.Pa ~/.suwako/suwako.conf
|
||||
path to this file
|
||||
.Sh AUTHORS
|
||||
.An Izuru Yakumo Aq Mt yakumo.izuru@chaotic.ninja
|
Loading…
x
Reference in New Issue
Block a user