return err instead of panic

pull/3144/head
Rajat Jindal 8 years ago
parent 420c3b776e
commit 33ce68bcdc

@ -137,7 +137,7 @@ func (t *templateCmd) run(cmd *cobra.Command, args []string) error {
if t.outputDir != "" { if t.outputDir != "" {
_, err = os.Stat(t.outputDir) _, err = os.Stat(t.outputDir)
if os.IsNotExist(err) { if os.IsNotExist(err) {
panic(fmt.Sprintf("output-dir '%s' does not exist", t.outputDir)) return fmt.Errorf("output-dir '%s' does not exist", t.outputDir)
} }
} }
@ -269,7 +269,10 @@ func (t *templateCmd) run(cmd *cobra.Command, args []string) error {
if whitespaceRegex.MatchString(data) { if whitespaceRegex.MatchString(data) {
continue continue
} }
writeToFile(t.outputDir, m.Name, data) err = writeToFile(t.outputDir, m.Name, data)
if err != nil {
return err
}
continue continue
} }
fmt.Printf("---\n# Source: %s\n", m.Name) fmt.Printf("---\n# Source: %s\n", m.Name)
@ -279,13 +282,17 @@ func (t *templateCmd) run(cmd *cobra.Command, args []string) error {
} }
// write the <data> to <output-dir>/<name> // write the <data> to <output-dir>/<name>
func writeToFile(outputDir string, name string, data string) { func writeToFile(outputDir string, name string, data string) error {
outfileName := strings.Join([]string{outputDir, name}, string(filepath.Separator)) outfileName := strings.Join([]string{outputDir, name}, string(filepath.Separator))
ensureDirectoryForFile(outfileName)
err := ensureDirectoryForFile(outfileName)
if err != nil {
return err
}
f, err := os.Create(outfileName) f, err := os.Create(outfileName)
if err != nil { if err != nil {
panic(err) return err
} }
defer f.Close() defer f.Close()
@ -293,23 +300,24 @@ func writeToFile(outputDir string, name string, data string) {
_, err = f.WriteString(fmt.Sprintf("##---\n# Source: %s\n%s", name, data)) _, err = f.WriteString(fmt.Sprintf("##---\n# Source: %s\n%s", name, data))
if err != nil { if err != nil {
panic(err) return err
} }
fmt.Printf("wrote %s\n", outfileName) fmt.Printf("wrote %s\n", outfileName)
return return nil
} }
// check if the directory exists to create file. creates if don't exists // check if the directory exists to create file. creates if don't exists
func ensureDirectoryForFile(file string) { func ensureDirectoryForFile(file string) error {
baseDir := path.Dir(file) baseDir := path.Dir(file)
_, err := os.Stat(baseDir) _, err := os.Stat(baseDir)
if !os.IsNotExist(err) { if err != nil && !os.IsNotExist(err) {
return return err
} }
err = os.MkdirAll(baseDir, defaultDirectoryPermission) err = os.MkdirAll(baseDir, defaultDirectoryPermission)
if err != nil { if err != nil {
panic(err) return err
} }
return nil
} }

Loading…
Cancel
Save