Skip to content

Commit 872abe4

Browse files
committed
Initial setup of the new go SDK
The new SDK provides support for PipelineRun events, cloudevents binding and JSON binding. It include tests to verify that the JSON produced by the SDK matches what's defined in the spec. There is no CLI functionality included, this can be consumed as a library as described in the README. Signed-off-by: Andrea Frittoli <[email protected]>
1 parent a5c7976 commit 872abe4

20 files changed

+2684
-2
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# IDEs
2+
.vscode

Makefile

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Make does not offer a recursive wildcard function, so here's one:
2+
rwildcard=$(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2))
3+
4+
SHELL := /bin/bash
5+
NAME := sdk-go
6+
BUILD_TARGET = build
7+
MAIN_SRC_FILE=main.go
8+
GO := GO111MODULE=on go
9+
GO_NOMOD :=GO111MODULE=off go
10+
REV := $(shell git rev-parse --short HEAD 2> /dev/null || echo 'unknown')
11+
ORG := cdevents
12+
REPO := sdk-go
13+
ORG_REPO := $(ORG)/$(REPO)
14+
RELEASE_ORG_REPO := $(ORG_REPO)
15+
ROOT_PACKAGE := github.com/$(ORG_REPO)
16+
GO_VERSION := 1.18
17+
GO_DEPENDENCIES := $(call rwildcard,pkg/,*.go) $(call rwildcard,cmd/,*.go)
18+
19+
BRANCH := $(shell git rev-parse --abbrev-ref HEAD 2> /dev/null || echo 'unknown')
20+
BUILD_DATE := $(shell date +%Y%m%d-%H:%M:%S)
21+
CGO_ENABLED = 0
22+
23+
REPORTS_DIR=$(BUILD_TARGET)/reports
24+
25+
GOTEST := $(GO) test
26+
27+
# set dev version unless VERSION is explicitly set via environment
28+
VERSION ?= $(shell echo "$$(git for-each-ref refs/tags/ --count=1 --sort=-version:refname --format='%(refname:short)' 2>/dev/null)-dev+$(REV)" | sed 's/^v//')
29+
30+
ifdef DISABLE_TEST_CACHING
31+
GOTEST += -count=1
32+
endif
33+
34+
TEST_PACKAGE ?= ./...
35+
COVER_OUT:=$(REPORTS_DIR)/cover.out
36+
COVERFLAGS=-coverprofile=$(COVER_OUT) --covermode=count --coverpkg=./...
37+
38+
test: ## Run tests with the "unit" build tag
39+
CGO_ENABLED=$(CGO_ENABLED) $(GOTEST) --tags=unit -failfast -short ./...
40+
41+
.PHONY: fmt
42+
fmt: importfmt ## Format the code
43+
$(eval FORMATTED = $(shell $(GO) fmt ./...))
44+
@if [ "$(FORMATTED)" == "" ]; \
45+
then \
46+
echo "All Go files properly formatted"; \
47+
else \
48+
echo "Fixed formatting for: $(FORMATTED)"; \
49+
fi
50+
51+
.PHONY: importfmt
52+
importfmt: get-fmt-deps
53+
@echo "Formatting the imports..."
54+
goimports -w $(GO_DEPENDENCIES)
55+
56+
get-fmt-deps: ## Install test dependencies
57+
$(GO_NOMOD) get golang.org/x/tools/cmd/goimports
58+
59+
.PHONY: lint
60+
lint: ## Lint the code
61+
./hack/gofmt.sh
62+
./hack/linter.sh
63+
./hack/generate.sh
64+
65+
.PHONY: all
66+
all: fmt test lint

README.md

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,78 @@
1-
# cdevents Go SDK
1+
# Golang CDEvents SDK
22

