Skip to content

Commit 7e842f7

Browse files
committed
Initial commit
0 parents  commit 7e842f7

22 files changed

+502
-0
lines changed

.github/workflows/build.yml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: build
2+
3+
on: push
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v1
10+
11+
- name: Install Opctl
12+
run: curl -L https://github.com/opctl/opctl/releases/download/0.1.45/opctl0.1.45.linux.tgz | sudo tar -xzv -C /usr/local/bin
13+
14+
- name: Build
15+
run: opctl run build

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Jetbrains
2+
.idea/
3+
4+
# opctl
5+
*args*.yml

.opspec/build/op.yml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name: build
2+
description: builds the op
3+
run:
4+
serial:
5+
- op:
6+
ref: github.com/opspec-pkgs/_.op.bootstrap#4.1.1
7+
inputs:
8+
srcDir: $(../..)
9+
- op:
10+
ref: test

.opspec/e2eTest/op.yml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: e2eTest
2+
run:
3+
serial:
4+
- op:
5+
ref: $(../../)
6+
inputs:
7+
headers:
8+
Content-Type:
9+
- text/html
10+
url: https://google.com
11+
method: GET
12+
outputs:
13+
body:
14+
headers:
15+
protocol:
16+
statusCode:
17+
- container:
18+
image:
19+
ref: alpine
20+
cmd:
21+
- sh
22+
- -ce
23+
- |
24+
echo $(protocol) $(statusCode)
25+
echo 'Content-Type'
26+
echo $(headers[Content-Type][0])
27+
echo 'headers'
28+
cat /headers
29+
echo ""
30+
echo "body"
31+
cat /body
32+
sleep 1
33+
files:
34+
/body: $(body)
35+
/headers: $(headers)

