すべてを書き直し、3.0.0をリリースします
git-svn-id: file:///srv/svn/repo/suwako/trunk@31 0b558ee1-521d-8b46-a41b-40029c97c055
This commit is contained in:
101
cmd/root.go
Normal file
101
cmd/root.go
Normal file
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
Copyright © 2025 Izuru Yakumo <eternal-servant@yakumo.dev>
|
||||
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"gopkg.in/ini.v1"
|
||||
)
|
||||
|
||||
var (
|
||||
cfgFile string
|
||||
input string
|
||||
output string
|
||||
)
|
||||
|
||||
var conf struct {
|
||||
endpoint string
|
||||
engine string
|
||||
}
|
||||
|
||||
type Translate struct {
|
||||
Output string `json:"translated_text"`
|
||||
}
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "suwako",
|
||||
Short: "A client for SimplyTranslate with illusionary origins",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
_split := fmt.Sprint(args)
|
||||
text := fmt.Sprint(strings.Trim(_split, "[]"))
|
||||
if len(output) == 0 {
|
||||
log.Fatalf("\033[1;31m%s\033[0m\n", "No target language")
|
||||
}
|
||||
var translate Translate
|
||||
encInput := url.PathEscape(text)
|
||||
queryURL := conf.endpoint + "/api/translate" + "?engine=" + conf.engine + "&from=" + input + "&to=" + output + "&text=" + encInput
|
||||
resp, err := http.Get(queryURL)
|
||||
CheckErr(err)
|
||||
defer resp.Body.Close()
|
||||
|
||||
_ = json.NewDecoder(resp.Body).Decode(&translate)
|
||||
CheckErr(err)
|
||||
if len(translate.Output) == 0 {
|
||||
log.Fatalf("\033[1;31m%s\033[0m\n", "There was no output, maybe the endpoint is down?")
|
||||
} else {
|
||||
fmt.Printf("%v\n", translate.Output)
|
||||
}
|
||||
},
|
||||
Version: "3.0.0",
|
||||
}
|
||||
func CheckErr(err error) {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
func Execute() {
|
||||
err := rootCmd.Execute()
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
cobra.OnInitialize(initConfig)
|
||||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $XDG_CONFIG_HOME/suwako.ini)")
|
||||
rootCmd.PersistentFlags().StringVarP(&input, "from", "f", "auto", "language to translate from (default is auto)")
|
||||
rootCmd.PersistentFlags().StringVarP(&output, "to", "t", "", "target language")
|
||||
}
|
||||
func parseConfig(file string) error {
|
||||
cfg, err := ini.Load(file)
|
||||
CheckErr(err)
|
||||
|
||||
conf.endpoint = cfg.Section("suwako").Key("endpoint").String()
|
||||
conf.engine = cfg.Section("suwako").Key("engine").String()
|
||||
|
||||
return nil
|
||||
}
|
||||
func initConfig() {
|
||||
if cfgFile != "" {
|
||||
parseConfig(cfgFile)
|
||||
|
||||
} else {
|
||||
// Find home directory.
|
||||
xdg, err := os.UserConfigDir()
|
||||
CheckErr(err)
|
||||
defaultCfgPath := xdg + "/suwako.ini"
|
||||
parseConfig(defaultCfgPath)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,100 +0,0 @@
|
||||
// $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"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"github.com/integrii/flaggy"
|
||||
"gopkg.in/ini.v1"
|
||||
"marisa.chaotic.ninja/suwako"
|
||||
)
|
||||
|
||||
var conf struct {
|
||||
engine string
|
||||
instance string
|
||||
}
|
||||
|
||||
var (
|
||||
input string
|
||||
source string = "auto"
|
||||
target string
|
||||
)
|
||||
|
||||
type Translate struct {
|
||||
Output string `json:"translated_text"`
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user