diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..f1b219b --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,6 @@ +version: 2 +updates: + - package-ecosystem: "gomod" + directory: "/" + schedule: + interval: "weekly" diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..0333b60 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,29 @@ +on: + push: + tags: + - "*" + +permissions: + contents: write + +jobs: + goreleaser: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + with: + fetch-depth: 0 + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: 1.17 + - run: go install github.com/knadh/stuffbin/stuffbin@latest + - name: Run GoReleaser + uses: goreleaser/goreleaser-action@v2 + with: + distribution: goreleaser + version: latest + args: release --rm-dist + env: + GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} diff --git a/.gitignore b/.gitignore index 99e36be..8e622c1 100644 --- a/.gitignore +++ b/.gitignore @@ -10,7 +10,7 @@ *.so *.dylib hopp-cli -hoop.bin +hopp.bin dist/ # Test binary, built with `go test -c` diff --git a/.goreleaser.yml b/.goreleaser.yml index 7d766ce..5d85eae 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -1,5 +1,16 @@ +project_name: hopp-cli +release: + github: + owner: hoppscotch + name: hopp-cli + prerelease: auto + draft: false + name_template: "hopp-cli {{.Version}}" +before: + hooks: + - go mod tidy env: - - 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 + - RELEASE_BUILDS=dist/hopp-cli_darwin_amd64/hopp-cli dist/hopp-cli_darwin_arm64/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 builds: - env: - CGO_ENABLED=0 @@ -12,6 +23,14 @@ builds: goarch: - 386 - amd64 + - arm64 + targets: + - linux_amd64 + - linux_arm64 + - windows_amd64 + - windows_arm64 + - darwin_amd64 + - darwin_arm64 hooks: post: make pack-releases ignore: @@ -19,29 +38,27 @@ builds: goarch: 386 archives: - replacements: - darwin: Darwin - linux: Linux + darwin: macOS + 386: i386 + linux: linux amd64: x86_64 checksum: - name_template: 'checksums.txt' + name_template: "checksums.txt" snapshot: - name_template: '{{ .Tag }}' + name_template: "{{ .Tag }}" changelog: sort: asc filters: exclude: - - '^docs:' - - '^test:' -release: - github: - owner: hoppscotch - name: hopp-cli + - "^docs:" + - "^test:" brews: - name: hopp-cli - github: + tap: owner: athul name: homebrew-tap + token: "{{ .Env.HB_TOKEN }}" folder: Formula - homepage: 'https://github.com/hoppscotch/hopp-cli' - description: 'CLI for Hoppscotch.io' + homepage: "https://github.com/hoppscotch/hopp-cli" + description: "CLI for Hoppscotch.io" install: bin.install "hopp-cli" diff --git a/.travis.yml b/.travis.yml index 09d50f4..4a1ed35 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,7 @@ sudo: false language: go go: - - "1.13" + - "1.16.x" before_script: - go get -v - go build diff --git a/Makefile b/Makefile index ca5ea23..2c8ae93 100644 --- a/Makefile +++ b/Makefile @@ -1,56 +1,41 @@ BIN:=hopp.bin +BINSTUFFED:=hopp-cli PREFIX?=/usr/local BINDIR?=$(PREFIX)/bin VERSION?=$(shell git tag | grep ^v | sort -V | tail -n 1) STATIC := ./templates/index.html ./templates/template.md:/template.md +all: deps build + +.PHONY: deps deps: - go get -u github.com/knadh/stuffbin/... + @printf '%s\n' 'Checking binary dependencies...' + @if [ ! -f "$(shell go env GOPATH)/bin/stuffbin" ]; then printf '%s\n' 'Installing stuffbin to GOPATH' && go install github.com/knadh/stuffbin/stuffbin@latest; fi -build: cli.go go.mod go.sum - @echo - @echo Building hopp-cli. This may take a minute or two. - @echo - go build -o ${BIN} -ldflags="-s -w -X 'main.buildVersion=${VERSION}'" *.go - stuffbin -a stuff -in ${BIN} -out ${BIN} ${STATIC} - @echo - @echo ...Done\! +.PHONY: build +build: + @printf '%s\n' 'Building hopp-cli. This may take a minute or two.' + go build -o ${BIN} -ldflags="-s -w -X 'main.buildVersion=${VERSION}'" + $(shell go env GOPATH)/bin/stuffbin -a stuff -in ${BIN} -out ${BINSTUFFED} ${STATIC} + rm ${BIN} .PHONY: clean clean: - @echo - @echo Cleaning... - @echo - go clean - @echo - @echo ...Done\! - -.PHONY: update -update: - @echo - @echo Updating from upstream repository... - @echo - git pull --rebase origin master - @echo - @echo ...Done\! + @printf '%s\n' 'Cleaning...' + @go clean + @if [ -f 'hopp-cli' ]; then rm hopp-cli; fi + @if [ -f 'hopp.bin' ]; then rm hopp.bin; fi .PHONY: install install: - @echo - @echo Installing hopp-cli... - @echo + @printf '%s\n' 'Installing hopp-cli...' install -m755 hopp-cli $(BINDIR) - @echo - @echo ...Done\! .PHONY: uninstall uninstall: - @echo - @echo Uninstalling hopp-cli... - @echo + @printf '%s\n' 'Uninstalling hopp-cli...' rm -f $(BINDIR)/hopp-cli - @echo - @echo ...Done\! + .PHONY: pack-releases pack-releases: - $(foreach var,$(RELEASE_BUILDS),stuffbin -a stuff -in ${var} -out ${var} ${STATIC};) \ No newline at end of file + $(foreach var,$(RELEASE_BUILDS),stuffbin -a stuff -in ${var} -out ./${var} ${STATIC};) \ No newline at end of file diff --git a/README.md b/README.md index c4903bb..d10641a 100644 --- a/README.md +++ b/README.md @@ -14,31 +14,55 @@ $ sh -c "$(curl -sL https://git.io/getpwcli)" - Clone the repo -``` +```shell $ git clone https://github.com/hoppscotch/hopp-cli.git ``` - Build and install -``` +```shell $ make - $ sudo make install ``` ### From Binary -- You can find the Binaries in Gzipped form from the [Releases](https://github.com/athul/pwcli/releases) page - **Supports** -- Linux(x64,x86) -- Mac(x64) -- Windows(x64,x86) +- You can download prebuilt binaries from the [Releases](https://github.com/hoppscotch/hopp-cli/releases) page. +- **Supported platforms**: + - Linux (x64, x86) + - Mac (x64) + - Windows (x64, x86) > **IMPORTANT: Not tested on Windows, please leave your feedback/bugs in the Issues section** +### Arch GNU/Linux + +- You can install from [AUR](https://aur.archlinux.org/) +- There are three different packages available + +Name | Link | Description +------------- | ------------------------------------------------- | ----------------------------- +hopp-cli-bin | https://aur.archlinux.org/packages/hopp-cli-bin/ | Pre-built binary +hopp-cli | https://aur.archlinux.org/packages/hopp-cli/ | Compiled from latest release +hopp-cli-git | https://aur.archlinux.org/packages/hopp-cli-git/ | Compiled from latest commit + ### Homebrew -Install by `brew install athul/tap/hopp-cli` +Install by + +```shell +brew install athul/tap/hopp-cli +``` + +### Windows + +You can download pre-built binaries from the [Releases](https://github.com/hoppscotch/hopp-cli/releases) page. + +Alternatively, you can install `hopp-cli` via [Scoop](https://scoop.sh/): + +```shell +scoop install hopp-cli +``` ## Usages @@ -46,8 +70,8 @@ Putting Simply: **Just pass the URL to the request method** ### Basic Commands -- GET : `$ hopp-cli get ` -- POST: `$ hopp-cli post ` +- GET : `$ hopp-cli get ` +- POST: `$ hopp-cli post ` - PATCH: `$ hopp-cli patch ` - PUT : `$ hopp-cli put ` - DELETE: `$ hopp-cli delete ` @@ -56,7 +80,6 @@ Example for a POST request: ```shell $ hopp-cli post https://reqres.in/api/users/2 -c js -b '{"name": "morp","job": "zion resident"}' - ``` ### Extra Commands @@ -64,19 +87,31 @@ $ hopp-cli post https://reqres.in/api/users/2 -c js -b '{"name": "morp","job": " - `send` for testing multiple endpoints - `gen` for generating API docs from Collection -**SEND**: This can be used to test multiple endpoints from the `hoppscotch-collection.json` file. +#### SEND + +This can be used to test multiple endpoints from the `hoppscotch-collection.json` file. > The output will only be the `statuscode` -Example : `hopp-cli send ` +Example: -Sample Output: -![](/assets/send.png) +```shell +$ hopp-cli send +``` ---- +Sample output: -**GEN**: Gen command Generates the API Documentation from `hoppscotch-collection.json` file and serves it as a Static Page on port `1341` -Example: `hopp-cli gen ` +![send-output](/assets/send.png) + +#### GEN + +The `gen` command generates the API documentation from `hoppscotch-collection.json` file and serves it as a static page on port `1341`. + +Example: + +```shell +$ hopp-cli gen +``` Sample Hosted site: https://hopp-docsify.surge.sh/ @@ -95,15 +130,14 @@ _(optional)_ - `-u` for the `Username` in Basic Auth - `-p` for the `password` in Basic Auth -### There are 2 flags especially for the data management requests like POST,PUT,PATCH and DELETE +### There are 2 flags especially for the data management requests like POST, PUT, PATCH and DELETE - `-c` or `--ctype` for the _Content Type_ - - `-b` or `--body` for the Data Body, this can be of json, html or plain text based on the request. > Enclose the body in Single Quotes(\') -**Content Types can be of** +**Content Types can be of** |Short Code|Content Type| |:---:|:---:| @@ -111,3 +145,57 @@ _(optional)_ |`html`|`text/html`| |`xml`|`application/xml`| |`plain`|`text/plain`| + +### Include Arbitrary Headers + +- `-H` or `--header` may be specified multiple times to include headers with the request. + +Example: + +```shell +$ hopp-cli get -H 'X-Api-Key: foobar' -H 'X-Api-Secret: super_secret' https://example.com/api/v1/accounts +``` + +### Providing a Request Body via stdin + +In addition to `-b`/`--body`, you may provide a request body via stdin.\ +If you combine this method with the `-b` flag, the body provided with `-b` will be ignored. + +**Example with Pipes** + +```shell +$ echo '{"foo":"bar"}' | hopp-cli post -c js http://example.com +``` + +**Example with Redirection** + +```shell +$ cat myrequest.json +{ + "foo": "bar" +} + +$ hopp-cli post -c js http://example.com 0 { @@ -186,7 +185,7 @@ func (c *Collection) sendPOST(req Requests, method string) (string, string, erro } } if err != nil { - return "", "", fmt.Errorf("Error creating request: %s", err.Error()) + return "", "", fmt.Errorf("error creating request: %w", err) } reqHTTP.Header.Set("Content-Type", req.Ctype) // Set Content type to said Ctype in Collection @@ -203,7 +202,7 @@ func (c *Collection) sendPOST(req Requests, method string) (string, string, erro client := getHTTPClient() resp, err := client.Do(reqHTTP) if err != nil { - return "", "", fmt.Errorf("Error sending request: %s", err.Error()) + return "", "", fmt.Errorf("error sending request: %w", err) } defer resp.Body.Close() @@ -245,7 +244,7 @@ func (c *Collection) getDatafromFolders() error { return nil } -//genTables generate the output in Tabular Form +// genTables generate the output in Tabular Form func genTables(data [][]string) { table := tablewriter.NewWriter(os.Stdout) table.SetHeader([]string{"Name", "URL", "Method", "Status", "Code"}) diff --git a/methods/validation.go b/methods/validation.go index 022932d..2dd0ded 100644 --- a/methods/validation.go +++ b/methods/validation.go @@ -1,23 +1,23 @@ package methods import ( - "fmt" + "errors" "net/url" "strings" ) func checkURL(urlStr string) (string, error) { if urlStr == "" { - return "", fmt.Errorf("URL is needed") + return "", errors.New("URL is needed") } prefixCheck := strings.HasPrefix(urlStr, "http://") || strings.HasPrefix(urlStr, "https://") if !prefixCheck { - return "", fmt.Errorf("URL missing protocol or contains invalid protocol") + return "", errors.New("URL missing protocol or contains invalid protocol") } if _, err := url.Parse(urlStr); err != nil { - return "", fmt.Errorf("URL is invalid") + return "", errors.New("URL is invalid") } return urlStr, nil