github.com/hairyhenderson/gomplate/v3@v3.11.7/config.go (about) 1 package gomplate 2 3 import ( 4 "io" 5 "os" 6 "strings" 7 8 "github.com/hairyhenderson/gomplate/v3/internal/config" 9 "github.com/hairyhenderson/gomplate/v3/internal/iohelpers" 10 ) 11 12 // Config - values necessary for rendering templates with gomplate. 13 // Mainly for use by the CLI 14 // 15 // Deprecated: this type will be phased out, internal/config.Config is used 16 // everywhere else, and will be exposed as API in a future version 17 type Config struct { 18 Input string 19 InputFiles []string 20 InputDir string 21 ExcludeGlob []string 22 OutputFiles []string 23 OutputDir string 24 OutputMap string 25 OutMode string 26 Out io.Writer 27 28 DataSources []string 29 DataSourceHeaders []string 30 Contexts []string 31 32 Plugins []string 33 34 LDelim string 35 RDelim string 36 37 Templates []string 38 } 39 40 // defaults - sets any unset fields to their default value (if applicable) 41 func (o *Config) defaults() *Config { 42 if o.OutputDir == "" { 43 o.OutputDir = "." 44 } 45 if o.InputFiles == nil { 46 o.InputFiles = []string{"-"} 47 } 48 if o.OutputFiles == nil { 49 o.OutputFiles = []string{"-"} 50 } 51 if o.LDelim == "" { 52 o.LDelim = "{{" 53 } 54 if o.RDelim == "" { 55 o.RDelim = "}}" 56 } 57 return o 58 } 59 60 //nolint:gocyclo 61 func (o *Config) String() string { 62 o.defaults() 63 64 c := "input: " 65 switch { 66 case o.Input != "": 67 c += "<arg>" 68 case o.InputDir != "": 69 c += o.InputDir 70 default: 71 c += strings.Join(o.InputFiles, ", ") 72 } 73 74 if len(o.ExcludeGlob) > 0 { 75 c += "\nexclude: " + strings.Join(o.ExcludeGlob, ", ") 76 } 77 78 c += "\noutput: " 79 switch { 80 case o.InputDir != "" && o.OutputDir != ".": 81 c += o.OutputDir 82 case o.OutputMap != "": 83 c += o.OutputMap 84 default: 85 c += strings.Join(o.OutputFiles, ", ") 86 } 87 88 if o.OutMode != "" { 89 c += "\nchmod: " + o.OutMode 90 } 91 92 if len(o.DataSources) > 0 { 93 c += "\ndatasources: " + strings.Join(o.DataSources, ", ") 94 } 95 if len(o.DataSourceHeaders) > 0 { 96 c += "\ndatasourceheaders: " + strings.Join(o.DataSourceHeaders, ", ") 97 } 98 if len(o.Contexts) > 0 { 99 c += "\ncontexts: " + strings.Join(o.Contexts, ", ") 100 } 101 102 if len(o.Plugins) > 0 { 103 c += "\nplugins: " + strings.Join(o.Plugins, ", ") 104 } 105 106 if o.LDelim != "{{" { 107 c += "\nleft_delim: " + o.LDelim 108 } 109 if o.RDelim != "}}" { 110 c += "\nright_delim: " + o.RDelim 111 } 112 113 if len(o.Templates) > 0 { 114 c += "\ntemplates: " + strings.Join(o.Templates, ", ") 115 } 116 return c 117 } 118 119 func (o *Config) toNewConfig() (*config.Config, error) { 120 cfg := &config.Config{ 121 Input: o.Input, 122 InputFiles: o.InputFiles, 123 InputDir: o.InputDir, 124 ExcludeGlob: o.ExcludeGlob, 125 OutputFiles: o.OutputFiles, 126 OutputDir: o.OutputDir, 127 OutputMap: o.OutputMap, 128 OutMode: o.OutMode, 129 LDelim: o.LDelim, 130 RDelim: o.RDelim, 131 Stdin: os.Stdin, 132 Stdout: &iohelpers.NopCloser{Writer: o.Out}, 133 Stderr: os.Stderr, 134 } 135 err := cfg.ParsePluginFlags(o.Plugins) 136 if err != nil { 137 return nil, err 138 } 139 err = cfg.ParseDataSourceFlags(o.DataSources, o.Contexts, o.Templates, o.DataSourceHeaders) 140 if err != nil { 141 return nil, err 142 } 143 return cfg, nil 144 }