From 5ffd20046ac8010985aead7055e05bbc92a18501 Mon Sep 17 00:00:00 2001 From: Arya Kiran Date: Tue, 29 Aug 2023 14:50:56 +0530 Subject: [PATCH] add json option for transl. + gofmt --- cmd/langlist.go | 8 ++++--- cmd/translate.go | 62 ++++++++++++++++++++++++++++++++---------------- pages/api.go | 4 ++++ serve/serve.go | 14 +++++------ 4 files changed, 58 insertions(+), 30 deletions(-) diff --git a/cmd/langlist.go b/cmd/langlist.go index f5e534e..ded834e 100644 --- a/cmd/langlist.go +++ b/cmd/langlist.go @@ -2,15 +2,17 @@ package cmd import ( "fmt" + "codeberg.org/aryak/mozhi/utils" - "github.com/spf13/cobra" + "github.com/ktr0731/go-fuzzyfinder" + "github.com/spf13/cobra" ) var ( engineused string - listtype string - raw bool + listtype string + raw bool ) // langlistCmd represents the langlist command diff --git a/cmd/translate.go b/cmd/translate.go index 802d872..0357049 100644 --- a/cmd/translate.go +++ b/cmd/translate.go @@ -1,17 +1,20 @@ package cmd import ( + "encoding/json" "fmt" + "codeberg.org/aryak/mozhi/utils" "github.com/spf13/cobra" ) var ( - engine string - query string - source string - dest string + engine string + query string + source string + dest string + rawjson bool ) var translateCmd = &cobra.Command{ @@ -20,27 +23,45 @@ var translateCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { if engine == "all" { data := utils.TranslateAll(dest, source, query) - 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) + if rawjson { + j, err := json.Marshal(data) + if err != nil { + fmt.Println(err) + } else { + fmt.Println(string(j)) + } + } 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("Source Language: "+data[i].SourceLang) - fmt.Println("Target Language: "+data[i].TargetLang) } } else { data, err := utils.Translate(engine, dest, source, query) - if err != nil { - fmt.Println(err) + if rawjson { + j, err := json.Marshal(data) + if err != nil { + fmt.Println(err) + } else { + fmt.Println(string(j)) + } + } else { + if err != nil { + fmt.Println(err) + } + 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) } - 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) } }, } @@ -53,6 +74,7 @@ func init() { 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(&query, "query", "q", "", "Text to be translated") + translateCmd.Flags().BoolVarP(&rawjson, "raw", "r", false, "Return output as json") translateCmd.MarkFlagRequired("engine") translateCmd.MarkFlagRequired("source") diff --git a/pages/api.go b/pages/api.go index 6a37c19..5f34fed 100644 --- a/pages/api.go +++ b/pages/api.go @@ -6,6 +6,7 @@ import ( ) // HandleSourceLanguages godoc +// // @Summary Show list of available source languages for engine // @Param engine query string true "Engine name" // @Success 200 {object} utils.List @@ -23,6 +24,7 @@ func HandleSourceLanguages(c *fiber.Ctx) error { } // HandleTargetLanguages godoc +// // @Summary Show list of available target languages for engine // @Param engine query string true "Engine name" // @Success 200 {object} utils.List @@ -40,6 +42,7 @@ func HandleTargetLanguages(c *fiber.Ctx) error { } // HandleTTS godoc +// // @Summary Get Text-To-Speech for specified language using specified engine // @Param engine query string true "Engine name" // @Param lang query string true "Language being TTS'd" @@ -61,6 +64,7 @@ func HandleTTS(c *fiber.Ctx) error { } // HandleTranslate godoc +// // @Summary Translate text // @Description When engine is set to all, it will return an array of utils.LangOut. // @Param engine query string true "Engine name" diff --git a/serve/serve.go b/serve/serve.go index 9ba4257..3350f15 100644 --- a/serve/serve.go +++ b/serve/serve.go @@ -6,9 +6,9 @@ import ( "os" "runtime" + _ "codeberg.org/aryak/mozhi/docs" "codeberg.org/aryak/mozhi/pages" "codeberg.org/aryak/mozhi/utils" - _ "codeberg.org/aryak/mozhi/docs" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/compress" @@ -22,12 +22,12 @@ import ( _ "github.com/joho/godotenv/autoload" ) -// @title Mozhi API -// @version 1.0 -// @description API for Mozhi, the alternative-frontend for many translation engines. -// @license.name AGPL 3.0 -// @license.url https://www.gnu.org/licenses/agpl-3.0.txt -// @BasePath /api +// @title Mozhi API +// @version 1.0 +// @description API for Mozhi, the alternative-frontend for many translation engines. +// @license.name AGPL 3.0 +// @license.url https://www.gnu.org/licenses/agpl-3.0.txt +// @BasePath /api func Serve(port string) { engine := html.New("./views", ".html")