add some engines support to mozhi CLI
All checks were successful
mozhi pipeline / Push Docker image to Codeberg docker registry (push) Successful in 19m53s
mozhi pipeline / Build and publish artifacts (push) Successful in 1h4m39s

This commit is contained in:
Arya 2024-08-27 13:02:34 +05:30
parent 8189007bda
commit a2ac174314
Signed by: arya
GPG Key ID: 842D12BDA50DF120

View File

@ -2,6 +2,7 @@ package cmd
import ( import (
"encoding/json" "encoding/json"
"strings"
"fmt" "fmt"
"codeberg.org/aryak/libmozhi" "codeberg.org/aryak/libmozhi"
@ -11,6 +12,7 @@ import (
var ( var (
engine string engine string
engines string
query string query string
source string source string
dest string dest string
@ -29,10 +31,6 @@ func printEngineResult(result libmozhi.LangOut, printPlaceHolderAndEngineName bo
} }
fmt.Println("Source Language: " + result.SourceLang) fmt.Println("Source Language: " + result.SourceLang)
fmt.Println("Target Language: " + result.TargetLang) fmt.Println("Target Language: " + result.TargetLang)
if printPlaceHolderAndEngineName {
fmt.Println("-----------------------------------")
}
} }
func printRaw(data interface{}) { func printRaw(data interface{}) {
@ -56,6 +54,25 @@ var translateCmd = &cobra.Command{
for _, result := range data { for _, result := range data {
printEngineResult(result, true) printEngineResult(result, true)
} }
fmt.Println("-----------------------------------")
}
} else if engine == "some" {
if engines != "" {
data, err := libmozhi.TranslateSome(strings.Split(engines, ","), dest, source, query)
if rawjson {
printRaw(data)
} else {
if err != nil {
fmt.Println(err)
} else {
for _, result := range data {
printEngineResult(result, true)
}
fmt.Println("-----------------------------------")
}
}
} else {
fmt.Println("Please add the --engines / -E argument when using the engine `some`")
} }
} else { } else {
data, err := libmozhi.Translate(engine, dest, source, query) data, err := libmozhi.Translate(engine, dest, source, query)
@ -76,13 +93,13 @@ func init() {
rootCmd.AddCommand(translateCmd) rootCmd.AddCommand(translateCmd)
translateCmd.Flags().SortFlags = false translateCmd.Flags().SortFlags = false
translateCmd.Flags().StringVarP(&engine, "engine", "e", "", "[all|google|libre|reverso|deepl|yandex|mymemory|duckduckgo]") translateCmd.Flags().StringVarP(&engine, "engine", "e", "", "[all|some|google|libre|reverso|deepl|yandex|mymemory|duckduckgo]")
translateCmd.Flags().StringVarP(&engines, "engines", "E", "", "Engines to select. This flag is to be used with the `some` engine only")
translateCmd.Flags().StringVarP(&source, "source", "s", "", "Source language. Use langlist command to get code for your language") translateCmd.Flags().StringVarP(&source, "source", "s", "", "Source language. Use langlist command to get code for your language")
translateCmd.Flags().StringVarP(&dest, "dest", "t", "", "Target language. Use langlist command to get code for your language") translateCmd.Flags().StringVarP(&dest, "dest", "t", "", "Target language. Use langlist command to get code for your language")
translateCmd.Flags().StringVarP(&query, "query", "q", "", "Text to be translated") translateCmd.Flags().StringVarP(&query, "query", "q", "", "Text to be translated")
translateCmd.Flags().BoolVarP(&rawjson, "raw", "r", false, "Return output as json") translateCmd.Flags().BoolVarP(&rawjson, "raw", "r", false, "Return output as json")
translateCmd.MarkFlagRequired("engine")
translateCmd.MarkFlagRequired("source") translateCmd.MarkFlagRequired("source")
translateCmd.MarkFlagRequired("dest") translateCmd.MarkFlagRequired("dest")
translateCmd.MarkFlagRequired("query") translateCmd.MarkFlagRequired("query")