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.
37 lines
716 B
Go
37 lines
716 B
Go
package operation
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
gopath "path"
|
|
|
|
"github.com/aziis98/cabret"
|
|
"github.com/aziis98/cabret/path"
|
|
)
|
|
|
|
var _ cabret.Operation = Target{}
|
|
|
|
type Target struct {
|
|
PathTemplate string
|
|
}
|
|
|
|
func (op Target) Process(c cabret.Content) (*cabret.Content, error) {
|
|
mr, ok := c.Metadata[cabret.MatchResult].(map[string]string)
|
|
if !ok {
|
|
return nil, fmt.Errorf(`invalid match result type %T`, c.Metadata[cabret.MatchResult])
|
|
}
|
|
|
|
target := path.RenderTemplate(op.PathTemplate, mr)
|
|
|
|
if err := os.MkdirAll(gopath.Dir(target), 0777); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := os.WriteFile(target, c.Data, 0666); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
c.Metadata["Target"] = op.PathTemplate
|
|
return &c, nil
|
|
}
|