scribe/filename.go

94 строки
1.9 KiB
Go
Исходник Обычный вид История

2015-07-08 22:40:51 +03:00
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Contributor:
// - Aaron Meihm ameihm@mozilla.com
2015-07-14 07:34:50 +03:00
2015-07-08 22:40:51 +03:00
package scribe
import (
2015-07-14 06:01:15 +03:00
"fmt"
"path"
2015-07-08 22:40:51 +03:00
"regexp"
)
2017-07-19 18:55:45 +03:00
// FileName is used to perform tests against a given file name on
// the file system
type FileName struct {
2016-03-22 00:36:07 +03:00
Path string `json:"path,omitempty" yaml:"path,omitempty"`
File string `json:"file,omitempty" yaml:"file,omitempty"`
2015-07-08 22:40:51 +03:00
matches []nameMatch
}
type nameMatch struct {
path string
match string
}
func (f *FileName) isChain() bool {
2015-08-03 21:45:11 +03:00
return false
}
func (f *FileName) fireChains(d *Document) ([]evaluationCriteria, error) {
return nil, nil
2015-08-03 21:45:11 +03:00
}
func (f *FileName) mergeCriteria(c []evaluationCriteria) {
2015-08-03 21:45:11 +03:00
}
func (f *FileName) validate(d *Document) error {
2015-07-14 06:01:15 +03:00
if len(f.Path) == 0 {
return fmt.Errorf("filename path must be set")
}
if len(f.File) == 0 {
return fmt.Errorf("filename file must be set")
}
return nil
}
func (f *FileName) expandVariables(v []Variable) {
2015-07-08 22:40:51 +03:00
f.Path = variableExpansion(v, f.Path)
}
func (f *FileName) getCriteria() (ret []evaluationCriteria) {
2015-07-08 22:40:51 +03:00
for _, x := range f.matches {
n := evaluationCriteria{}
n.identifier = x.path
n.testValue = x.match
2015-07-08 22:40:51 +03:00
ret = append(ret, n)
}
return ret
}
func (f *FileName) prepare() error {
2015-07-08 22:40:51 +03:00
debugPrint("prepare(): analyzing file system, path %v, file \"%v\"\n", f.Path, f.File)
sfl := newSimpleFileLocator()
sfl.root = f.Path
err := sfl.locate(f.File, true)
if err != nil {
return err
}
re, err := regexp.Compile(f.File)
if err != nil {
return err
}
for _, x := range sfl.matches {
_, testFilename := path.Split(x)
mtch := re.FindStringSubmatch(testFilename)
2015-07-08 22:40:51 +03:00
if len(mtch) < 2 {
continue
}
nnm := nameMatch{}
nnm.path = x
nnm.match = mtch[1]
f.matches = append(f.matches, nnm)
}
return nil
}