Skip to content

Commit

Permalink
Rewrite help to match httpie added capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
rs committed Aug 1, 2018
1 parent 24f0965 commit db760fb
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 35 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ See [HTTPie doc](https://httpie.org/doc) for more examples.
## Differences with httpie

* Like `curl` but unlike `httpie`, headers are written on `stderr` instead of `stdout`.
* Output is not buffered, all the formatting is done on the fly so you can easily debug streamed data.
*
* Output is not buffered, all the formatting is done on the fly so you can easily debug streamed data.
22 changes: 22 additions & 0 deletions formatter/help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package formatter

import (
"bytes"
"io"
)

type HelpAdapter struct {
Out io.Writer
CmdName string
}

func (j HelpAdapter) Write(p []byte) (n int, err error) {
cmd := "http"
if len(j.CmdName) == 0 {
cmd = j.CmdName
}
p = bytes.Replace(p,
[]byte("curl [options...] <url>"),
[]byte(cmd+" [options...] [METHOD] URL [REQUEST_ITEM [REQUEST_ITEM ...]]"), 1)
return j.Out.Write(p)
}
74 changes: 41 additions & 33 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ func main() {
verbose := opts.Has("verbose") || opts.Has("v")
quiet := opts.Has("silent") || opts.Has("s")

// Remove progress bar.
opts = append(opts, "-sS")
if len(opts) == 0 {
// Show help if no args
opts = append(opts, "-h")
} else {
// Remove progress bar.
opts = append(opts, "-sS")
}

// Change default method based on binary name.
switch os.Args[0] {
Expand All @@ -33,41 +38,44 @@ func main() {
}
}

if data := opts.Val("d"); data != "" {
// If data is provided via -d, read it from there for the verbose mode.
// XXX handle the @filename case.
w := &formatter.JSON{
Out: input,
Scheme: formatter.DefaultColorScheme,
}
w.Write([]byte(data))
} else if !terminal.IsTerminal(0) {
// If something is piped in to the command, tell curl to use it as input.
opts = append(opts, "-d@-")
// Tee the stdin to the buffer used show the posted data in verbose mode.
stdin = io.TeeReader(stdin, &formatter.JSON{
Out: input,
Scheme: formatter.DefaultColorScheme,
})
}
if terminal.IsTerminal(1) {
// Format/colorize JSON output if stdout is to the terminal.
stdout = &formatter.JSON{
Out: stdout,
Scheme: formatter.DefaultColorScheme,
if opts.Has("h") || opts.Has("help") {
stdout = &formatter.HelpAdapter{Out: stdout, CmdName: os.Args[0]}
} else {
if data := opts.Val("d"); data != "" {
// If data is provided via -d, read it from there for the verbose mode.
// XXX handle the @filename case.
w := &formatter.JSON{
Out: input,
Scheme: formatter.DefaultColorScheme,
}
w.Write([]byte(data))
} else if !terminal.IsTerminal(0) {
// If something is piped in to the command, tell curl to use it as input.
opts = append(opts, "-d@-")
// Tee the stdin to the buffer used show the posted data in verbose mode.
stdin = io.TeeReader(stdin, &formatter.JSON{
Out: input,
Scheme: formatter.DefaultColorScheme,
})
}
}
if terminal.IsTerminal(2) {
// If stderr is not redirected, output headers.
if !quiet {
opts = append(opts, "-v")
if terminal.IsTerminal(1) {
// Format/colorize JSON output if stdout is to the terminal.
stdout = &formatter.JSON{
Out: stdout,
Scheme: formatter.DefaultColorScheme,
}
}
stderr = &formatter.HeaderColorizer{
Out: stderr,
Scheme: formatter.DefaultColorScheme,
if terminal.IsTerminal(2) {
// If stderr is not redirected, output headers.
if !quiet {
opts = append(opts, "-v")
}
stderr = &formatter.HeaderColorizer{
Out: stderr,
Scheme: formatter.DefaultColorScheme,
}
}
}
fmt.Println(opts)
cmd := exec.Command("curl", opts...)
cmd.Stdin = stdin
cmd.Stdout = stdout
Expand Down

0 comments on commit db760fb

Please sign in to comment.