You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
1.7 KiB
Go
89 lines
1.7 KiB
Go
2 years ago
|
package operation
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"log"
|
||
|
"mime"
|
||
|
"os"
|
||
|
gopath "path"
|
||
|
|
||
|
"github.com/aziis98/cabret"
|
||
|
"github.com/aziis98/cabret/path"
|
||
|
)
|
||
|
|
||
|
func init() {
|
||
|
registerType("source", &Source{})
|
||
|
}
|
||
|
|
||
|
// Source is a ListOperation that appends the matched files to the processing items
|
||
|
type Source struct {
|
||
|
Patterns []string
|
||
|
}
|
||
|
|
||
|
func (op *Source) Load(config map[string]any) error {
|
||
|
if v, ok := config[ShortFormValueKey]; ok {
|
||
|
pattern, ok := v.(string)
|
||
|
if !ok {
|
||
|
return fmt.Errorf(`expected pattern but got "%v" of type %T`, v, v)
|
||
|
}
|
||
|
|
||
|
op.Patterns = []string{pattern}
|
||
|
return nil
|
||
|
}
|
||
|
if v, ok := config["paths"]; ok {
|
||
|
patterns, ok := v.([]string)
|
||
|
if !ok {
|
||
|
return fmt.Errorf(`expected list of patterns but got "%v" of type %T`, v, v)
|
||
|
}
|
||
|
|
||
|
op.Patterns = patterns
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
return fmt.Errorf(`invalid config for "source": %#v`, config)
|
||
|
}
|
||
|
|
||
|
func (op Source) ProcessList(contents []cabret.Content) ([]cabret.Content, error) {
|
||
|
files, err := cabret.FindFiles([]string{})
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
matches := []cabret.MatchResult{}
|
||
|
|
||
|
for _, patternStr := range op.Patterns {
|
||
|
pat, err := path.ParsePattern(patternStr)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
for _, f := range files {
|
||
|
if ok, captures, _ := pat.Match(f); ok {
|
||
|
matches = append(matches, cabret.MatchResult{
|
||
|
File: f,
|
||
|
Captures: captures,
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
for _, m := range matches {
|
||
|
log.Printf(`[operation.Source] reading "%s"`, m.File)
|
||
|
|
||
|
data, err := os.ReadFile(m.File)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
contents = append(contents, cabret.Content{
|
||
|
Type: mime.TypeByExtension(gopath.Ext(m.File)),
|
||
|
Data: data,
|
||
|
Metadata: cabret.Map{
|
||
|
cabret.MatchResultKey: m.Captures,
|
||
|
},
|
||
|
})
|
||
|
}
|
||
|
|
||
|
return contents, nil
|
||
|
}
|