Merge pull request #2174 from dolanor/simple-go-guide
docs: add simple Go guide
This commit is contained in:
commit
8ef3b00b0f
33
docs/guides/1229-go-ci.md
Normal file
33
docs/guides/1229-go-ci.md
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
---
|
||||||
|
slug: /1229/go-ci
|
||||||
|
displayed_sidebar: '0.2'
|
||||||
|
---
|
||||||
|
|
||||||
|
# Testing and building a Go project
|
||||||
|
|
||||||
|
This guide explains how to run some tests and build a Go project with the `go` cue package.
|
||||||
|
|
||||||
|
## Plan
|
||||||
|
|
||||||
|
The plan consist of 2 actions:
|
||||||
|
|
||||||
|
- `test` for `go test`
|
||||||
|
- `build` for `go build`
|
||||||
|
|
||||||
|
both use the [Go dagger package](https://github.com/dagger/dagger/tree/main/pkg/universe.dagger.io/go)
|
||||||
|
|
||||||
|
```cue file=../plans/go-ci/plan.cue
|
||||||
|
```
|
||||||
|
|
||||||
|
## Go project
|
||||||
|
|
||||||
|
The project is a simple program that takes `$NAME` environment variable, use the [Go greeting package](https://github.com/dagger/dagger/tree/main/docs/plans/go-ci/hello/greeting) to print the `greeting.Greeting()` out.
|
||||||
|
|
||||||
|
```go file=../plans/go-ci/hello/main.go title=hello/main.go
|
||||||
|
```
|
||||||
|
|
||||||
|
```go file=../plans/go-ci/hello/greeting/greeting.go title=hello/greeting/greeting.go
|
||||||
|
```
|
||||||
|
|
||||||
|
```go file=../plans/go-ci/hello/greeting/greeting_test.go title=hello/greeting/greeting_test.go
|
||||||
|
```
|
3
docs/plans/go-ci/hello/go.mod
Normal file
3
docs/plans/go-ci/hello/go.mod
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
module dagger.io/testgreetci
|
||||||
|
|
||||||
|
go 1.17
|
7
docs/plans/go-ci/hello/greeting/greeting.go
Normal file
7
docs/plans/go-ci/hello/greeting/greeting.go
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package greeting
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func Greeting(name string) string {
|
||||||
|
return fmt.Sprintf("Hi %s!", name)
|
||||||
|
}
|
13
docs/plans/go-ci/hello/greeting/greeting_test.go
Normal file
13
docs/plans/go-ci/hello/greeting/greeting_test.go
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package greeting
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestGreeting(t *testing.T) {
|
||||||
|
name := "Dagger Test"
|
||||||
|
expect := "Hi Dagger Test!"
|
||||||
|
value := Greeting(name)
|
||||||
|
|
||||||
|
if expect != value {
|
||||||
|
t.Fatalf("Hello(%s) = '%s', expected '%s'", name, value, expect)
|
||||||
|
}
|
||||||
|
}
|
16
docs/plans/go-ci/hello/main.go
Normal file
16
docs/plans/go-ci/hello/main.go
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"dagger.io/testgreetci/greeting"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
name := os.Getenv("NAME")
|
||||||
|
if name == "" {
|
||||||
|
name = "John Doe"
|
||||||
|
}
|
||||||
|
fmt.Printf(greeting.Greeting(name))
|
||||||
|
}
|
23
docs/plans/go-ci/plan.cue
Normal file
23
docs/plans/go-ci/plan.cue
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"dagger.io/dagger"
|
||||||
|
|
||||||
|
"universe.dagger.io/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
dagger.#Plan & {
|
||||||
|
client: filesystem: "./hello": read: contents: dagger.#FS
|
||||||
|
|
||||||
|
actions: {
|
||||||
|
test: go.#Test & {
|
||||||
|
source: client.filesystem."./hello".read.contents
|
||||||
|
package: "./..."
|
||||||
|
}
|
||||||
|
|
||||||
|
build: go.#Build & {
|
||||||
|
source: client.filesystem."./hello".read.contents
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -55,6 +55,7 @@ module.exports = {
|
|||||||
"guides/pushing-plan-dependencies",
|
"guides/pushing-plan-dependencies",
|
||||||
"guides/handling-outputs",
|
"guides/handling-outputs",
|
||||||
"guides/migrate-from-dagger-0.1",
|
"guides/migrate-from-dagger-0.1",
|
||||||
|
"guides/go-ci",
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user