refactor: cleanup and simplify code

This commit is contained in:
Bnyro 2024-02-20 10:11:45 +01:00
parent c985f14cfe
commit 65a9fe29b2
5 changed files with 43 additions and 45 deletions

View File

@ -33,7 +33,7 @@ var langlistCmd = &cobra.Command{
fmt.Println(err)
}
for _, idx := range idxs {
if raw == true {
if raw {
fmt.Println(list[idx].Id)
} else {
fmt.Println("Selected Language:", list[idx].Id, "("+list[idx].Name+")")

View File

@ -17,6 +17,33 @@ var (
rawjson bool
)
func printEngineResult(result libmozhi.LangOut, printPlaceHolderAndEngineName bool) {
if printPlaceHolderAndEngineName {
fmt.Println("-----------------------------------")
fmt.Println("Engine: " + result.Engine)
}
fmt.Println("Translated Text: " + result.OutputText)
if source == "auto" {
fmt.Println("Detected Language: " + result.AutoDetect)
}
fmt.Println("Source Language: " + result.SourceLang)
fmt.Println("Target Language: " + result.TargetLang)
if printPlaceHolderAndEngineName {
fmt.Println("-----------------------------------")
}
}
func printRaw(data interface{}) {
j, err := json.Marshal(data)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(string(j))
}
}
var translateCmd = &cobra.Command{
Use: "translate",
Short: "Translate.",
@ -24,44 +51,21 @@ var translateCmd = &cobra.Command{
if engine == "all" {
data := libmozhi.TranslateAll(dest, source, query)
if rawjson {
j, err := json.Marshal(data)
if err != nil {
fmt.Println(err)
printRaw(data)
} else {
fmt.Println(string(j))
for _, result := range data {
printEngineResult(result, true)
}
} else {
for i := 0; i < len(data); i++ {
fmt.Println("-----------------------------------")
fmt.Println("Engine: " + data[i].Engine)
fmt.Println("Translated Text: " + data[i].OutputText)
if source == "auto" {
fmt.Println("Detected Language: " + data[i].AutoDetect)
}
fmt.Println("Source Language: " + data[i].SourceLang)
fmt.Println("Target Language: " + data[i].TargetLang)
}
fmt.Println("-----------------------------------")
}
} else {
data, err := libmozhi.Translate(engine, dest, source, query)
if rawjson {
j, err := json.Marshal(data)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(string(j))
}
printRaw(data)
} else {
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Translated Text: " + data.OutputText)
if source == "auto" {
fmt.Println("Detected Language: " + data.AutoDetect)
}
fmt.Println("Source Language: " + data.SourceLang)
fmt.Println("Target Language: " + data.TargetLang)
printEngineResult(data, false)
}
}
}

View File

@ -82,20 +82,14 @@ func HandleTranslate(c *fiber.Ctx) error {
if engine == "" || from == "" || to == "" || text == "" {
return fiber.NewError(fiber.StatusBadRequest, "from, to, engine, text are required query strings.")
}
var dataarr []libmozhi.LangOut
var data libmozhi.LangOut
var err error
if engine == "all" {
dataarr = libmozhi.TranslateAll(to, from, text)
dataarr := libmozhi.TranslateAll(to, from, text)
return c.JSON(dataarr)
} else {
data, err = libmozhi.Translate(engine, to, from, text)
data, err := libmozhi.Translate(engine, to, from, text)
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
}
if engine == "all" {
return c.JSON(dataarr)
} else {
return c.JSON(data)
}
}

View File

@ -12,7 +12,7 @@ import (
func langListMerge(engines map[string]string) ([]libmozhi.List, []libmozhi.List) {
sl := []libmozhi.List{}
tl := []libmozhi.List{}
for key, _ := range engines {
for key := range engines {
temp, _ := libmozhi.LangList(key, "sl")
temp2, _ := libmozhi.LangList(key, "tl")
sl = append(sl, temp...)

View File

@ -7,6 +7,9 @@ import (
"regexp"
)
var nonAlphanumericRegex = regexp.MustCompile(`[^a-zA-Z]+`)
var nonAlphaRegex = regexp.MustCompile(`[^a-zA-Z0-9]+`)
func GetQueryOrFormValue(c *fiber.Ctx, key string) string {
if c.Method() == "POST" {
return c.FormValue(key)
@ -16,15 +19,12 @@ func GetQueryOrFormValue(c *fiber.Ctx, key string) string {
}
func EnvTrueNoExist(env string) bool {
if _, ok := os.LookupEnv(env); ok == false || os.Getenv(env) == "true" {
return true
}
return false
_, envFound := os.LookupEnv(env)
return !envFound || os.Getenv(env) == "true"
}
func Sanitize(str string, strip string) string {
nonAlphanumericRegex := regexp.MustCompile(`[^a-zA-Z]+`)
nonAlphaRegex := regexp.MustCompile(`[^a-zA-Z0-9]+`)
if strip == "alpha" {
return nonAlphaRegex.ReplaceAllString(str, "")
} else if strip == "alphanumeric" {