Skip to content

Commit

Permalink
feat: add ignore regex
Browse files Browse the repository at this point in the history
  • Loading branch information
alxndr13 committed Jun 13, 2023
1 parent 68981c6 commit eb29726
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 5 deletions.
37 changes: 37 additions & 0 deletions fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,43 @@ func CopyFile(fs afero.Fs, sourcePath, targetPath string) error {
return nil
}

// CopyFileFsToFs will copy the given sourcePath to the targetPath inside the passed afero.Fs.
func CopyFileFsToFs(sourceFs afero.Fs, targetFs afero.Fs, sourcePath, targetPath string) error {
sourceExists, err := afero.Exists(sourceFs, sourcePath)
if err != nil {
return fmt.Errorf("CopyFileFsToFs failed to check source file: %w", err)
}
if !sourceExists {
return fmt.Errorf("CopyFileFsToFs source path: %s: %w", sourcePath, os.ErrNotExist)
}

err = targetFs.MkdirAll(filepath.Dir(targetPath), 0755)
if err != nil {
return fmt.Errorf("CopyFileFsToFs failed to create path: %w", err)
}

sourceData, err := afero.ReadFile(sourceFs, sourcePath)
if err != nil {
return fmt.Errorf("CopyFileFsToFs failed to read source file: %w", err)
}

targetFile, err := targetFs.Create(targetPath)
if err != nil {
return fmt.Errorf("CopyFileFsToFs failed to create target file: %w", err)
}
defer targetFile.Close()

bytesWritten, err := targetFile.Write(sourceData)
if err != nil {
return fmt.Errorf("CopyFileFsToFs failed to write target file: %w", err)
}
if bytesWritten != len(sourceData) {
return fmt.Errorf("CopyFileFsToFs did not write all source file bytes into the target file, retry")
}

return nil
}

// WriteFile ensures that `targetPath` exists in the `fs` and then writes `data` into it.
func WriteFile(fs afero.Fs, targetPath string, data []byte, mode fs.FileMode) error {
err := fs.MkdirAll(filepath.Dir(targetPath), 0755)
Expand Down
18 changes: 14 additions & 4 deletions skipper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package skipper
import (
"fmt"
"path/filepath"
"regexp"

"github.com/spf13/afero"
)
Expand All @@ -11,10 +12,11 @@ import (
const skipperKey string = "skipper"

type SkipperConfig struct {
Classes []string `yaml:"use,omitempty"`
Components []ComponentConfig `mapstructure:"components,omitempty"`
Copies []CopyConfig `yaml:"copy,omitempty"`
Renames []RenameConfig `yaml:"rename,omitempty"`
Classes []string `yaml:"use,omitempty"`
Components []ComponentConfig `mapstructure:"components,omitempty"`
Copies []CopyConfig `yaml:"copy,omitempty"`
IgnoreRegex []string `yaml:"ignore_regex,omitempty"`
Renames []RenameConfig `yaml:"rename,omitempty"`
}

type CopyConfig struct {
Expand Down Expand Up @@ -51,6 +53,7 @@ func MergeSkipperConfig(merge ...*SkipperConfig) (mergedConfig *SkipperConfig) {
mergedConfig.Classes = append(mergedConfig.Classes, config.Classes...)
mergedConfig.Components = append(mergedConfig.Components, config.Components...)
mergedConfig.Copies = append(mergedConfig.Copies, config.Copies...)
mergedConfig.IgnoreRegex = append(mergedConfig.IgnoreRegex, config.IgnoreRegex...)
mergedConfig.Renames = append(mergedConfig.Renames, config.Renames...)
}
return mergedConfig
Expand All @@ -76,6 +79,13 @@ func LoadSkipperConfig(file *YamlFile, rootKey string) (*SkipperConfig, error) {
return nil, fmt.Errorf("failed to unmarshal SkipperConfig: %w", err)
}

for _, regex := range config.IgnoreRegex {
_, err := regexp.Compile(regex)
if err != nil {
return nil, fmt.Errorf("regex %s in ignore_regexp is not valid re2 regex", err)
}
}

return &config, nil
}

Expand Down
28 changes: 27 additions & 1 deletion template.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io/fs"
"path/filepath"
"regexp"
"strconv"
"strings"
"text/template"
Expand Down Expand Up @@ -44,14 +45,15 @@ var customFuncs map[string]any = map[string]any{

type Templater struct {
Files []*File
IgnoreRegex []*regexp.Regexp
templateRootPath string
outputRootPath string
templateFs afero.Fs
outputFs afero.Fs
templateFuncs template.FuncMap
}

func NewTemplater(fileSystem afero.Fs, templateRootPath, outputRootPath string, userFuncMap map[string]any) (*Templater, error) {
func NewTemplater(fileSystem afero.Fs, templateRootPath, outputRootPath string, userFuncMap map[string]any, ignoreRegex []string) (*Templater, error) {
t := &Templater{
templateFs: afero.NewBasePathFs(fileSystem, templateRootPath),
outputFs: afero.NewBasePathFs(fileSystem, outputRootPath),
Expand All @@ -77,6 +79,12 @@ func NewTemplater(fileSystem afero.Fs, templateRootPath, outputRootPath string,
return nil, fmt.Errorf("templateRootPath does not exist: %s", templateRootPath)
}

// Save the regexp
for _, v := range ignoreRegex {
r := regexp.MustCompile(v)
t.IgnoreRegex = append(t.IgnoreRegex, r)
}

// discover all files in the templateRootPath
err = afero.Walk(t.templateFs, "", func(filePath string, info fs.FileInfo, err error) error {
if info.IsDir() {
Expand Down Expand Up @@ -125,6 +133,15 @@ func (t *Templater) Execute(template *File, data any, allowNoValue bool, renameC
}

func (t *Templater) execute(tplFile *File, data any, targetPath string, allowNoValue bool) error {
for _, v := range t.IgnoreRegex {
if v.MatchString(tplFile.Path) {
err := CopyFileFsToFs(t.templateFs, t.outputFs, tplFile.Path, targetPath)
if err != nil {
return fmt.Errorf("could not copy file %s: %w", tplFile.Path, err)
}
return nil
}
}
err := tplFile.Load(t.templateFs)
if err != nil {
return err
Expand Down Expand Up @@ -218,3 +235,12 @@ func (t *Templater) getTemplateByPath(path string) *File {
}
return nil
}

func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}

0 comments on commit eb29726

Please sign in to comment.