Changeset 22 in code


Ignore:
Timestamp:
Oct 22, 2023, 1:07:46 AM (20 months ago)
Author:
yakumo.izuru
Message:

Refactor, now require instance URL to include API endpoint

Signed-off-by: Izuru Yakumo <yakumo.izuru@…>

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/cmd/suwako/main.go

    r21 r22  
    1 // $TheSupernovaDuo: suwako,v 1.5.1 2023/4/15 23:9:28 yakumo_izuru Exp $
     1// $TheSupernovaDuo: suwako,v 1.5.2 2023/10/21 00:58:44 yakumo_izuru Exp $
    22// Command line client for SimplyTranslate, a privacy friendly frontend to other translation engines
    33package main
     
    2323        target string
    2424)
     25
    2526type Translate struct {
    2627        Output string `json:"translated_text"`
    2728}
     29
     30func errCheck(err error) {
     31        if err != nil {
     32                log.Fatal("Something happened :(", err)
     33        }
     34}
     35
    2836func loadCfg() {
    2937        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")
     
    3644
    3745        home, err := os.UserHomeDir()
    38         sanityCheck(err)
     46        errCheck(err)
    3947        conf := home + "/.suwako/suwako.conf"
    4048        err = godotenv.Load(conf)
    41         sanityCheck(err)
     49        errCheck(err)
    4250
    4351        engine = os.Getenv("SUWAKO_ENGINE")
     
    4654        flag.Parse()
    4755}
    48 func check() {
    49         if len(input) == 0 || len(target) == 0 {
    50                 log.Fatal("Either there is no input or there is no target language")
     56
     57func main() {
     58        // Load configuration and parse flags
     59        loadCfg()
     60
     61        // Verify command-line inputs
     62        if len(input) == 0 {
     63                log.Fatal("There is no input")
    5164        }
    52 }
    53 func main() {
    54         loadCfg()
    55         check()
     65        if len(target) == 0 {
     66                log.Fatal("No target language")
     67        }
    5668       
     69        // Map variable to struct
    5770        var translate Translate
     71
     72        // Encode input just in case
    5873        var encInput = url.PathEscape(input)
    59         var apiEndpoint = "/api/translate"
    60         var queryURL = instance + apiEndpoint + "?engine=" + engine + "&from=" + source + "&to=" + target + "&text=" + encInput
    6174
     75        // Construct the final path to query
     76        var queryURL = instance + "?engine=" + engine + "&from=" + source + "&to=" + target + "&text=" + encInput
     77
     78        // Shoot danmaku to path
    6279        resp, err := http.Get(queryURL)
    63         sanityCheck(err)
     80        errCheck(err)
    6481        defer resp.Body.Close()
     82
     83        // Decode JSON response, discard everything else, print to standard output
    6584        _ = json.NewDecoder(resp.Body).Decode(&translate)
    66         sanityCheck(err)
     85        errCheck(err)
    6786        fmt.Printf("%v\n",translate.Output)
    6887}
    69 func sanityCheck(err error) {
    70         if err != nil {
    71                 log.Fatal(err)
    72         }
    73 }
Note: See TracChangeset for help on using the changeset viewer.