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