From 914d5cd9f4277b4c09318f7748939333dfa3df85 Mon Sep 17 00:00:00 2001 From: Igor Nemilentsev Date: Thu, 2 Jul 2020 23:15:27 +0300 Subject: [PATCH] Added cmd line alises. Added http debugging --- pkg/api/api.go | 41 ++++++++++++++++++++++++++++++++++---- pkg/translator/args.go | 37 +++++++++++++++++++++++----------- pkg/translator/linguleo.go | 6 ++++++ 3 files changed, 68 insertions(+), 16 deletions(-) diff --git a/pkg/api/api.go b/pkg/api/api.go index 6fd398d..fe9b7aa 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -9,11 +9,15 @@ import ( "io" "net/http" "net/http/cookiejar" + "net/http/httputil" + "os" "strconv" "strings" "sync" "time" + "github.com/sirupsen/logrus" + "github.com/trezorg/lingualeo/pkg/channel" "github.com/trezorg/lingualeo/pkg/logger" @@ -95,7 +99,7 @@ func (api *API) auth() error { if err != nil { return nil } - responseBody, err := request("POST", authURL, api.client, jsonValue, "") + responseBody, err := request("POST", authURL, api.client, jsonValue, "", api.Debug) if err != nil { return err } @@ -110,7 +114,29 @@ func (api *API) auth() error { return nil } -func request(method string, url string, client *http.Client, body []byte, query string) (*string, error) { +func debugRequest(request *http.Request) { + dump, err := httputil.DumpRequestOut(request, true) + if err != nil { + logrus.Error(err) + } else { + logrus.SetOutput(os.Stderr) + logrus.Debug(string(dump)) + logrus.SetOutput(os.Stdout) + } +} + +func debugResponse(response *http.Response) { + dump, err := httputil.DumpResponse(response, true) + if err != nil { + logrus.Error(err) + } else { + logrus.SetOutput(os.Stderr) + logrus.Debug(string(dump)) + logrus.SetOutput(os.Stdout) + } +} + +func request(method string, url string, client *http.Client, body []byte, query string, debug bool) (*string, error) { var requestBody io.Reader = nil if len(body) > 0 { requestBody = bytes.NewBuffer(body) @@ -133,10 +159,17 @@ func request(method string, url string, client *http.Client, body []byte, query req.Header.Add(key, header) } } + + if debug { + debugRequest(req) + } resp, err := client.Do(req) if err != nil { return nil, err } + if debug { + debugResponse(resp) + } defer func() { err := resp.Body.Close() if err != nil { @@ -173,7 +206,7 @@ func (api *API) translateRequest(word string) (*string, error) { if err != nil { return nil, err } - return request("POST", translateURL, api.client, jsonValue, "") + return request("POST", translateURL, api.client, jsonValue, "", api.Debug) } func (api *API) addRequest(word string, translate []string) (*string, error) { @@ -183,7 +216,7 @@ func (api *API) addRequest(word string, translate []string) (*string, error) { "port": "1001", } jsonValue, _ := json.Marshal(values) - return request("POST", addWordURL, api.client, jsonValue, "") + return request("POST", addWordURL, api.client, jsonValue, "", api.Debug) } func (api *API) TranslateWord(word string) OpResult { diff --git a/pkg/translator/args.go b/pkg/translator/args.go index 2da6f7b..c872132 100644 --- a/pkg/translator/args.go +++ b/pkg/translator/args.go @@ -79,6 +79,7 @@ func prepareCliArgs(version string) Lingualeo { return fmt.Errorf("there are no words to translate") } args.Words = utils.Unique(c.Args().Slice()) + args.Translate = translate.Value() if args.Add && len(args.Translate) > 0 && len(args.Words) > 1 { return fmt.Errorf("you should add only one word with custom transcation") } @@ -132,52 +133,61 @@ func prepareCliArgs(version string) Lingualeo { ` app.Flags = []cli.Flag{ &cli.StringFlag{ - Name: "email, e", + Name: "email", + Aliases: []string{"e"}, Value: "", Usage: "Lingualeo email", Destination: &args.Email, }, &cli.StringFlag{ - Name: "password, p", + Name: "password", + Aliases: []string{"p"}, Value: "", Usage: "Lingualeo password", Destination: &args.Password, }, &cli.StringFlag{ - Name: "config, c", + Name: "config", + Aliases: []string{"c"}, Value: "", Usage: "Config file. Either in toml, yaml or json format", Destination: &args.Config, }, &cli.StringFlag{ - Name: "player, m", + Name: "player", + Aliases: []string{"m"}, Value: "", Usage: "Media player to pronounce words", Destination: &args.Player, }, &cli.StringFlag{ - Name: "log-level, l", + Name: "log-level", + Aliases: []string{"l"}, Value: "INFO", Usage: "Log level", Destination: &args.LogLevel, }, &cli.BoolFlag{ - Name: "sound, s", + Name: "sound", + Aliases: []string{"s"}, Usage: "Pronounce words", Destination: &args.Sound, }, &cli.BoolFlag{ - Name: "download, dl", + Name: "download", + Aliases: []string{"dl"}, Usage: "Download file to play sound. In case a player is not able to play url directly", Destination: &args.DownloadSoundFile, }, &cli.BoolFlag{ - Name: "debug, d", + Name: "debug", + Aliases: []string{"d"}, Usage: "Debug mode. Set DEBUG mode", Destination: &args.Debug, }, &cli.BoolFlag{ - Name: "log-pretty-print, lpr", + Name: "log-pretty-print", + Aliases: []string{"lpr"}, Usage: "Log pretty print", Destination: &args.LogPrettyPrint, }, @@ -189,17 +199,20 @@ func prepareCliArgs(version string) Lingualeo { Usage: "Add to lingualeo dictionary", Flags: []cli.Flag{ &cli.BoolFlag{ - Name: "force, f", + Name: "force", + Aliases: []string{"f"}, Usage: "Force add to lingualeo dictionary", Destination: &args.Force, }, &cli.BoolFlag{ - Name: "replace, r", + Name: "replace", + Aliases: []string{"r"}, Usage: "Custom translation. Replace word instead of adding", Destination: &args.TranslateReplace, }, &cli.StringSliceFlag{ - Name: "translate, t", + Name: "translate", + Aliases: []string{"t"}, Usage: "Custom translation: lingualeo add -t word1 -t word2 word", Destination: &translate, }, diff --git a/pkg/translator/linguleo.go b/pkg/translator/linguleo.go index 67efef9..3196eea 100644 --- a/pkg/translator/linguleo.go +++ b/pkg/translator/linguleo.go @@ -5,6 +5,8 @@ import ( "os" "sync" + "github.com/sirupsen/logrus" + "github.com/trezorg/lingualeo/pkg/api" "github.com/trezorg/lingualeo/pkg/channel" "github.com/trezorg/lingualeo/pkg/files" @@ -48,6 +50,10 @@ func NewLingualeo(version string) (Lingualeo, error) { if err != nil { return client, err } + if client.Debug { + client.LogLevel = logrus.DebugLevel.String() + client.LogPrettyPrint = true + } logger.InitLogger(client.LogLevel, client.LogPrettyPrint) client.checkMediaPlayer() a, err := api.NewAPI(client.Email, client.Password, client.Debug)