Skip to content

Commit

Permalink
Added cmd line alises. Added http debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Nemilentsev authored and Ihar Niamilentsau committed Jul 3, 2020
1 parent 532b316 commit 914d5cd
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 16 deletions.
41 changes: 37 additions & 4 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down
37 changes: 25 additions & 12 deletions pkg/translator/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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,
},
Expand All @@ -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,
},
Expand Down
6 changes: 6 additions & 0 deletions pkg/translator/linguleo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 914d5cd

Please sign in to comment.