Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdostert committed Nov 17, 2024
0 parents commit e5730cf
Show file tree
Hide file tree
Showing 22 changed files with 502 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: build

on: push

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1

- name: Install Opctl
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

- name: Build
run: opctl run build
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Jetbrains
.idea/

# opctl
*args*.yml
10 changes: 10 additions & 0 deletions .opspec/build/op.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: build
description: builds the op
run:
serial:
- op:
ref: github.com/opspec-pkgs/_.op.bootstrap#4.1.1
inputs:
srcDir: $(../..)
- op:
ref: test
35 changes: 35 additions & 0 deletions .opspec/e2eTest/op.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: e2eTest
run:
serial:
- op:
ref: $(../../)
inputs:
headers:
Content-Type:
- text/html
url: https://google.com
method: GET
outputs:
body:
headers:
protocol:
statusCode:
- container:
image:
ref: alpine
cmd:
- sh
- -ce
- |
echo $(protocol) $(statusCode)
echo 'Content-Type'
echo $(headers[Content-Type][0])
echo 'headers'
cat /headers
echo ""
echo "body"
cat /body
sleep 1
files:
/body: $(body)
/headers: $(headers)
44 changes: 44 additions & 0 deletions .opspec/provision-image/op.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: provision-image
description: |
Provisions image for the op to [opspec-pkgs image registry](https://github.com/orgs/opspec-pkgs/packages) w/ name `$(opDotYml.name):${opDotYml.version}`.
> Before first use, a Dockerfile MUST be added to `.opspec/provision-image`
inputs:
githubUsername:
string:
constraints: { minLength: 1 }
description: user for logging in to github
githubAccessToken:
string:
constraints: { minLength: 1 }
description: accessToken for logging in to github
isSecret: true
run:
serial:
- op:
ref: github.com/opspec-pkgs/yaml.parse#1.0.0
inputs:
yaml: $(../../op.yml)
outputs:
result: $(opDotYml)
- container:
image: { ref: 'alpine:3.6' }
cmd: [sh, -ce, 'echo -n ghcr.io/opspec-pkgs/${opName#*/*/}:$(opDotYml.version) > /imageName']
envVars:
opName: $(opDotYml.name)
files:
/imageName: $(imageName)
- op:
ref: github.com/opspec-pkgs/docker.image.build#2.1.0
inputs:
dockerfile: $(./Dockerfile)
imageName:
outputs: { imageTar }
- op:
ref: github.com/opspec-pkgs/docker.image.push#2.1.0
inputs:
imageTar:
imageName:
registry: ghcr.io
username: $(githubUsername)
password: $(githubAccessToken)
43 changes: 43 additions & 0 deletions .opspec/release/op.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: release
description: releases the op
inputs:
githubUsername:
string:
constraints: { minLength: 1 }
description: user for logging in to github
githubAccessToken:
string:
constraints: { minLength: 1 }
description: accessToken for logging in to github
isSecret: true
run:
serial:
- op:
ref: build
- op:
ref: github.com/opspec-pkgs/yaml.parse#1.0.0
inputs:
yaml: $(../../op.yml)
outputs:
result: $(opDotYml)
- op:
ref: github.com/opspec-pkgs/git.repo.resolve-commit#1.0.2
inputs: { dotGitDir: $(../../.git) }
outputs: { commit: }
- container:
image: { ref: 'alpine:3.6' }
cmd: [sh, -ce, 'echo -n ${opName#*/*/} > /opRepo']
envVars:
opName: $(opDotYml.name)
files:
/opRepo: $(opRepo)
- op:
ref: github.com/opspec-pkgs/github.release.create#1.1.0
inputs:
owner: opspec-pkgs
repo: $(opRepo)
loginUsername: $(githubUsername)
loginPassword: $(githubAccessToken)
tag: $(opDotYml.version)
commitish: $(commit)
name: $(opDotYml.version)
8 changes: 8 additions & 0 deletions .opspec/test/op.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: test
description: tests the op
run:
op:
ref: github.com/opspec-pkgs/opctl.op.validate#1.0.0
inputs:
opctlVersion: 0.1.53
op: $(../..)
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Change Log

All notable changes will be documented in this file in accordance with
[![keepachangelog 1.0.0](https://img.shields.io/badge/keepachangelog-1.0.0-brightgreen.svg)](http://keepachangelog.com/en/1.0.0/)

## \[Unreleased]
1 change: 1 addition & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @opspec-pkgs/maintainers
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
[![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)

<img src="icon.svg" alt="icon" height="100px">

# Problem statement

Sends an http request

# Example usage

## Visualize

```shell
opctl ui github.com/opspec-pkgs/http.send#1.0.0
```

## Run

```
opctl run github.com/opspec-pkgs/http.send#1.0.0
```

## Compose

```yaml
op:
ref: github.com/opspec-pkgs/http.send#1.0.0
inputs:
method: # 👈 required; provide a value
url: # 👈 required; provide a value
## uncomment to override defaults
# body: "[object Object]"
# headers: [object Object]
outputs:
body:
headers:
protocol:
statusCode:
```
# Support
join us on
[![Slack](https://img.shields.io/badge/slack-opctl-E01563.svg)](https://join.slack.com/t/opctl/shared_invite/zt-51zodvjn-Ul_UXfkhqYLWZPQTvNPp5w)
or
[open an issue](https://github.com/opspec-pkgs/http.send/issues)
# Releases
releases are versioned according to
[![semver 2.0.0](https://img.shields.io/badge/semver-2.0.0-brightgreen.svg)](http://semver.org/spec/v2.0.0.html)
and [tagged](https://git-scm.com/book/en/v2/Git-Basics-Tagging); see
[CHANGELOG.md](CHANGELOG.md) for release notes
# Contributing
see
[project/CONTRIBUTING.md](https://github.com/opspec-pkgs/project/blob/main/CONTRIBUTING.md)
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/opspec-pkgs/http.send

go 1.23.2
Empty file added go.sum
Empty file.
10 changes: 10 additions & 0 deletions icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions internal/inout/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// package inout is used to process op input to an http request and http response to op output
package inout
54 changes: 54 additions & 0 deletions internal/inout/getHeaders.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package inout

import (
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
)

func getHeaders() (
map[string][]string,
error,
) {
headersJSONBytes, err := os.ReadFile(HeadersFilePath)
if err != nil {
return nil, err
}

rawHeaders := map[string][]interface{}{}
if err := json.Unmarshal(headersJSONBytes, &rawHeaders); err != nil {
return nil, err
}

headersMap := http.Header{}
for k, vSlice := range rawHeaders {
for _, rawV := range vSlice {
v, err := convertToString(rawV)
if err != nil {
return nil, err
}

headersMap.Add(k, v)
}
}

return headersMap, nil
}

func convertToString(
in interface{},
) (string, error) {
if s, ok := in.(string); ok {
return s, nil
} else if b, ok := in.(bool); ok {
return strconv.FormatBool(b), nil
} else if f, ok := in.(float64); ok {
return strconv.FormatFloat(f, 'f', -1, 64), nil
} else if i, ok := in.(int64); ok {
return strconv.FormatInt(i, 10), nil
}

return "", fmt.Errorf("%+v not a valid header value", in)
}
46 changes: 46 additions & 0 deletions internal/inout/getRequest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package inout

import (
"net/http"
"os"
)

func GetRequest() (
*http.Request,
error,
) {
methodBytes, err := os.ReadFile(MethodFilePath)
if err != nil {
return nil, err
}

urlBytes, err := os.ReadFile(URLFilePath)
if err != nil {
return nil, err
}

reqBody, err := os.Open(BodyFilePath)
if err != nil {
return nil, err
}

req, err := http.NewRequest(
string(methodBytes),
string(urlBytes),
reqBody,
)
if err != nil {
return nil, err
}

if req.ContentLength == 0 {
req.Body = nil
}

req.Header, err = getHeaders()
if err != nil {
return nil, err
}

return req, nil
}
10 changes: 10 additions & 0 deletions internal/inout/paths.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package inout

const (
BodyFilePath = "/body.txt"
HeadersFilePath = "/headers.json"
MethodFilePath = "/method.txt"
ProtocolFilePath = "/protocol.txt"
StatusCodeFilePath = "/statusCode.txt"
URLFilePath = "/url.txt"
)
20 changes: 20 additions & 0 deletions internal/inout/setBody.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package inout

import (
"io"
"os"
)

func setBody(
body io.ReadCloser,
) error {
bodyFile, err := os.Create(BodyFilePath)
if err != nil {
return err
}
defer bodyFile.Close()

_, err = io.Copy(bodyFile, body)
defer body.Close()
return err
}
Loading

0 comments on commit e5730cf

Please sign in to comment.