[31] | 1 | /*
|
---|
| 2 | Copyright © 2025 Izuru Yakumo <eternal-servant@yakumo.dev>
|
---|
| 3 |
|
---|
| 4 | */
|
---|
| 5 | package cmd
|
---|
| 6 |
|
---|
| 7 | import (
|
---|
| 8 | "encoding/json"
|
---|
| 9 | "fmt"
|
---|
| 10 | "log"
|
---|
| 11 | "net/http"
|
---|
| 12 | "net/url"
|
---|
| 13 | "os"
|
---|
| 14 | "strings"
|
---|
| 15 |
|
---|
| 16 | "github.com/spf13/cobra"
|
---|
| 17 | "gopkg.in/ini.v1"
|
---|
| 18 | )
|
---|
| 19 |
|
---|
| 20 | var (
|
---|
| 21 | cfgFile string
|
---|
| 22 | input string
|
---|
| 23 | output string
|
---|
| 24 | )
|
---|
| 25 |
|
---|
| 26 | var conf struct {
|
---|
| 27 | endpoint string
|
---|
| 28 | engine string
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | type Translate struct {
|
---|
| 32 | Output string `json:"translated_text"`
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | var rootCmd = &cobra.Command{
|
---|
| 36 | Use: "suwako",
|
---|
| 37 | Short: "A client for SimplyTranslate with illusionary origins",
|
---|
| 38 | Args: cobra.MinimumNArgs(1),
|
---|
| 39 | Run: func(cmd *cobra.Command, args []string) {
|
---|
| 40 | _split := fmt.Sprint(args)
|
---|
| 41 | text := fmt.Sprint(strings.Trim(_split, "[]"))
|
---|
| 42 | if len(output) == 0 {
|
---|
| 43 | log.Fatalf("\033[1;31m%s\033[0m\n", "No target language")
|
---|
| 44 | }
|
---|
| 45 | var translate Translate
|
---|
| 46 | encInput := url.PathEscape(text)
|
---|
| 47 | queryURL := conf.endpoint + "/api/translate" + "?engine=" + conf.engine + "&from=" + input + "&to=" + output + "&text=" + encInput
|
---|
| 48 | resp, err := http.Get(queryURL)
|
---|
| 49 | CheckErr(err)
|
---|
| 50 | defer resp.Body.Close()
|
---|
| 51 |
|
---|
| 52 | _ = json.NewDecoder(resp.Body).Decode(&translate)
|
---|
| 53 | CheckErr(err)
|
---|
| 54 | if len(translate.Output) == 0 {
|
---|
| 55 | log.Fatalf("\033[1;31m%s\033[0m\n", "There was no output, maybe the endpoint is down?")
|
---|
| 56 | } else {
|
---|
| 57 | fmt.Printf("%v\n", translate.Output)
|
---|
| 58 | }
|
---|
| 59 | },
|
---|
| 60 | Version: "3.0.0",
|
---|
| 61 | }
|
---|
| 62 | func CheckErr(err error) {
|
---|
| 63 | if err != nil {
|
---|
| 64 | log.Fatal(err)
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 | func Execute() {
|
---|
| 68 | err := rootCmd.Execute()
|
---|
| 69 | if err != nil {
|
---|
| 70 | os.Exit(1)
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | func init() {
|
---|
| 75 | cobra.OnInitialize(initConfig)
|
---|
| 76 | rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $XDG_CONFIG_HOME/suwako.ini)")
|
---|
| 77 | rootCmd.PersistentFlags().StringVarP(&input, "from", "f", "auto", "language to translate from (default is auto)")
|
---|
| 78 | rootCmd.PersistentFlags().StringVarP(&output, "to", "t", "", "target language")
|
---|
| 79 | }
|
---|
| 80 | func parseConfig(file string) error {
|
---|
| 81 | cfg, err := ini.Load(file)
|
---|
| 82 | CheckErr(err)
|
---|
| 83 |
|
---|
| 84 | conf.endpoint = cfg.Section("suwako").Key("endpoint").String()
|
---|
| 85 | conf.engine = cfg.Section("suwako").Key("engine").String()
|
---|
| 86 |
|
---|
| 87 | return nil
|
---|
| 88 | }
|
---|
| 89 | func initConfig() {
|
---|
| 90 | if cfgFile != "" {
|
---|
| 91 | parseConfig(cfgFile)
|
---|
| 92 |
|
---|
| 93 | } else {
|
---|
| 94 | // Find home directory.
|
---|
| 95 | xdg, err := os.UserConfigDir()
|
---|
| 96 | CheckErr(err)
|
---|
| 97 | defaultCfgPath := xdg + "/suwako.ini"
|
---|
| 98 | parseConfig(defaultCfgPath)
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | }
|
---|