Skip to content

Commit

Permalink
add rescue workflow for redis conformance tests (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
xh3b4sd authored Sep 28, 2023
1 parent 43292c8 commit a45b224
Show file tree
Hide file tree
Showing 8 changed files with 271 additions and 6 deletions.
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ generated for.
### Create Workflows

```
$ workflow create -h
workflow create -h
```

```
Create github workflows and config files.
Usage:
Expand All @@ -30,9 +33,10 @@ Available Commands:
pbfgo Create a protocol buffer workflow for golang code generation.
pbflint Create a protocol buffer workflow for schema validation.
pbfts Create a protocol buffer workflow for typescript code generation.
redigo Create a golang workflow for e.g. running redis conformance tests.
redigo Create a redis workflow for e.g. running conformance tests.
releasego Create a golang workflow for e.g. uploading cross compiled release assets.
releases3 Create a golang workflow for e.g. uploading cross compiled release assets.
rescue Create a redis workflow for e.g. running conformance tests.
typescript Create a typescript workflow for e.g. building and formatting typescript code.
Flags:
Expand All @@ -42,7 +46,10 @@ Use "workflow create [command] --help" for more information about a command.
```

```
$ workflow create dependabot -h
workflow create dependabot -h
```

```
Create a dependabot workflow for e.g. golang and docker.
Usage:
Expand All @@ -61,7 +68,10 @@ Flags:


```
$ workflow update all -h
workflow update all -h
```

```
Update all github workflows to the latest version. When creating a new
workflow file the original command instruction in form of os.Args is written
to the header of the workflow file. A typical workflow file header looks like
Expand Down
14 changes: 14 additions & 0 deletions cmd/create/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/xh3b4sd/workflow/cmd/create/redigo"
"github.com/xh3b4sd/workflow/cmd/create/releasego"
"github.com/xh3b4sd/workflow/cmd/create/releases3"
"github.com/xh3b4sd/workflow/cmd/create/rescue"
"github.com/xh3b4sd/workflow/cmd/create/typescript"
)

Expand Down Expand Up @@ -207,6 +208,18 @@ func New(config Config) (*cobra.Command, error) {
}
}

var rescueCmd *cobra.Command
{
c := rescue.Config{
Logger: config.Logger,
}

rescueCmd, err = rescue.New(c)
if err != nil {
return nil, tracer.Mask(err)
}
}

