Changeset 21 in code
Legend:
- Unmodified
- Added
- Removed
-
trunk/cmd/suwako/main.go
r18 r21 10 10 "net/http" 11 11 "net/url" 12 "os" 13 14 "github.com/joho/godotenv" 12 15 "marisa.chaotic.ninja/suwako" 13 "os"14 16 ) 15 17 … … 24 26 Output string `json:"translated_text"` 25 27 } 26 func init() {28 func loadCfg() { 27 29 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") 28 30 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 30 33 flag.Usage = func() { 31 34 fmt.Printf("usage: suwako -f [lang] -i [text] -t [lang]\nversion: %v\n", suwako.FullVersion()) 32 35 } 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 } 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") 51 } 33 52 } 34 53 func main() { 35 engine = os.Getenv("SUWAKO_ENGINE")36 instance = os.Getenv("SUWAKO_INSTANCE")54 loadCfg() 55 check() 37 56 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 struct50 57 var translate Translate 51 // Build the full URL to query52 58 var encInput = url.PathEscape(input) 53 59 var apiEndpoint = "/api/translate" 54 60 var queryURL = instance + apiEndpoint + "?engine=" + engine + "&from=" + source + "&to=" + target + "&text=" + encInput 55 // Begin the request and process the response 61 56 62 resp, err := http.Get(queryURL) 57 63 sanityCheck(err) 58 64 defer resp.Body.Close() 59 // JSON decoding60 65 _ = json.NewDecoder(resp.Body).Decode(&translate) 61 66 sanityCheck(err) -
trunk/go.mod
r13 r21 2 2 3 3 go 1.18 4 5 require github.com/joho/godotenv v1.5.1 -
trunk/go.sum
r6 r21 1 github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= 2 github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
Note:
See TracChangeset
for help on using the changeset viewer.