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

Commit 7bc2111

Browse files
athuljoicemjoseph
andauthored
✨ API Doc Generation (#23)
* Add `gen` command and basic FE * Add Headers and RawParams and Auth in FE * Minor Updates * 🔥 Moved the whole to markdown and docsify * 📝 Add autobrowser open and Updated Readme * Final Touches for Stuffing and deployment Co-authored-by: Joice M. Joseph <[email protected]> * Minor Refactor to Code and Docs * meaningful name for fstructm, renamed to FileTrunk. removed named variable definition in FileTrunk * Add Flag for Auto Browser Open * 📝 Updated Docs on Broswer Flag Co-authored-by: Joice M. Joseph <[email protected]>
1 parent f1b75a1 commit 7bc2111

File tree

12 files changed

+429
-40
lines changed

12 files changed

+429
-40
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
*.so
1111
*.dylib
1212
hopp-cli
13+
hoop.bin
14+
dist/
1315

1416
# Test binary, built with `go test -c`
1517
*.test

.goreleaser.yml

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
1-
# This is an example goreleaser.yaml file with some sane defaults.
2-
# Make sure to check the documentation at http://goreleaser.com
3-
before:
4-
hooks:
5-
# you may remove this if you don't use vgo
6-
- go mod tidy
7-
# you may remove this if you don't need go generate
8-
- go get -v
1+
env:
2+
- RELEASE_BUILDS=dist/hopp-cli_darwin_amd64/hopp-cli dist/hopp-cli_linux_386/hopp-cli dist/hopp-cli_linux_amd64/hopp-cli dist/hopp-cli_windows_386/hopp-cli.exe dist/hopp-cli_windows_amd64/hopp-cli.exe
93
builds:
104
- env:
115
- CGO_ENABLED=0
126
ldflags:
13-
- -s -w -X main.VERSION={{.Version}}
7+
- -s -w -X main.buildVersion={{.Version}}
148
goos:
159
- darwin
1610
- linux
1711
- windows
1812
goarch:
1913
- 386
2014
- amd64
15+
hooks:
16+
post: make pack-releases
2117
ignore:
2218
- goos: darwin
2319
goarch: 386

Makefile

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1+
BIN:=hopp.bin
12
PREFIX?=/usr/local
23
BINDIR?=$(PREFIX)/bin
34
VERSION?=$(shell git tag | grep ^v | sort -V | tail -n 1)
4-
GOFLAGS?=-ldflags '-X main.VERSION=${VERSION}'
5+
STATIC := ./templates/index.html ./templates/template.md:/template.md
56

6-
hopp-cli: cli.go go.mod go.sum
7+
deps:
8+
go get -u github.com/knadh/stuffbin/...
9+
10+
build: cli.go go.mod go.sum
711
@echo
812
@echo Building hopp-cli. This may take a minute or two.
913
@echo
10-
go build $(GOFLAGS) -o $@
14+
go build -o ${BIN} -ldflags="-s -w -X 'main.buildVersion=${VERSION}'" *.go
15+
stuffbin -a stuff -in ${BIN} -out ${BIN} ${STATIC}
1116
@echo
1217
@echo ...Done\!
1318

@@ -46,3 +51,6 @@ uninstall:
4651
rm -f $(BINDIR)/hopp-cli
4752
@echo
4853
@echo ...Done\!
54+
.PHONY: pack-releases
55+
pack-releases:
56+
$(foreach var,$(RELEASE_BUILDS),stuffbin -a stuff -in ${var} -out ${var} ${STATIC};)

README.md

Lines changed: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# Hoppscotch CLI [![hoppscotch](https://img.shields.io/badge/Made_for-Hoppscotch-hex_color_code?logo=Postwoman)](https://hoppscotch.io) [![Go Report Card](https://goreportcard.com/badge/github.com/athul/pwcli)](https://goreportcard.com/report/github.com/athul/pwcli)
1+
# Hoppscotch CLI [![hoppscotch](https://img.shields.io/badge/Made_for-Hoppscotch-hex_color_code?logo=Postwoman)](https://hoppscotch.io) [![Go Report Card](https://goreportcard.com/badge/github.com/athul/pwcli)](https://goreportcard.com/report/github.com/athul/pwcli)
22

3-
Send HTTP requests from terminal. An alternative to cURL, httpie ⚡️
3+
Send HTTP requests from terminal and Generate API Docs. An alternative to cURL, httpie ⚡️
44

5-
# Installation
5+
## Installation
66

77
### From Script
88

@@ -34,46 +34,80 @@ $ sudo make install
3434
- Mac(x64)
3535
- Windows(x64,x86)
3636

37+
> **IMPORTANT: Not tested on Windows, please leave your feedback/bugs in the Issues section**
38+
3739
### Homebrew
3840

3941
Install by `brew install athul/tap/hopp-cli`
4042

41-
> **IMPORTANT: Not tested on Windows, please leave your feedback/bugs in the Issues section**
42-
43-
# Usages
43+
## Usages
4444

4545
Putting Simply: **Just pass the URL to the request method**
46-
## Basic
47-
- GET : `hopp-cli get <url> `
48-
- POST: `hopp-cli post <url> `
49-
- PATCH: `hopp-cli patch <url>`
50-
- PUT : `hopp-cli put <url>`
51-
- DELETE: `hopp-cli delete <url>`
5246

53-
Example for a POST request:
54-
`hopp-cli post https://reqres.in/api/users/2 -c js -b '{"name": "morp","job": "zion resident"}`
47+
### Basic Commands
48+
49+
- GET : `$ hopp-cli get <url> `
50+
- POST: `$ hopp-cli post <url> `
51+
- PATCH: `$ hopp-cli patch <url>`
52+
- PUT : `$ hopp-cli put <url>`
53+
- DELETE: `$ hopp-cli delete <url>`
54+
55+
Example for a POST request:
56+
57+
```shell
58+
$ hopp-cli post https://reqres.in/api/users/2 -c js -b '{"name": "morp","job": "zion resident"}'
59+
60+
```
61+
62+
### Extra Commands
63+
64+
- `send` for testing multiple endpoints
65+
- `gen` for generating API docs from Collection
66+
67+
**SEND**: This can be used to test multiple endpoints from the `hoppscotch-collection.json` file.
5568

56-
### Extra
69+
> The output will only be the `statuscode`
5770
58-
**SEND**: This can be used to test multiple endpoints from the `hoppscotch-collection.json` file. The output will only be the `statuscode`.
5971
Example : `hopp-cli send <PATH to hoppscotch collection.json>`
60-
o/p:
72+
73+
Sample Output:
6174
![](/assets/send.png)
6275

76+
---
77+
78+
**GEN**: Gen command Generates the API Documentation from `hoppscotch-collection.json` file and serves it as a Static Page on port `1341`
79+
Example: `hopp-cli gen <PATH to hoppscotch collection.json>`
80+
81+
Sample Hosted site: https://hopp-docsify.surge.sh/
82+
83+
Powered by [Doscify](https://docsify.js.org)
84+
85+
Flags:
86+
87+
- `browser` or `b` to toggle whether the browser should open automatically [Boolean]
88+
- `port` or `p` for specifying the port where the server should listen to [Integer]
6389

6490
### There are 3 Authentication Flags
65-
*(optional)*
91+
92+
_(optional)_
93+
6694
- `-t` or `--token` for a Bearer Token for Authentication
6795
- `-u` for the `Username` in Basic Auth
6896
- `-p` for the `password` in Basic Auth
97+
6998
### There are 2 flags especially for the data management requests like POST,PUT,PATCH and DELETE
70-
- `-c` or `--ctype` for the *Content Type*
7199

72-
- `-b` or `--body` for the Data Body, this can be of json, html or plain text based on the request.
73-
> Enclose the body in Single Quotes(\')
100+
- `-c` or `--ctype` for the _Content Type_
101+
102+
- `-b` or `--body` for the Data Body, this can be of json, html or plain text based on the request.
103+
104+
> Enclose the body in Single Quotes(\')
105+
106+
**Content Types can be of**
74107

75-
**Content Types can be of**
76-
`html` : `text/html`
77-
`js` : `application/json`
78-
`xml` : `application/xml`
79-
`plain` : `text/plain`
108+
|Short Code|Content Type|
109+
|:---:|:---:|
110+
|`js`|`application/json`|
111+
|`html`|`text/html`|
112+
|`xml`|`application/xml`|
113+
|`plain`|`text/plain`|

assets/send.png

277 KB
Loading

cli.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import (
1111
)
1212

1313
// VERSION is set by `make` during the build to the most recent tag
14-
var VERSION = ""
14+
var buildVersion = "unknown"
1515

1616
func main() {
1717
app := cli.NewApp()
1818
app.Name = color.HiGreenString("Hoppscotch CLI")
19-
app.Version = color.HiRedString(VERSION)
19+
app.Version = color.HiRedString(buildVersion)
2020
app.Usage = color.HiYellowString("Test API endpoints without the hassle")
2121
app.Description = color.HiBlueString("Made with <3 by Hoppscotch Team")
2222

@@ -36,6 +36,17 @@ func main() {
3636
Usage: "Add the Password",
3737
},
3838
}
39+
genFlags := []cli.Flag{
40+
cli.IntFlag{
41+
Name: "port, p",
42+
Usage: "Port at which the server will open to",
43+
Value: 1341,
44+
},
45+
cli.BoolFlag{
46+
Name: "browser, b",
47+
Usage: "Whether to open the browser automatically",
48+
},
49+
}
3950
postFlags := []cli.Flag{
4051
cli.StringFlag{
4152
Name: "token, t",
@@ -124,6 +135,17 @@ func main() {
124135
return nil
125136
},
126137
},
138+
{
139+
Name: "gen",
140+
Usage: "Generate Documentation from the Hoppscotch Collection.json",
141+
Flags: genFlags,
142+
Action: func(c *cli.Context) error {
143+
if err := mets.GenerateDocs(c); err != nil {
144+
return err
145+
}
146+
return nil
147+
},
148+
},
127149
}
128150
cli.AppHelpTemplate = fmt.Sprintf(`%s
129151

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ go 1.13
44

55
require (
66
github.com/fatih/color v1.9.0
7+
github.com/knadh/stuffbin v1.1.0
78
github.com/olekukonko/tablewriter v0.0.4
9+
github.com/pkg/browser v0.0.0-20201112035734-206646e67786
810
github.com/stretchr/testify v1.4.0 // indirect
911
github.com/tidwall/pretty v1.0.1
1012
github.com/urfave/cli v1.22.2

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
1010
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
1111
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
1212
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
13+
github.com/knadh/stuffbin v1.1.0 h1:f5S5BHzZALjuJEgTIOMC9NidEnBJM7Ze6Lu1GHR/lwU=
14+
github.com/knadh/stuffbin v1.1.0/go.mod h1:yVCFaWaKPubSNibBsTAJ939q2ABHudJQxRWZWV5yh+4=
1315
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
1416
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
1517
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
@@ -24,6 +26,8 @@ github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+tw
2426
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
2527
github.com/olekukonko/tablewriter v0.0.4 h1:vHD/YYe1Wolo78koG299f7V/VAS08c6IpCLn+Ejf/w8=
2628
github.com/olekukonko/tablewriter v0.0.4/go.mod h1:zq6QwlOf5SlnkVbMSr5EoBv3636FWnp+qbPhuoO21uA=
29+
github.com/pkg/browser v0.0.0-20201112035734-206646e67786 h1:4Gk0Dsp90g2YwfsxDOjvkEIgKGh+2R9FlvormRycveA=
30+
github.com/pkg/browser v0.0.0-20201112035734-206646e67786/go.mod h1:N6UoU20jOqggOuDwUaBQpluzLNDqif3kq9z2wpdYEfQ=
2731
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2832
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2933
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=

methods/docs.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package methods
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"log"
7+
"net/http"
8+
"os"
9+
"strconv"
10+
"time"
11+
12+
"github.com/knadh/stuffbin"
13+
"github.com/pkg/browser"
14+
"github.com/urfave/cli"
15+
)
16+
17+
//FileTrunk handles the buffer for generated README.md File
18+
type FileTrunk struct{ bytes.Buffer }
19+
20+
// Name holds the FileName, here README.md
21+
func (f *FileTrunk) Name() string { return "README.md" }
22+
23+
// Size holds the size of the File
24+
func (f *FileTrunk) Size() int64 { return int64(f.Len()) }
25+
26+
// Mode holds the file Mode
27+
func (f *FileTrunk) Mode() os.FileMode { return 0755 }
28+
29+
// ModTime holds creation time of File
30+
func (f *FileTrunk) ModTime() time.Time { return time.Now() }
31+
32+
// IsDir checks if True
33+
func (f *FileTrunk) IsDir() bool { return false }
34+
35+
// Sys - I have no idea
36+
func (f *FileTrunk) Sys() interface{} { return nil }
37+
38+
//GenerateDocs generates the Documentation site from the hoppscotch-collection.json
39+
func GenerateDocs(c *cli.Context) error {
40+
execPath, err := os.Executable() //get Executable Path for StuffBin
41+
if err != nil {
42+
return err
43+
}
44+
fs, err := initFileSystem(execPath) //Init Virtual FS
45+
if err != nil {
46+
return err
47+
}
48+
49+
colls, err := ReadCollection(c.Args().Get(0))
50+
if err != nil {
51+
return err
52+
}
53+
// FuncMap for the HTML template
54+
fmap := map[string]interface{}{
55+
"html": func(val string) string { return val },
56+
}
57+
58+
t, err := stuffbin.ParseTemplates(fmap, fs, "/template.md")
59+
60+
// f will be used to store rendered templates in memory.
61+
var f FileTrunk
62+
63+
// Execute the template to the file.
64+
if err = t.Execute(&f, colls); err != nil {
65+
return err
66+
}
67+
68+
if err := fs.Add(stuffbin.NewFile("/README.md", &f, f.Bytes())); err != nil {
69+
return err
70+
}
71+
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
72+
out, err := fs.Read("templates/index.html")
73+
if err != nil {
74+
log.Println(err)
75+
}
76+
w.Write(out)
77+
})
78+
PortStr := ":" + strconv.Itoa(c.Int("port"))
79+
URL := fmt.Sprintf("http://localhost%s", PortStr)
80+
81+
http.Handle("/static/", http.StripPrefix("/static/", fs.FileServer()))
82+
83+
log.Printf("\033[1;36mServer Listening at %s\033[0m", URL)
84+
85+
if !c.Bool("browser") { //Check if User wants to open the Broswer
86+
browser.OpenURL(URL) // AutoOpen the Broswer
87+
}
88+
89+
http.ListenAndServe(PortStr, nil)
90+
return nil
91+
}

methods/fs.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package methods
2+
3+
import (
4+
"log"
5+
6+
"github.com/knadh/stuffbin"
7+
)
8+
9+
func initFileSystem(binPath string) (stuffbin.FileSystem, error) {
10+
fs, err := stuffbin.UnStuff(binPath)
11+
// If files are not stuffed with the binary,
12+
// try to pick up files from local file system.
13+
if err == stuffbin.ErrNoID {
14+
// Running in local mode. Load the required static assets into
15+
// the in-memory stuffbin.FileSystem.
16+
17+
files := []string{
18+
"./templates/index.html",
19+
"./templates/template.md:/template.md",
20+
}
21+
22+
// mutates err object.
23+
fs, err = stuffbin.NewLocalFS("/", files...)
24+
if err != nil {
25+
log.Println("Error in Virtual FS", err)
26+
}
27+
}
28+
29+
// Either unstuff or NewLocalFS throws error,
30+
// mutated error value will be returned
31+
return fs, err
32+
}

0 commit comments

Comments
 (0)