grim/convey

Parents 853830158cca
Children 89afa53fab1b
Support directories for templating in the kubectl commands as well
--- a/kubectl/command.go Tue Feb 20 16:47:05 2018 -0600
+++ b/kubectl/command.go Tue Feb 20 17:24:04 2018 -0600
@@ -18,6 +18,7 @@
import (
"io/ioutil"
+ "os"
"path/filepath"
"github.com/aphistic/gomol"
@@ -71,6 +72,45 @@
return nil
}
+func templateEnvironmentFile(filename string, env []string) error {
+ // read the file
+ raw, err := ioutil.ReadFile(filename)
+ if err != nil {
+ return err
+ }
+
+ // replace the environment variables in it
+ data, err := environment.Mapper(string(raw), env)
+ if err != nil {
+ return err
+ }
+
+ // write the templated data back out
+ err = ioutil.WriteFile(filename, []byte(data), 0700)
+ if err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func templateEnvironmentDirectory(directory string, env []string) error {
+ files, err := ioutil.ReadDir(directory)
+ if err != nil {
+ return err
+ }
+
+ for _, file := range files {
+ absFile := filepath.Join(directory, file.Name())
+ err = templateEnvironmentFile(absFile, env)
+ if err != nil {
+ return err
+ }
+ }
+
+ return nil
+}
+
// templateEnvironment will copy files from the workspace into the scratch
// directory and replace environment variables with those that convey knows
// about.
@@ -92,23 +132,21 @@
// figure out the full path to it
absFile := filepath.Join(scratchDir, resolvedFile)
- // read the file
- raw, err := ioutil.ReadFile(absFile)
+ // check if we're looking at a file or a directory
+ entry, err := os.Stat(absFile)
if err != nil {
return err
}
- // replace the environment variables in it
- data, err := environment.Mapper(string(raw), env)
+ if entry.IsDir() {
+ err = templateEnvironmentDirectory(absFile, env)
+ } else {
+ err = templateEnvironmentFile(absFile, env)
+ }
if err != nil {
return err
}
- // write the templated data back out
- err = ioutil.WriteFile(absFile, []byte(data), 0700)
- if err != nil {
- return err
- }
}
return nil