Skip to content
This repository was archived by the owner on Jul 14, 2022. It is now read-only.

Commit f3d27cf

Browse files
Implemented request-body data insertion through text-editor (#45)
* added feature to add request body through text-editor * resolved file busy issue * updated error verb & minor changes * resolved changes * minor refactor * updated readme * updated readme with suggested changes * updated print statement for formatted output Authored by: @devblin Co-authored-by: HackMD <no-reply@hackmd.io>
1 parent 8e85342 commit f3d27cf

3 files changed

Lines changed: 83 additions & 3 deletions

File tree

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,25 @@ $ cat myrequest.json
177177

178178
$ hopp-cli post -c js http://example.com <myrequest.json
179179
```
180+
181+
### Providing a Request Body via text-editor
182+
183+
In addition to providing request body via `-b / --body` flag and stdin,
184+
you can also use `-e / --editor` flag which opens default text-editor in your system.
185+
186+
**Example:**
187+
188+
```shell
189+
$ hopp-cli post https://reqres.in/api/users/2 -c js -e
190+
```
191+
192+
It will preferrably open editor based on `$EDITOR` environment variable.
193+
194+
**For example:**
195+
If the environment variable is `$EDITOR=code` it will open VSCode for request-body input. Else, it will use default editor value based on the OS.
196+
197+
| OS | Default Editor |
198+
| ------- | -------------- |
199+
| Linux | `nano` |
200+
| macOS | `nano` |
201+
| Windows | `notepad` |

cli.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ func main() {
7272
Name: "body, b",
7373
Usage: "Body of the Post Request",
7474
},
75+
cli.BoolFlag{
76+
Name: "editor, e",
77+
Usage: "Open editor to insert request body",
78+
},
7579
cli.StringSliceFlag{
7680
Name: "header, H",
7781
Usage: "Header to pass with the request. Can be used multiple times.",
@@ -169,5 +173,5 @@ func main() {
169173
l.Println(color.HiRedString("\n%s", err.Error()))
170174
os.Exit(1)
171175
}
172-
fmt.Println(out)
176+
fmt.Fprintf(color.Output, out)
173177
}

methods/basic.go

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ package methods
33
import (
44
"bytes"
55
"fmt"
6-
"io"
6+
"io/ioutil"
77
"net/http"
88
"os"
9+
"os/exec"
10+
"runtime"
911
"strings"
1012

13+
"github.com/fatih/color"
1114
"github.com/urfave/cli"
1215
)
1316

@@ -26,10 +29,61 @@ func BasicRequestWithBody(c *cli.Context, method string) (string, error) {
2629
return "", fmt.Errorf("error getting file info for stdin fd: %w", err)
2730
}
2831
if (stat.Mode() & os.ModeCharDevice) == 0 {
29-
body, err = io.ReadAll(os.Stdin)
32+
body, err = ioutil.ReadAll(os.Stdin)
3033
if err != nil {
3134
return "", fmt.Errorf("error reading from stdin: %w", err)
3235
}
36+
} else if c.Bool("editor") {
37+
var path string
38+
var editor = os.Getenv("EDITOR")
39+
40+
if editor == "" {
41+
// check OS
42+
currentOs := runtime.GOOS
43+
44+
// based on OS find assign default editor
45+
if currentOs == "linux" || currentOs == "darwin" {
46+
editor = "nano"
47+
} else if currentOs == "windows" {
48+
editor = "notepad"
49+
}
50+
}
51+
52+
path, err = exec.LookPath(editor)
53+
if err != nil {
54+
return "", fmt.Errorf("Unable to locate %s: %w", editor, err)
55+
}
56+
57+
// create temp file for writing request body
58+
tempFile, err := ioutil.TempFile("", "HOPP-CLI-REQUEST*.txt")
59+
if err != nil {
60+
return "", fmt.Errorf("Unable to create temp file: %w", err)
61+
}
62+
63+
tempFileName := string(tempFile.Name())
64+
defer os.Remove(tempFileName)
65+
tempFile.Close()
66+
67+
// open temp file in selected editor and wait until closed
68+
cmd := exec.Command(path, tempFileName)
69+
cmd.Stdin = os.Stdin
70+
cmd.Stdout = os.Stdout
71+
72+
if err := cmd.Start(); err != nil {
73+
return "", fmt.Errorf("Unable to open editor: %w", err)
74+
}
75+
76+
color.Yellow("Waiting for file to close..\n\n")
77+
78+
if err := cmd.Wait(); err != nil {
79+
return "", fmt.Errorf("Error waiting for file: %w", err)
80+
}
81+
82+
// read temp file contents
83+
body, err = ioutil.ReadFile(tempFileName)
84+
if err != nil {
85+
return "", fmt.Errorf("Error reading file %s: %w", tempFileName, err)
86+
}
3387
} else {
3488
body = []byte(c.String("body"))
3589
}

0 commit comments

Comments
 (0)