Changeset 21 in code for trunk/cmd


Ignore:
Timestamp:
Oct 21, 2023, 8:12:04 PM (20 months ago)
Author:
yakumo.izuru
Message:

Load config with godotenv

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

File:
1 edited

Legend:

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

    r18 r21  
    1010        "net/http"
    1111        "net/url"
     12        "os"
     13
     14        "github.com/joho/godotenv"
    1215        "marisa.chaotic.ninja/suwako"
    13         "os"
    1416)
    1517
     
    2426        Output string `json:"translated_text"`
    2527}
    26 func init() {
     28func loadCfg() {
    2729        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")
    2830        flag.StringVar(&input, "i", "", "Enter the text to be translated")
    29         flag.StringVar(&target, "t", "en", "Set the language to translate to")
     31        flag.StringVar(&target, "t", "", "Set the language to translate to")
     32
    3033        flag.Usage = func() {
    3134                fmt.Printf("usage: suwako -f [lang] -i [text] -t [lang]\nversion: %v\n", suwako.FullVersion())
    3235        }
     36
     37        home, err := os.UserHomeDir()
     38        sanityCheck(err)
     39        conf := home + "/.suwako/suwako.conf"
     40        err = godotenv.Load(conf)
     41        sanityCheck(err)
     42
     43        engine = os.Getenv("SUWAKO_ENGINE")
     44        instance = os.Getenv("SUWAKO_INSTANCE")
     45
     46        flag.Parse()
     47}
     48func check() {
     49        if len(input) == 0 || len(target) == 0 {
     50                log.Fatal("Either there is no input or there is no target language")
     51        }
    3352}
    3453func main() {
    35         engine = os.Getenv("SUWAKO_ENGINE")
    36         instance = os.Getenv("SUWAKO_INSTANCE")
     54        loadCfg()
     55        check()
    3756       
    38         flag.Parse()
    39 
    40         if len(engine) == 0 || len(instance) == 0 {
    41                 log.Println("SUWAKO_ENGINE and/or SUWAKO_INSTANCE are unset")
    42                 log.Println("Defaulting to simplytranslate.org with engine 'google'")
    43                 engine = "google"
    44                 instance = "https://simplytranslate.org"
    45         }
    46         if len(input) == 0 {
    47                 log.Fatal("Missing input text.")
    48         }
    49         // Map a variable to the struct
    5057        var translate Translate
    51         // Build the full URL to query
    5258        var encInput = url.PathEscape(input)
    5359        var apiEndpoint = "/api/translate"
    5460        var queryURL = instance + apiEndpoint + "?engine=" + engine + "&from=" + source + "&to=" + target + "&text=" + encInput
    55         // Begin the request and process the response
     61
    5662        resp, err := http.Get(queryURL)
    5763        sanityCheck(err)
    5864        defer resp.Body.Close()
    59         // JSON decoding
    6065        _ = json.NewDecoder(resp.Body).Decode(&translate)
    6166        sanityCheck(err)
Note: See TracChangeset for help on using the changeset viewer.