github.com/hairyhenderson/gomplate/v4@v4.0.0-pre-2.0.20240520121557-362f058f0c93/config.go (about)

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