summaryrefslogtreecommitdiff
path: root/config.go
diff options
context:
space:
mode:
authorDave Henderson <dhenderson@gmail.com>2019-03-31 15:31:33 -0400
committerDave Henderson <dhenderson@gmail.com>2019-03-31 15:45:12 -0400
commita602ee3ec7c42ce53057e26fefdf8a949a10eb7b (patch)
treebef57b8f24931f507191c78578f90d403c13f820 /config.go
parent197ad689bc2922012e4573386214d3a844ca827e (diff)
Refactor Config
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'config.go')
-rw-r--r--config.go113
1 files changed, 113 insertions, 0 deletions
diff --git a/config.go b/config.go
new file mode 100644
index 00000000..26f48ee7
--- /dev/null
+++ b/config.go
@@ -0,0 +1,113 @@
+package gomplate
+
+import (
+ "os"
+ "strconv"
+ "strings"
+)
+
+// Config - values necessary for rendering templates with gomplate.
+// Mainly for use by the CLI
+type Config struct {
+ Input string
+ InputFiles []string
+ InputDir string
+ ExcludeGlob []string
+ OutputFiles []string
+ OutputDir string
+ OutMode string
+
+ DataSources []string
+ DataSourceHeaders []string
+ Contexts []string
+
+ LDelim string
+ RDelim string
+
+ Templates []string
+}
+
+// defaults - sets any unset fields to their default value (if applicable)
+func (o *Config) defaults() *Config {
+ if o.OutputDir == "" {
+ o.OutputDir = "."
+ }
+ if o.InputFiles == nil {
+ o.InputFiles = []string{"-"}
+ }
+ if o.OutputFiles == nil {
+ o.OutputFiles = []string{"-"}
+ }
+ if o.LDelim == "" {
+ o.LDelim = "{{"
+ }
+ if o.RDelim == "" {
+ o.RDelim = "}}"
+ }
+ return o
+}
+
+// parse an os.FileMode out of the string, and let us know if it's an override or not...
+func (o *Config) getMode() (os.FileMode, bool, error) {
+ modeOverride := o.OutMode != ""
+ m, err := strconv.ParseUint("0"+o.OutMode, 8, 32)
+ if err != nil {
+ return 0, false, err
+ }
+ mode := os.FileMode(m)
+ if mode == 0 && o.Input != "" {
+ mode = 0644
+ }
+ return mode, modeOverride, nil
+}
+
+// nolint: gocyclo
+func (o *Config) String() string {
+ o.defaults()
+
+ c := "input: "
+ if o.Input != "" {
+ c += "<arg>"
+ } else if o.InputDir != "" {
+ c += o.InputDir
+ } else {
+ c += strings.Join(o.InputFiles, ", ")
+ }
+
+ if len(o.ExcludeGlob) > 0 {
+ c += "\nexclude: " + strings.Join(o.ExcludeGlob, ", ")
+ }
+
+ c += "\noutput: "
+ if o.InputDir != "" && o.OutputDir != "." {
+ c += o.OutputDir
+ } else {
+ c += strings.Join(o.OutputFiles, ", ")
+ }
+
+ if o.OutMode != "" {
+ c += "\nchmod: " + o.OutMode
+ }
+
+ if len(o.DataSources) > 0 {
+ c += "\ndatasources: " + strings.Join(o.DataSources, ", ")
+ }
+ if len(o.DataSourceHeaders) > 0 {
+ c += "\ndatasourceheaders: " + strings.Join(o.DataSourceHeaders, ", ")
+ }
+ if len(o.Contexts) > 0 {
+ c += "\ncontexts: " + strings.Join(o.Contexts, ", ")
+ }
+
+ if o.LDelim != "{{" {
+ c += "\nleft_delim: " + o.LDelim
+ }
+ if o.RDelim != "}}" {
+ c += "\nright_delim: " + o.RDelim
+ }
+
+ if len(o.Templates) > 0 {
+ c += "\ntemplates: " + strings.Join(o.Templates, ", ")
+ }
+ return c
+}