Changeset 5 in code for trunk/main.go


Ignore:
Timestamp:
Dec 13, 2022, 1:18:40 PM (2 years ago)
Author:
koizumi.aoi
Message:

Ready for 1.1

Signed-off-by: Aoi K <koizumi.aoi@…>

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/main.go

    r4 r5  
     1// $KyokoNet: stcli-go,v 1.1 2022/12/13 10:07:00 akoizumi Exp
     2// Command line client for SimplyTranslate, a privacy friendly frontend to Google Translate
    13package main
    24
     
    1012        "os"
    1113)
    12 
    1314var (
    14         instanceURL string
    15         engine      string
    16         fromLang    string
    17         toLang      string
    18         text        string
     15        engine string
     16        from string
     17        instance string
     18        input string
     19        to string
    1920)
    20 
    21 type TranslateAPI struct {
    22         OutText string `json:"translated-text"`
     21type Translate struct {
     22        Output string `json:"translated-text"`
    2323}
    24 
    2524func init() {
    26         flag.StringVar(&instanceURL, "i", "https://translate.bus-hit.me/api/translate/", "Instance for SimplyTranslate (default: https://translate.bus-hit.me)")
    27         flag.StringVar(&engine, "e", "google", "Translation engine (default: google)")
    28         flag.StringVar(&fromLang, "f", "auto", "Source language (default: auto)")
    29         flag.StringVar(&toLang, "t", "", "Target language (default: unset)")
    30         flag.StringVar(&text, "T", "", "Text to translate (default: unset)")
     25        flag.StringVar(&engine, "e", "google", "Translation engine to use (default: google)")
     26        flag.StringVar(&from, "f", "auto", "Set the language to translate from. This can be skipped as it will autodetect the language you're translating from")
     27        flag.StringVar(&instance, "i", "https://simplytranslate.org/api/translate/", "Instance to use (default: https://simplytranslate.org/api/translate/)")
     28        flag.StringVar(&input, "I", "", "Enter the text to be translated")
     29        flag.StringVar(&to, "t", "en", "Set the language to translate to (default: en)")
    3130}
    3231func main() {
     32        // Begin flag parsing
    3333        flag.Parse()
    34         if len(text) == 0 || len(toLang) == 0 {
     34        // Check if any of those two variables is empty.
     35        // It actually needs the two to have content.
     36        if len(input) == 0 || len(to) == 0 {
    3537                log.Fatal("Missing either the text or the target language.")
    3638                os.Exit(1)
    3739        }
    38         var o TranslateAPI
    39         var queryURL = instanceURL + "?engine=" + engine + "&from=" + fromLang + "&to=" + toLang + "&text=" + text
     40        // Map a variable to the struct
     41        var translate Translate
     42        // Build the full URL to query
     43        var queryURL = instance + "?engine=" + engine + "&from=" + from + "&to=" + to + "&text=" + input
     44        // Begin the request and process the response
    4045        req, err := http.Get(queryURL)
    4146        sanityCheck(err)
    4247        defer req.Body.Close()
    4348        resp, err := io.ReadAll(req.Body)
    44         _ = json.Unmarshal([]byte(resp), &o)
     49        _ = json.Unmarshal([]byte(resp), &translate)
    4550        sanityCheck(err)
    46         fmt.Printf("Input: %s (%s)\n",text,fromLang)
    47         fmt.Printf("Output: %s (%s)\n",o.OutText,toLang)
     51        // Pretty-print both the input and the output given.
     52        fmt.Printf("Input: %v\n", input)
     53        fmt.Printf("Output: %v\n",translate.Output)
    4854}
    4955func sanityCheck(err error) {
Note: See TracChangeset for help on using the changeset viewer.