.opspec/provision-image/op.yml

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: provision-image
2+
description: |
3+
Provisions image for the op to [opspec-pkgs image registry](https://github.com/orgs/opspec-pkgs/packages) w/ name `$(opDotYml.name):${opDotYml.version}`.
4+
5+
> Before first use, a Dockerfile MUST be added to `.opspec/provision-image`
6+
inputs:
7+
githubUsername:
8+
string:
9+
constraints: { minLength: 1 }
10+
description: user for logging in to github
11+
githubAccessToken:
12+
string:
13+
constraints: { minLength: 1 }
14+
description: accessToken for logging in to github
15+
isSecret: true
16+
run:
17+
serial:
18+
- op:
19+
ref: github.com/opspec-pkgs/yaml.parse#1.0.0
20+
inputs:
21+
yaml: $(../../op.yml)
22+
outputs:
23+
result: $(opDotYml)
24+
- container:
25+
image: { ref: 'alpine:3.6' }
26+
cmd: [sh, -ce, 'echo -n ghcr.io/opspec-pkgs/${opName#*/*/}:$(opDotYml.version) > /imageName']
27+
envVars:
28+
opName: $(opDotYml.name)
29+
files:
30+
/imageName: $(imageName)
31+
- op:
32+
ref: github.com/opspec-pkgs/docker.image.build#2.1.0
33+
inputs:
34+
dockerfile: $(./Dockerfile)
35+
imageName:
36+
outputs: { imageTar }
37+
- op:
38+
ref: github.com/opspec-pkgs/docker.image.push#2.1.0
39+
inputs:
40+
imageTar:
41+
imageName:
42+
registry: ghcr.io
43+
username: $(githubUsername)
44+
password: $(githubAccessToken)

.opspec/release/op.yml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: release
2+
description: releases the op
3+
inputs:
4+
githubUsername:
5+
string:
6+
constraints: { minLength: 1 }
7+
description: user for logging in to github
8+
githubAccessToken:
9+
string:
10+
constraints: { minLength: 1 }
11+
description: accessToken for logging in to github
12+
isSecret: true
13+
run:
14+
serial:
15+
- op:
16+
ref: build
17+
- op:
18+
ref: github.com/opspec-pkgs/yaml.parse#1.0.0
19+
inputs:
20+
yaml: $(../../op.yml)
21+
outputs:
22+
result: $(opDotYml)
23+
- op:
24+
ref: github.com/opspec-pkgs/git.repo.resolve-commit#1.0.2
25+
inputs: { dotGitDir: $(../../.git) }
26+
outputs: { commit: }
27+
- container:
28+
image: { ref: 'alpine:3.6' }
29+
cmd: [sh, -ce, 'echo -n ${opName#*/*/} > /opRepo']
30+
envVars:
31+
opName: $(opDotYml.name)
32+
files:
33+
/opRepo: $(opRepo)
34+
- op:
35+
ref: github.com/opspec-pkgs/github.release.create#1.1.0
36+
inputs:
37+
owner: opspec-pkgs
38+
repo: $(opRepo)
39+
loginUsername: $(githubUsername)
40+
loginPassword: $(githubAccessToken)
41+
tag: $(opDotYml.version)
42+
commitish: $(commit)
43+
name: $(opDotYml.version)

.opspec/test/op.yml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name: test
2+
description: tests the op
3+
run:
4+
op:
5+
ref: github.com/opspec-pkgs/opctl.op.validate#1.0.0
6+
inputs:
7+
opctlVersion: 0.1.53
8+
op: $(../..)

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Change Log
2+
3+
All notable changes will be documented in this file in accordance with
4+
[![keepachangelog 1.0.0](https://img.shields.io/badge/keepachangelog-1.0.0-brightgreen.svg)](http://keepachangelog.com/en/1.0.0/)
5+
6+
## \[1.0.0] - 2024-11-16

CODEOWNERS

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @opspec-pkgs/maintainers

README.md

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
[![Build Status](https://github.com/opspec-pkgs/http.send/workflows/build/badge.svg?branch=main)](https://github.com/opspec-pkgs/http.send/actions?query=workflow%3Abuild+branch%3Amain)
2+
3+
<img src="icon.svg" alt="icon" height="100px">
4+
5+
# Problem statement
6+
7+
Sends an http request
8+
9+
# Example usage
10+
11+
## Visualize
12+
13+
```shell
14+
opctl ui github.com/opspec-pkgs/http.send#1.0.0
15+
```
16+
17+
## Run
18+
19+
```
20+
opctl run github.com/opspec-pkgs/http.send#1.0.0
21+
```
22+
23+
## Compose
24+
25+
```yaml
26+
op:
27+
ref: github.com/opspec-pkgs/http.send#1.0.0
28+
inputs:
29+
method: # 👈 required; provide a value
30+
url: # 👈 required; provide a value
31+
## uncomment to override defaults
32+
# body: "[object Object]"
33+
# headers: [object Object]
34+
outputs:
35+
body:
36+
headers:
37+
protocol:
38+
statusCode:
39+
```
40+
41+
# Support
42+
43+
join us on
44+
[![Slack](https://img.shields.io/badge/slack-opctl-E01563.svg)](https://join.slack.com/t/opctl/shared_invite/zt-51zodvjn-Ul_UXfkhqYLWZPQTvNPp5w)
45+
or
46+
[open an issue](https://github.com/opspec-pkgs/http.send/issues)
47+
48+
# Releases
49+
50+
releases are versioned according to
51+
[![semver 2.0.0](https://img.shields.io/badge/semver-2.0.0-brightgreen.svg)](http://semver.org/spec/v2.0.0.html)
52+
and [tagged](https://git-scm.com/book/en/v2/Git-Basics-Tagging); see
53+
[CHANGELOG.md](CHANGELOG.md) for release notes
54+
55+
# Contributing
56+
57+
see
58+
[project/CONTRIBUTING.md](https://github.com/opspec-pkgs/project/blob/main/CONTRIBUTING.md)

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/opspec-pkgs/http.send
2+
3+
go 1.23.2

go.sum

Whitespace-only changes.

icon.svg

+10
Loading

internal/inout/doc.go

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// package inout is used to process op input to an http request and http response to op output
2+
package inout

internal/inout/getHeaders.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package inout
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
"os"
8+
"strconv"
9+
)
10+
11+
func getHeaders() (
12+
map[string][]string,
13+
error,
14+
) {
15+
headersJSONBytes, err := os.ReadFile(HeadersFilePath)
16+
if err != nil {
17+
return nil, err
18+
}
19+
20+
rawHeaders := map[string][]interface{}{}
21+
if err := json.Unmarshal(headersJSONBytes, &rawHeaders); err != nil {
22+
return nil, err
23+
}
24+
25+
headersMap := http.Header{}
26+
for k, vSlice := range rawHeaders {
27+
for _, rawV := range vSlice {
28+
v, err := convertToString(rawV)
29+
if err != nil {
30+
return nil, err
31+
}
32+
33+
headersMap.Add(k, v)
34+
}
35+
}
36+
37+
return headersMap, nil
38+
}
39+
40+
func convertToString(
41+
in interface{},
42+
) (string, error) {
43+
if s, ok := in.(string); ok {
44+
return s, nil
45+
} else if b, ok := in.(bool); ok {
46+
return strconv.FormatBool(b), nil
47+
} else if f, ok := in.(float64); ok {
48+
return strconv.FormatFloat(f, 'f', -1, 64), nil
49+
} else if i, ok := in.(int64); ok {
50+
return strconv.FormatInt(i, 10), nil
51+
}
52+
53+
return "", fmt.Errorf("%+v not a valid header value", in)
54+
}

internal/inout/getRequest.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package inout
2+
3+
import (
4+
"net/http"
5+
"os"
6+
)
7+
8+
func GetRequest() (
9+
*http.Request,
10+
error,
11+
) {
12+
methodBytes, err := os.ReadFile(MethodFilePath)
13+
if err != nil {
14+
return nil, err
15+
}
16+
17+
urlBytes, err := os.ReadFile(URLFilePath)
18+
if err != nil {
19+
return nil, err
20+
}
21+
22+
reqBody, err := os.Open(BodyFilePath)
23+
if err != nil {
24+
return nil, err
25+
}
26+
27+
req, err := http.NewRequest(
28+
string(methodBytes),
29+
string(urlBytes),
30+
reqBody,
31+
)
32+
if err != nil {
33+
return nil, err
34+
}
35+
36+
if req.ContentLength == 0 {
37+
req.Body = nil
38+
}
39+
40+
req.Header, err = getHeaders()
41+
if err != nil {
42+
return nil, err
43+
}
44+
45+
return req, nil
46+
}

internal/inout/paths.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package inout
2+
3+
const (
4+
BodyFilePath = "/body.txt"
5+
HeadersFilePath = "/headers.json"
6+
MethodFilePath = "/method.txt"
7+
ProtocolFilePath = "/protocol.txt"
8+
StatusCodeFilePath = "/statusCode.txt"
9+
URLFilePath = "/url.txt"
10+
)

internal/inout/setBody.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package inout
2+
3+
import (
4+
"io"
5+
"os"
6+
)
7+
8+
func setBody(
9+
body io.ReadCloser,
10+
) error {
11+
bodyFile, err := os.Create(BodyFilePath)
12+
if err != nil {
13+
return err
14+
}
15+
defer bodyFile.Close()
16+
17+
_, err = io.Copy(bodyFile, body)
18+
defer body.Close()
19+
return err
20+
}

0 commit comments

Comments
 (0)