github.com/hairyhenderson/templater@v3.5.0+incompatible/config.go (about)

     1  package gomplate
     2  
     3  import (
     4  	"os"
     5  	"strconv"
     6  	"strings"
     7  )
     8  
     9  // Config - values necessary for rendering templates with gomplate.
    10  // Mainly for use by the CLI
    11  type Config struct {
    12  	Input       string
    13  	InputFiles  []string
    14  	InputDir    string
    15  	ExcludeGlob []string
    16  	OutputFiles []string
    17  	OutputDir   string
    18  	OutputMap   string
    19  	OutMode     string
    20  
    21  	DataSources       []string
    22  	DataSourceHeaders []string
    23  	Contexts          []string
    24  
    25  	LDelim string
    26  	RDelim string
    27  
    28  	Templates []string
    29  }
    30  
    31  // defaults - sets any unset fields to their default value (if applicable)
    32  func (o *Config) defaults() *Config {
    33  	if o.OutputDir == "" {
    34  		o.OutputDir = "."
    35  	}
    36  	if o.InputFiles == nil {
    37  		o.InputFiles = []string{"-"}
    38  	}
    39  	if o.OutputFiles == nil {
    40  		o.OutputFiles = []string{"-"}
    41  	}
    42  	if o.LDelim == "" {
    43  		o.LDelim = "{{"
    44  	}
    45  	if o.RDelim == "" {
    46  		o.RDelim = "}}"
    47  	}
    48  	return o
    49  }
    50  
    51  // parse an os.FileMode out of the string, and let us know if it's an override or not...
    52  func (o *Config) getMode() (os.FileMode, bool, error) {
    53  	modeOverride := o.OutMode != ""
    54  	m, err := strconv.ParseUint("0"+o.OutMode, 8, 32)
    55  	if err != nil {
    56  		return 0, false, err
    57  	}
    58  	mode := os.FileMode(m)
    59  	if mode == 0 && o.Input != "" {
    60  		mode = 0644
    61  	}
    62  	return mode, modeOverride, nil
    63  }
    64  
    65  // nolint: gocyclo
    66  func (o *Config) String() string {
    67  	o.defaults()
    68  
    69  	c := "input: "
    70  	switch {
    71  	case o.Input != "":
    72  		c += "<arg>"
    73  	case o.InputDir != "":
    74  		c += o.InputDir
    75  	default:
    76  		c += strings.Join(o.InputFiles, ", ")
    77  	}
    78  
    79  	if len(o.ExcludeGlob) > 0 {
    80  		c += "\nexclude: " + strings.Join(o.ExcludeGlob, ", ")
    81  	}
    82  
    83  	c += "\noutput: "
    84  	switch {
    85  	case o.InputDir != "" && o.OutputDir != ".":
    86  		c += o.OutputDir
    87  	case o.OutputMap != "":
    88  		c += o.OutputMap
    89  	default:
    90  		c += strings.Join(o.OutputFiles, ", ")
    91  	}
    92  
    93  	if o.OutMode != "" {
    94  		c += "\nchmod: " + o.OutMode
    95  	}
    96  
    97  	if len(o.DataSources) > 0 {
    98  		c += "\ndatasources: " + strings.Join(o.DataSources, ", ")
    99  	}
   100  	if len(o.DataSourceHeaders) > 0 {
   101  		c += "\ndatasourceheaders: " + strings.Join(o.DataSourceHeaders, ", ")
   102  	}
   103  	if len(o.Contexts) > 0 {
   104  		c += "\ncontexts: " + strings.Join(o.Contexts, ", ")
   105  	}
   106  
   107  	if o.LDelim != "{{" {
   108  		c += "\nleft_delim: " + o.LDelim
   109  	}
   110  	if o.RDelim != "}}" {
   111  		c += "\nright_delim: " + o.RDelim
   112  	}
   113  
   114  	if len(o.Templates) > 0 {
   115  		c += "\ntemplates: " + strings.Join(o.Templates, ", ")
   116  	}
   117  	return c
   118  }