Skip to content

Commit

Permalink
internal/client: use hexadecimal input and output
Browse files Browse the repository at this point in the history
  • Loading branch information
igarciaolaizola committed Dec 11, 2018
1 parent bb6a25d commit a5f9194
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ debug.test
.reports/
.temp/
.vscode/
.history/
1 change: 1 addition & 0 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,6 @@ func newClientCommand() *cobra.Command {
cmd.Flags().BoolVar(&cfg.Insecure, "insecure", false, "skip tls validation")
cmd.Flags().StringVar(&cfg.Data, "data", "", "initial body data to be sent")
cmd.Flags().StringArrayVar(&cfg.Headers, "header", nil, "custom headers, ex.: \"Content-Type: application/json\"")
cmd.Flags().BoolVar(&cfg.Hex, "hex", false, "use hexadecimal input/output for data")
return cmd
}
14 changes: 13 additions & 1 deletion internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"crypto/tls"
"encoding/hex"
"errors"
"fmt"
"io"
Expand All @@ -22,6 +23,7 @@ type Config struct {
Insecure bool
Headers []string
Data string
Hex bool
}

// Client is a http2 client
Expand Down Expand Up @@ -74,6 +76,10 @@ func (c *Client) Run() error {
body = os.Stdin
}

if c.Hex {
body = hex.NewDecoder(body)
}

// Establish http connection
req, err := http.NewRequest(c.Method, c.Addr, body)
if c.Headers != nil {
Expand Down Expand Up @@ -101,6 +107,12 @@ func (c *Client) Run() error {
}()

log.Println(fmt.Sprintf("Connected to %s", c.Addr))
io.Copy(os.Stdout, resp.Body)

out := io.Writer(os.Stdout)
if c.Hex {
out = hex.NewEncoder(out)
}

io.Copy(out, resp.Body)
return nil
}

0 comments on commit a5f9194

Please sign in to comment.