From 274ca840ae9b1e51042a8bc7fac3a11577db391a Mon Sep 17 00:00:00 2001 From: Ian Walter Date: Mon, 2 May 2016 22:37:29 -0400 Subject: [PATCH] Fixing removal of filepath.join from writeTemplateToFile --- cobra/cmd/add.go | 11 ++++++----- cobra/cmd/helpers.go | 6 ++++-- cobra/cmd/init.go | 8 ++++---- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/cobra/cmd/add.go b/cobra/cmd/add.go index 70d4d7b1..08200f31 100644 --- a/cobra/cmd/add.go +++ b/cobra/cmd/add.go @@ -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) } } diff --git a/cobra/cmd/helpers.go b/cobra/cmd/helpers.go index 4218b034..8ee3bb0f 100644 --- a/cobra/cmd/helpers.go +++ b/cobra/cmd/helpers.go @@ -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 { diff --git a/cobra/cmd/init.go b/cobra/cmd/init.go index 8fe6c303..cb0c8fd7 100644 --- a/cobra/cmd/init.go +++ b/cobra/cmd/init.go @@ -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) }