-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add rescue workflow for redis conformance tests (#3)
- Loading branch information
Showing
8 changed files
with
271 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
` |