Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial setup of the new go SDK #1

Merged
merged 1 commit into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# IDEs
.vscode
66 changes: 66 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Make does not offer a recursive wildcard function, so here's one:
rwildcard=$(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2))

SHELL := /bin/bash
NAME := sdk-go
BUILD_TARGET = build
MAIN_SRC_FILE=main.go
GO := GO111MODULE=on go
GO_NOMOD :=GO111MODULE=off go
REV := $(shell git rev-parse --short HEAD 2> /dev/null || echo 'unknown')
ORG := cdevents
REPO := sdk-go
ORG_REPO := $(ORG)/$(REPO)
RELEASE_ORG_REPO := $(ORG_REPO)
ROOT_PACKAGE := github.com/$(ORG_REPO)
GO_VERSION := 1.18
GO_DEPENDENCIES := $(call rwildcard,pkg/,*.go) $(call rwildcard,cmd/,*.go)

BRANCH := $(shell git rev-parse --abbrev-ref HEAD 2> /dev/null || echo 'unknown')
BUILD_DATE := $(shell date +%Y%m%d-%H:%M:%S)
CGO_ENABLED = 0

REPORTS_DIR=$(BUILD_TARGET)/reports

GOTEST := $(GO) test

# set dev version unless VERSION is explicitly set via environment
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//')

ifdef DISABLE_TEST_CACHING
GOTEST += -count=1
endif

TEST_PACKAGE ?= ./...
COVER_OUT:=$(REPORTS_DIR)/cover.out
COVERFLAGS=-coverprofile=$(COVER_OUT) --covermode=count --coverpkg=./...

test: ## Run tests with the "unit" build tag
CGO_ENABLED=$(CGO_ENABLED) $(GOTEST) --tags=unit -failfast -short ./...

.PHONY: fmt
fmt: importfmt ## Format the code
$(eval FORMATTED = $(shell $(GO) fmt ./...))
@if [ "$(FORMATTED)" == "" ]; \
then \
echo "All Go files properly formatted"; \
else \
echo "Fixed formatting for: $(FORMATTED)"; \
fi

.PHONY: importfmt
importfmt: get-fmt-deps
@echo "Formatting the imports..."
goimports -w $(GO_DEPENDENCIES)

get-fmt-deps: ## Install test dependencies
$(GO_NOMOD) get golang.org/x/tools/cmd/goimports

.PHONY: lint
lint: ## Lint the code
./hack/gofmt.sh
./hack/linter.sh
./hack/generate.sh

.PHONY: all
all: fmt test lint
79 changes: 77 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,78 @@
# cdevents Go SDK
# Golang CDEvents SDK

Coming soon!
Golang SDK to emit [CDEvents](https://cdevents/dev).

The SDK can be used to create CDEvents and send them as CloudEvents, as well as parse a received CloudEvent into a CDEvent.

## Disclaimer 🚧

This SDK is work in work in progress, it will be maintained in sync with the
specification but it only covers a limited number of events.
For a wider range of events, the [old SDK/CLI][old-sdk] may be used, with the
caveat that the old SDK is not aligned with the new version of the specification.

## Get started

Add the module as dependency using go mod:

```golang
go get github.com/cdevents/sdk-go
```

And import the module in your code

```golang
import cdevents "github.com/cdevents/sdk-go"
```

## Create your first CDEvent

To create a CDEvent, for instance a [*pipelineRun queued*](https://cdevents.dev/docs/core/#pipelinerun-queued) one:

```golang
func main() {

// Create the base event
event, err := cdevent.NewCDEvent(cdevent.PipelineRunStartedEventV1)
if err != nil {
log.Fatalf("could not create a cdevent, %v", err)
}

// Set the required context fields
event.SetSubjectId("myPipelineRun1")
event.SetSource("my/first/cdevent/program")

// Set the required subject fields
event.SetSubjectPipelineName("myPipeline")
event.SetSubjectURL("https://example.com/myPipeline")
}
```

## Send your first CDEvent as CloudEvent

To send a CDEvent as CloudEvent:

```golang
func main() {
// (...) set the event first
ce := cdevents.AsCloudEvent(event)

// Set send options
ctx := cloudevents.ContextWithTarget(context.Background(), "http://localhost:8080/")
ctx = cloudevents.WithEncodingBinary(ctx)

// Sent the CloudEvent
if result := c.Send(ctx, ce); cloudevents.IsUndelivered(result) {
log.Fatalf("failed to send, %v", result)
}
}
```

See the [CloudEvents](https://github.com/cloudevents/sdk-go#send-your-first-cloudevent) docs as well.

## References

- [CDEvents](https://cdevents.dev)
- [CDFoundation SIG Events Vocabulary Draft](https://github.com/cdfoundation/sig-events/tree/main/vocabulary-draft)

[old-sdk]: https://github.com/cdfoundation/sig-events/tree/main/cde/sdk/go
40 changes: 40 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module github.com/cdevents/sdk-go

go 1.18

require (
github.com/cdfoundation/sig-events/cde/sdk/go v0.0.0-20220713121728-bb0ed97717e1
github.com/cloudevents/sdk-go/v2 v2.10.1
github.com/google/go-cmp v0.5.8
github.com/google/uuid v1.1.2
)

require (
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/satori/go.uuid v1.2.0 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/cobra v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.12.0 // indirect
github.com/subosito/gotenv v1.3.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.17.0 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0 // indirect
)
Loading