3-
Coming soon!
3+
Golang SDK to emit [CDEvents](https://cdevents/dev).
4+
5+
The SDK can be used to create CDEvents and send them as CloudEvents, as well as parse a received CloudEvent into a CDEvent.
6+
7+
## Disclaimer 🚧
8+
9+
This SDK is work in work in progress, it will be maintained in sync with the
10+
specification but it only covers a limited number of events.
11+
For a wider range of events, the [old SDK/CLI][old-sdk] may be used, with the
12+
caveat that the old SDK is not aligned with the new version of the specification.
13+
14+
## Get started
15+
16+
Add the module as dependency using go mod:
17+
18+
```golang
19+
go get github.com/cdevents/sdk-go
20+
```
21+
22+
And import the module in your code
23+
24+
```golang
25+
import cdevents "github.com/cdevents/sdk-go"
26+
```
27+
28+
## Create your first CDEvent
29+
30+
To create a CDEvent, for instance a [*pipelineRun queued*](https://cdevents.dev/docs/core/#pipelinerun-queued) one:
31+
32+
```golang
33+
func main() {
34+
35+
// Create the base event
36+
event, err := cdevent.NewCDEvent(cdevent.PipelineRunStartedEventV1)
37+
if err != nil {
38+
log.Fatalf("could not create a cdevent, %v", err)
39+
}
40+
41+
// Set the required context fields
42+
event.SetSubjectId("myPipelineRun1")
43+
event.SetSource("my/first/cdevent/program")
44+
45+
// Set the required subject fields
46+
event.SetPipelineName("myPipeline")
47+
event.SetPipelineURL("https://example.com/myPipeline")
48+
}
49+
```
50+
51+
## Send your first CDEvent as CloudEvent
52+
53+
To send a CDEvent as CloudEvent:
54+
55+
```golang
56+
func main() {
57+
// (...) set the event first
58+
ce := cdevents.AsCloudEvent(event)
59+
60+
// Set send options
61+
ctx := cloudevents.ContextWithTarget(context.Background(), "http://localhost:8080/")
62+
ctx = cloudevents.WithEncodingBinary(ctx)
63+
64+
// Sent the CloudEvent
65+
if result := c.Send(ctx, ce); cloudevents.IsUndelivered(result) {
66+
log.Fatalf("failed to send, %v", result)
67+
}
68+
}
69+
```
70+
71+
See the [CloudEvents](https://github.com/cloudevents/sdk-go#send-your-first-cloudevent) docs as well.
72+
73+
## References
74+
75+
- [CDEvents](https://cdevents.dev)
76+
- [CDFoundation SIG Events Vocabulary Draft](https://github.com/cdfoundation/sig-events/tree/main/vocabulary-draft)
77+
78+
[old-sdk]: https://github.com/cdfoundation/sig-events/tree/main/cde/sdk/go

go.mod

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
module github.com/cdevents/sdk-go
2+
3+
go 1.18
4+
5+
require (
6+
github.com/cdfoundation/sig-events/cde/sdk/go v0.0.0-20220713121728-bb0ed97717e1
7+
github.com/cloudevents/sdk-go/v2 v2.10.1
8+
github.com/google/go-cmp v0.5.8
9+
github.com/google/uuid v1.1.2
10+
)
11+
12+
require (
13+
github.com/fsnotify/fsnotify v1.5.4 // indirect
14+
github.com/hashicorp/hcl v1.0.0 // indirect
15+
github.com/inconshreveable/mousetrap v1.0.0 // indirect
16+
github.com/json-iterator/go v1.1.12 // indirect
17+
github.com/magiconair/properties v1.8.6 // indirect
18+
github.com/mitchellh/go-homedir v1.1.0 // indirect
19+
github.com/mitchellh/mapstructure v1.5.0 // indirect
20+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
21+
github.com/modern-go/reflect2 v1.0.2 // indirect
22+
github.com/pelletier/go-toml v1.9.5 // indirect
23+
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
24+
github.com/satori/go.uuid v1.2.0 // indirect
25+
github.com/spf13/afero v1.8.2 // indirect
26+
github.com/spf13/cast v1.5.0 // indirect
27+
github.com/spf13/cobra v1.5.0 // indirect
28+
github.com/spf13/jwalterweatherman v1.1.0 // indirect
29+
github.com/spf13/pflag v1.0.5 // indirect
30+
github.com/spf13/viper v1.12.0 // indirect
31+
github.com/subosito/gotenv v1.3.0 // indirect
32+
go.uber.org/atomic v1.7.0 // indirect
33+
go.uber.org/multierr v1.6.0 // indirect
34+
go.uber.org/zap v1.17.0 // indirect
35+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
36+
golang.org/x/text v0.3.7 // indirect
37+
gopkg.in/ini.v1 v1.66.4 // indirect
38+
gopkg.in/yaml.v2 v2.4.0 // indirect
39+
gopkg.in/yaml.v3 v3.0.0 // indirect
40+
)

0 commit comments

Comments
 (0)