update workflow interface

This commit is contained in:
Tigor Hutasuhut 2024-06-27 16:47:11 +07:00
parent 44c1f1a629
commit 5c338a27ee
5 changed files with 120 additions and 2 deletions

1
go.mod
View file

@ -6,6 +6,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/cobra v1.8.1 // indirect github.com/spf13/cobra v1.8.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.9.0 // indirect github.com/stretchr/testify v1.9.0 // indirect

2
go.sum
View file

@ -6,6 +6,8 @@ github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLf
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=

View file

@ -1,8 +1,10 @@
package qbitrun package qbitrun
import "github.com/tigorlazuardi/qbitrun/lib/workflow"
type QBitRun struct { type QBitRun struct {
context RunnerContext context RunnerContext
workflow WorkflowDir workflow workflow.WorkflowInput
} }
// New creates a QBitRun instance intended for single session. // New creates a QBitRun instance intended for single session.
@ -13,7 +15,7 @@ type QBitRun struct {
// Users must not use or modify the []io.ReadCloser in the WorkflowDir after // Users must not use or modify the []io.ReadCloser in the WorkflowDir after
// calling this function, especially if the WorkflowDir is created using // calling this function, especially if the WorkflowDir is created using
// WorkflowFromReadClosers or WorkflowFromReaders. // WorkflowFromReadClosers or WorkflowFromReaders.
func New(workflows WorkflowDir, ctx RunnerContext) *QBitRun { func New(workflows workflow.WorkflowInput, ctx RunnerContext) *QBitRun {
return &QBitRun{ return &QBitRun{
context: ctx, context: ctx,
workflow: workflows, workflow: workflows,

109
lib/workflow/open.go Normal file
View file

@ -0,0 +1,109 @@
package workflow
import (
"errors"
"io"
"io/fs"
"os"
"path/filepath"
"github.com/sourcegraph/conc/iter"
"gopkg.in/yaml.v3"
)
type WorkflowFiles []io.ReadCloser
func (wo WorkflowFiles) Close() error {
var errs []error
for _, rc := range wo {
if err := rc.Close(); err != nil {
errs = append(errs, err)
}
}
return errors.Join(errs...)
}
func (wo WorkflowFiles) Workflows() ([]Workflow, error) {
var (
errs = make([]error, len(wo))
workflows = make([]Workflow, len(wo))
)
iter.ForEachIdx(wo, func(i int, rc *io.ReadCloser) {
var w Workflow
if err := yaml.NewDecoder(*rc).Decode(&w); err != nil {
errs[i] = err
} else {
workflows[i] = w
}
if err := (*rc).Close(); err != nil && errs[i] == nil {
errs[i] = err
}
})
return workflows, errors.Join(errs...)
}
type DirFilter = func(path string, d fs.FileInfo) bool
// OpenDir opens a directory and returns a WorkflowInput.
//
// Set filter to filter out unwanted files.
func OpenDir(dir string, filters ...DirFilter) (WorkflowFiles, error) {
var files []io.ReadCloser
err := filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
ok := true
for _, filter := range filters {
ok = filter(path, info)
if !ok {
break
}
}
if ok {
file, err := os.Open(path)
if err != nil {
return err
}
files = append(files, file)
}
return nil
})
if err != nil {
return nil, err
}
return WorkflowFiles(files), nil
}
// WorkflowFromReadClosers creates a WorkflowInput from a list of io.ReadCloser.
//
// WorkflowFromReadClosers assumes ownership of the ReadClosers and will close
// all of them after the WorkflowInput is consumed (whether successfully or not).
func WorkflowFromReadClosers(rcs ...io.ReadCloser) WorkflowFiles {
return WorkflowFiles(rcs)
}
// WorkflowFromReaders creates a WorkflowInput from a list of io.Reader.
//
// If the Reader implements io.ReadCloser, it will be used as is.
// If not, it will be wrapped with io.NopCloser.
//
// WorkflowFromReaders assumes ownership of the Readers and will close (if implements io.ReadCloser)
// all of them after the WorkflowInput is consumed (whether successfully or not).
func WorkflowFromReaders(readers ...io.Reader) WorkflowFiles {
var rcs []io.ReadCloser
for _, r := range readers {
if rc, ok := r.(io.ReadCloser); ok {
rcs = append(rcs, rc)
} else {
rcs = append(rcs, io.NopCloser(r))
}
}
return WorkflowFiles(rcs)
}

View file

@ -1,5 +1,9 @@
package workflow package workflow
type WorkflowInput interface {
Workflows() ([]Workflow, error)
}
type Workflow struct { type Workflow struct {
Name string `yaml:"name"` Name string `yaml:"name"`
Disabled bool `yaml:"disabled"` Disabled bool `yaml:"disabled"`