QBitRun/lib/workflow/open.go

110 lines
2.5 KiB
Go

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)
}