var typescriptCmd *cobra.Command
{
c := typescript.Config{
Expand Down Expand Up @@ -246,6 +259,7 @@ func New(config Config) (*cobra.Command, error) {
c.AddCommand(redigoCmd)
c.AddCommand(releasegoCmd)
c.AddCommand(releases3Cmd)
c.AddCommand(rescueCmd)
c.AddCommand(typescriptCmd)
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/create/redigo/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

const (
name = "redigo"
short = "Create a golang workflow for e.g. running redis conformance tests."
long = "Create a golang workflow for e.g. running redis conformance tests."
short = "Create a redis workflow for e.g. running conformance tests."
long = "Create a redis workflow for e.g. running conformance tests."
)

type Config struct {
Expand Down
44 changes: 44 additions & 0 deletions cmd/create/rescue/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package rescue

import (
"github.com/spf13/cobra"
"github.com/xh3b4sd/logger"
"github.com/xh3b4sd/tracer"
)

const (
name = "rescue"
short = "Create a redis workflow for e.g. running conformance tests."
long = "Create a redis workflow for e.g. running conformance tests."
)

type Config struct {
Logger logger.Interface
}

func New(con Config) (*cobra.Command, error) {
if con.Logger == nil {
return nil, tracer.Maskf(invalidConfigError, "%T.Logger must not be empty", con)
}

var c *cobra.Command
{
f := &flag{}

r := &runner{
fla: f,
log: con.Logger,
}

c = &cobra.Command{
Use: name,
Short: short,
Long: long,
RunE: r.Run,
}

f.Init(c)
}

return c, nil
}
23 changes: 23 additions & 0 deletions cmd/create/rescue/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package rescue

import (
"errors"

"github.com/xh3b4sd/tracer"
)

var invalidConfigError = &tracer.Error{
Kind: "invalidConfigError",
}

func IsInvalidConfig(err error) bool {
return errors.Is(err, invalidConfigError)
}

var invalidFlagError = &tracer.Error{
Kind: "invalidFlagError",
}

func IsInvalidFlag(err error) bool {
return errors.Is(err, invalidFlagError)
}
35 changes: 35 additions & 0 deletions cmd/create/rescue/flag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package rescue

import (
"strings"

"github.com/spf13/cobra"
"github.com/xh3b4sd/tracer"

"github.com/xh3b4sd/workflow/pkg/version"
)

type flag struct {
Version struct {
Golang string
}
}

func (f *flag) Init(cmd *cobra.Command) {
cmd.Flags().StringVarP(&f.Version.Golang, "version-golang", "g", version.Golang, "Golang version to use in, e.g. workflow files.")
}

func (f *flag) Validate() error {
{
if f.Version.Golang == "" {
return tracer.Maskf(invalidFlagError, "-g/--version-golang must not be empty")
}

s := strings.Split(f.Version.Golang, ".")
if len(s) != 3 {
return tracer.Maskf(invalidFlagError, "-g/--version-golang must have 3 parts like 1.15.2")
}
}

return nil
}
95 changes: 95 additions & 0 deletions cmd/create/rescue/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package rescue

import (
"bytes"
"context"
"os"
"strings"
"text/template"

"github.com/spf13/cobra"
"github.com/xh3b4sd/logger"
"github.com/xh3b4sd/tracer"

"github.com/xh3b4sd/workflow/pkg/version"
)

type runner struct {
fla *flag
log logger.Interface
}

func (r *runner) Run(cmd *cobra.Command, args []string) error {
ctx := context.Background()

err := r.fla.Validate()
if err != nil {
return tracer.Mask(err)
}

err = r.run(ctx, cmd, args)
if err != nil {
return tracer.Mask(err)
}

return nil
}

func (r *runner) data() interface{} {
type Data struct {
Command string
Version version.Version
}

return Data{
Command: strings.Join(os.Args, " "),
Version: version.Version{
Checkout: version.Checkout,
Golang: desiredVersion(r.fla.Version.Golang),
},
}
}

func (r *runner) run(ctx context.Context, cmd *cobra.Command, args []string) error {
{
p := ".github/workflows/"

err := os.MkdirAll(p, os.ModePerm)
if err != nil {
return tracer.Mask(err)
}
}

{
p := ".github/workflows/go-redis.yaml"

t, err := template.New(p).Parse(templateWorkflow)
if err != nil {
return tracer.Mask(err)
}

var b bytes.Buffer
err = t.ExecuteTemplate(&b, p, r.data())
if err != nil {
return tracer.Mask(err)
}

err = os.WriteFile(p, b.Bytes(), 0600)
if err != nil {
return tracer.Mask(err)
}
}

return nil
}

func desiredVersion(v string) string {
s := strings.Split(v, ".")
if len(s) != 3 {
// The given version must be something like 1.15.2 which implies 3 parts
// when splitting at the period.
panic("must have 3 parts")
}

return s[0] + "." + s[1]
}
44 changes: 44 additions & 0 deletions cmd/create/rescue/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package rescue

const templateWorkflow = `#
# Do not edit. This file was generated via the "workflow" command line tool.
# More information about the tool can be found at github.com/xh3b4sd/workflow.
#
# {{ .Command }}
#
name: "go-redis"
on: "push"
jobs:
go-redis:
runs-on: "ubuntu-latest"
container: "golang:{{ .Version.Golang }}-alpine"
services:
redis:
image: "redis"
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: "Setup Git Project"
uses: "actions/checkout@v{{ .Version.Checkout }}"
- name: "Install Race Dependency"
run: |
apk add gcc
apk add musl-dev
- name: "Check Go Tests"
env:
CGO_ENABLED: "1"
REDIS_HOST: "redis"
REDIS_PORT: "6379"
run: |
go test ./... -race -tags redis
`

0 comments on commit a45b224

Please sign in to comment.