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"
|
2015-07-19 22:26:45 +03:00
|
|
|
"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
|
2015-10-15 18:55:38 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-10-15 18:55:38 +03:00
|
|
|
func (f *FileName) isChain() bool {
|
2015-08-03 21:45:11 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-10-15 18:55:38 +03:00
|
|
|
func (f *FileName) fireChains(d *Document) ([]evaluationCriteria, error) {
|
2015-08-04 23:38:17 +03:00
|
|
|
return nil, nil
|
2015-08-03 21:45:11 +03:00
|
|
|
}
|
|
|
|
|
2015-10-15 18:55:38 +03:00
|
|
|
func (f *FileName) mergeCriteria(c []evaluationCriteria) {
|
2015-08-03 21:45:11 +03:00
|
|
|
}
|
|
|
|
|
2015-10-15 18:55:38 +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
|
|
|
|
}
|
|
|
|
|
2015-10-15 18:55:38 +03:00
|
|
|
func (f *FileName) expandVariables(v []Variable) {
|
2015-07-08 22:40:51 +03:00
|
|
|
f.Path = variableExpansion(v, f.Path)
|
|
|
|
}
|
|
|
|
|
2015-10-15 18:55:38 +03:00
|
|
|
func (f *FileName) getCriteria() (ret []evaluationCriteria) {
|
2015-07-08 22:40:51 +03:00
|
|
|
for _, x := range f.matches {
|
2015-07-14 22:13:02 +03:00
|
|
|
n := evaluationCriteria{}
|
|
|
|
n.identifier = x.path
|
|
|
|
n.testValue = x.match
|
2015-07-08 22:40:51 +03:00
|
|
|
ret = append(ret, n)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2015-10-15 18:55:38 +03:00
|
|
|
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 {
|
2015-07-19 22:26:45 +03:00
|
|
|
_, 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
|
|
|
|
}
|