Use Base and Stat instead of os.Seperator split

* Validates we're using filenames and ensures compatibility with all filesystems
This commit is contained in:
Patrick O'brien 2017-02-05 16:45:41 +10:00
parent e70ed0d070
commit dea061f571

View file

@ -215,23 +215,37 @@ func (s *State) processReplacements() error {
return errors.Errorf("replace parameters must have 2 arguments, given: %s", replace) return errors.Errorf("replace parameters must have 2 arguments, given: %s", replace)
} }
var toReplaceFname string
toReplace, replaceWith := splits[0], splits[1] toReplace, replaceWith := splits[0], splits[1]
toReplaceSplit := strings.Split(toReplace, string(filepath.Separator)) inf, err := os.Stat(toReplace)
fileName := toReplaceSplit[len(toReplaceSplit)-1] if err != nil {
return errors.Errorf("cannot stat %q", toReplace)
}
if inf.IsDir() {
return errors.Errorf("replace argument must be a path to a file not a dir: %q", toReplace)
}
toReplaceFname = inf.Name()
inf, err = os.Stat(replaceWith)
if err != nil {
return errors.Errorf("cannot stat %q", replaceWith)
}
if inf.IsDir() {
return errors.Errorf("replace argument must be a path to a file not a dir: %q", replaceWith)
}
var err error
switch filepath.Dir(toReplace) { switch filepath.Dir(toReplace) {
case templatesDirectory: case templatesDirectory:
err = replaceTemplate(s.Templates.Template, fileName, replaceWith) err = replaceTemplate(s.Templates.Template, toReplaceFname, replaceWith)
case templatesSingletonDirectory: case templatesSingletonDirectory:
err = replaceTemplate(s.SingletonTemplates.Template, fileName, replaceWith) err = replaceTemplate(s.SingletonTemplates.Template, toReplaceFname, replaceWith)
case templatesTestDirectory: case templatesTestDirectory:
err = replaceTemplate(s.TestTemplates.Template, fileName, replaceWith) err = replaceTemplate(s.TestTemplates.Template, toReplaceFname, replaceWith)
case templatesSingletonTestDirectory: case templatesSingletonTestDirectory:
err = replaceTemplate(s.SingletonTestTemplates.Template, fileName, replaceWith) err = replaceTemplate(s.SingletonTestTemplates.Template, toReplaceFname, replaceWith)
case templatesTestMainDirectory: case templatesTestMainDirectory:
err = replaceTemplate(s.TestMainTemplate, fileName, replaceWith) err = replaceTemplate(s.TestMainTemplate, toReplaceFname, replaceWith)
default: default:
return errors.Errorf("replace file's directory not part of any known folder: %s", toReplace) return errors.Errorf("replace file's directory not part of any known folder: %s", toReplace)
} }