Changeset 10 in code for trunk/main.go


Ignore:
Timestamp:
Mar 13, 2023, 11:17:03 AM (2 years ago)
Author:
koizumi.aoi
Message:

rep: hard-coding api endpoint, change variable names to be less confusing, etc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/main.go

    r9 r10  
    1 // $TheSupernovaDuo: stcli,v 1.3.1 2023/02/11 07:54:25 akoizumi Exp
     1// $TheSupernovaDuo: stcli,v 1.4.0 2023/03/13 08:14:55 akoizumi Exp
    22// Command line client for SimplyTranslate, a privacy friendly frontend to other translation engines
    33package main
     
    1111        "net/url"
    1212)
     13
    1314var (
    1415        engine string
    15         from string
    1616        instance string
    1717        input string
    18         to string
     18        source string
     19        target string
    1920)
    2021type Translate struct {
     
    2223}
    2324func init() {
    24         flag.StringVar(&engine, "e", "google", "Translation engine to use (default: google)")
    25         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")
    26         flag.StringVar(&instance, "i", "https://simplytranslate.org/api/translate/", "Instance to use (default: https://simplytranslate.org/api/translate/)")
     25        flag.StringVar(&engine, "e", "google", "Translation engine to use")
     26        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")
     27        flag.StringVar(&instance, "i", "https://translate.bus-hit.me", "Instance to use)")
    2728        flag.StringVar(&input, "I", "", "Enter the text to be translated")
    28         flag.StringVar(&to, "t", "en", "Set the language to translate to (default: en)")
     29        flag.StringVar(&target, "t", "en", "Set the language to translate to")
    2930}
    3031func main() {
    3132        // Begin flag parsing
    3233        flag.Parse()
    33         // Check if any of those two variables is empty.
    34         // It actually needs the two to have content.
     34        // Check if there's any input, otherwise bail out.
    3535        if len(input) == 0 {
    3636                log.Fatal("Missing input.")
     
    3939        var translate Translate
    4040        // Build the full URL to query
    41         var encinput = url.PathEscape(input)
    42         var queryURL = instance + "?engine=" + engine + "&from=" + from + "&to=" + to + "&text=" + encinput
     41        var encInput = url.PathEscape(input)
     42        var apiEndpoint = "/api/translate"
     43        var queryURL = instance + apiEndpoint + "?engine=" + engine + "&from=" + source + "&to=" + target + "&text=" + encInput
    4344        // Begin the request and process the response
    4445        resp, err := http.Get(queryURL)
Note: See TracChangeset for help on using the changeset viewer.