Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
speier committed Nov 24, 2020
0 parents commit d046693
Show file tree
Hide file tree
Showing 217 changed files with 196,257 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

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

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

dist
tmp
8 changes: 8 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
build:
binary: hrun
goos:
- linux
- darwin
- windows
goarch:
- amd64
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Kalman Speier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
VERSION=v0.1.0

release:
@git tag -a ${VERSION} -m "Release ${VERSION}" && git push origin ${VERSION}
@goreleaser --rm-dist
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# hrun

HTTP request runner with a simple DSL based on JavaScript.

## Install

Download [latest release](https://github.com/speier/hrun/releases)

## Usage example

Create a script:

```js
const host = 'https://httpbin.org'

GET(`${host}/get`)
POST(`${host}/post`, 'Accept: application/vnd.foobar.v2+json', { payload: { data: 'foobar' }})
```

Run the script:

```sh
$ hrun -f examples/basic.js
```

## API

All HTTP method is a function with an URL, headers and payload arguments:

```js
METHOD(URL, [headers], [payload])
```

`URL`: required

`[headers]`: optional

`[payload]`: optional
7 changes: 7 additions & 0 deletions examples/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const host = 'https://httpbin.org'

// simple GET
GET(`${host}/get`)

// append header and POST data
POST(`${host}/post`, 'Accept: application/vnd.foobar.v2+json', { payload: { data: 'foobar' }})
12 changes: 12 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module github.com/speier/hrun

go 1.15

require (
github.com/dlclark/regexp2 v1.4.0 // indirect
github.com/dop251/goja v0.0.0-20201107160812-7545ac6de48a
github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect
github.com/speier/gowasm v0.0.0-20181207125641-bfb154601c86
golang.org/x/text v0.3.4 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
)
35 changes: 35 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
github.com/GeertJohan/go.rice v0.0.0-20170420135705-c02ca9a983da h1:UVU3a9pRUyLdnBtn60WjRl0s4SEyJc2ChCY56OAR6wI=
github.com/GeertJohan/go.rice v0.0.0-20170420135705-c02ca9a983da/go.mod h1:DgrzXonpdQbfN3uYaGz1EG4Sbhyum/MMIn6Cphlh2bw=
github.com/PuerkitoBio/goquery v1.5.0/go.mod h1:qD2PgZ9lccMbQlc7eEOjaeRlFQON7xY8kdmcsrnKqMg=
github.com/andybalholm/cascadia v1.0.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
github.com/daaku/go.zipexe v0.0.0-20150329023125-a5fe2436ffcb h1:tUf55Po0vzOendQ7NWytcdK0VuzQmfAgvGBUOQvN0WA=
github.com/daaku/go.zipexe v0.0.0-20150329023125-a5fe2436ffcb/go.mod h1:U0vRfAucUOohvdCxt5MWLF+TePIL0xbCkbKIiV8TQCE=
github.com/dlclark/regexp2 v1.1.6/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E=
github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/dop251/goja v0.0.0-20181125163413-2dd08a5fc665/go.mod h1:Mw6PkjjMXWbTj+nnj4s3QPXq1jaT0s5pC0iFD4+BOAA=
github.com/dop251/goja v0.0.0-20201107160812-7545ac6de48a h1:RYcWAh8DBgQQ7Fi3YhoyMhtGiF8JHKBDSeym7wd9o10=
github.com/dop251/goja v0.0.0-20201107160812-7545ac6de48a/go.mod h1:Mw6PkjjMXWbTj+nnj4s3QPXq1jaT0s5pC0iFD4+BOAA=
github.com/go-chi/chi v3.3.3+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ=
github.com/go-sourcemap/sourcemap v2.1.2+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg=
github.com/go-sourcemap/sourcemap v2.1.3+incompatible h1:W1iEw64niKVGogNgBN3ePyLFfuisuzeidWPMPWmECqU=
github.com/go-sourcemap/sourcemap v2.1.3+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/kardianos/osext v0.0.0-20170510131534-ae77be60afb1 h1:PJPDf8OUfOK1bb/NeTKd4f1QXZItOX389VN3B6qC8ro=
github.com/kardianos/osext v0.0.0-20170510131534-ae77be60afb1/go.mod h1:1NbS8ALrpOvjt0rHPNLyCIeMtbizbir8U//inJ+zuB8=
github.com/speier/gowasm v0.0.0-20181207125641-bfb154601c86 h1:APkflNnWcvoyjBOpamAlZBuqrGmP+3uXeNC8HIxZYcA=
github.com/speier/gowasm v0.0.0-20181207125641-bfb154601c86/go.mod h1:aqRByxxkRQVvR6lWxkcwQ2Wa6DdDPRJCGkKVYmPsCaI=
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.4 h1:0YWbFKbhXG/wIiuHDSKpS0Iy7FSA+u45VtBMfQcFTTc=
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
125 changes: 125 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package main

import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"strings"
)

var (
filename = flag.String("f", "", "filename")
methods = map[string]interface{}{
"HEADER": setHeader,
"GET": httpGet,
"POST": httpPost,
}
header = http.Header{}
cookies = []*http.Cookie{}
)

func init() {
flag.Parse()
}

func main() {
// default headers
header.Set("Accept", "application/json")
header.Set("Content-Type", "application/json")

// run script
vm := NewInterpreter(methods)
_, err := vm.RunFile(*filename)
if err != nil {
println(err.Error())
}
}

func setHeader(args ...interface{}) {
for _, a := range args {
s, ok := a.(string)
if ok {
h := strings.Split(s, ":")
if len(h) == 2 {
k, v := h[0], h[1]
header.Set(k, v)
}
}
}
}

func httpGet(host string, args ...interface{}) {
res, err := httpreq(http.MethodGet, host, args...)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(res)
}

func httpPost(host string, args ...interface{}) {
res, err := httpreq(http.MethodPost, host, args...)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(res)
}

func httpreq(method string, host string, args ...interface{}) (string, error) {
payload := &bytes.Buffer{}
for _, a := range args {
// headers
s, ok := a.(string)
if ok {
h := strings.Split(s, ":")
if len(h) == 2 {
k, v := h[0], h[1]
header.Set(k, v)
}
}
// payload
m, ok := a.(map[string]interface{})
if ok {
b, err := json.Marshal(m)
if err != nil {
return "", err
}
_, err = payload.Write(b)
if err != nil {
return "", err
}
}
}

req, err := http.NewRequest(method, host, payload)
if err != nil {
return "", err
}

// set request headers and cookies
req.Header = header
for _, c := range cookies {
req.AddCookie(c)
}

client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return "", err
}
defer res.Body.Close()

body, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}

// save response cookies
for _, c := range res.Cookies() {
cookies = append(cookies, c)
}

return string(body), nil
}
8 changes: 8 additions & 0 deletions vendor/github.com/GeertJohan/go.rice/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions vendor/github.com/GeertJohan/go.rice/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions vendor/github.com/GeertJohan/go.rice/AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions vendor/github.com/GeertJohan/go.rice/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d046693

Please sign in to comment.