--- a/cmd/utils.go Sun May 07 11:46:28 2017 +0200
+++ b/cmd/utils.go Sun May 07 11:56:37 2017 +0200
@@ -9,6 +9,8 @@
"fmt"
"io/ioutil"
"os"
+ "path/filepath"
+ "strings"
"github.com/pkg/errors"
"github.com/spf13/cobra"
@@ -57,7 +59,7 @@
if of == "template" {
opt = outputTemplate
if outputTemplateFile != "" {
- tmpl, err := ioutil.ReadFile(outputTemplateFile)
+ tmpl, err := readTemplate(outputTemplateFile, viper.GetString("template_directory"))
if err != nil {
return nil, err
}
@@ -67,6 +69,30 @@
return printer.NewPrinter(of, opt)
}
+func readTemplate(name, templateDir string) ([]byte, error) {
+ if strings.HasPrefix(name, "/") || strings.HasPrefix(name, "./") || strings.HasPrefix(name, "../") {
+ return ioutil.ReadFile(name)
+ }
+
+ if templateDir != "" {
+ // If the template file can be found in the template directory,
+ // use this file.
+ fullName := filepath.Join(templateDir, name)
+ if fileExists(fullName) {
+ name = fullName
+ }
+ }
+
+ return ioutil.ReadFile(name)
+}
+
+func fileExists(filename string) bool {
+ if _, err := os.Stat(filename); err != nil {
+ return false
+ }
+ return true
+}
+
func errPrint(format string, a ...interface{}) (n int, err error) {
return fmt.Fprintf(os.Stderr, format+"\n", a...)
}