Fixing removal of filepath.join from writeTemplateToFile

This commit is contained in:
Ian Walter 2016-05-02 22:37:29 -04:00
parent 034973cce1
commit 274ca840ae
3 changed files with 14 additions and 11 deletions

View file

@ -145,15 +145,16 @@ func init() {
data["parentName"] = parentName()
data["cmdName"] = cmdName
file := filepath.Join(ProjectPath(), guessCmdDir(), cmdPath, cmdName+".go")
path := filepath.Join(ProjectPath(), guessCmdDir(), cmdPath)
filename := cmdName + ".go"
// Check if the file exists before trying to create it.
if _, err := os.Stat(file); os.IsNotExist(err) {
if err := writeTemplateToFile(file, template, data); err != nil {
if _, err := os.Stat(filepath.Join(path, filename)); os.IsNotExist(err) {
if err = writeTemplateToFile(path, filename, template, data); err != nil {
er(err)
}
fmt.Println(cmdName, "created at", file)
fmt.Println(cmdName, "created at", path)
} else {
fmt.Println(cmdName, "already exists at", file)
fmt.Println(cmdName, "already exists at", path)
}
}

View file

@ -224,13 +224,15 @@ func dirExists(path string) (bool, error) {
return false, err
}
func writeTemplateToFile(file string, template string, data interface{}) error {
func writeTemplateToFile(path, file, template string, data interface{}) error {
filename := filepath.Join(path, file)
r, err := templateToReader(template, data)
if err != nil {
return err
}
return safeWriteToDisk(file, r)
return safeWriteToDisk(filename, r)
}
func writeStringToFile(path, file, text string) error {

View file

@ -104,7 +104,7 @@ func createLicenseFile() {
buf := new(bytes.Buffer)
buf.ReadFrom(r)
err := writeTemplateToFile(ProjectPath()+"LICENSE", buf.String(), data)
err := writeTemplateToFile(ProjectPath(), "LICENSE", buf.String(), data)
_ = err
// if err != nil {
// er(err)
@ -139,7 +139,7 @@ func main() {
data["importpath"] = guessImportPath() + "/" + guessCmdDir()
err := writeTemplateToFile(ProjectPath()+"main.go", template, data)
err := writeTemplateToFile(ProjectPath(), "main.go", template, data)
_ = err
// if err != nil {
// er(err)
@ -233,8 +233,8 @@ func initConfig() {
data["viper"] = viper.GetBool("useViper")
err := writeTemplateToFile(ProjectPath()+string(os.PathSeparator)+guessCmdDir()+"root.go", template, data)
if err != nil {
path := ProjectPath() + string(os.PathSeparator) + guessCmdDir()
if err := writeTemplateToFile(path, "root.go", template, data); err != nil {
er(err)
}