github.com/thlcodes/genfig@v0.3.2-alpha/example/config/plugin_substitutor.go (about)

     1  // Code generated by genfig plugin 'substitutor'; DO NOT EDIT.
     2  
     3  package config
     4  
     5  import (
     6  	"os"
     7  	"regexp"
     8  	"strings"
     9  )
    10  
    11  var _ = strings.Contains
    12  
    13  const (
    14  	maxSubstitutionIteraions = 5
    15  )
    16  
    17  var (
    18  	raw Config
    19  )
    20  
    21  // Substitute replaces all.
    22  // The return value informs, whether all substitutions could be
    23  // applied within {maxSubstitutionIteraions} or not
    24  func (c *Config) Substitute() bool {
    25  	c.ResetSubstitution()
    26  
    27  	// backup the "raw" configuration
    28  	raw = *c
    29  
    30  	run := 0
    31  	for {
    32  		if run == maxSubstitutionIteraions {
    33  			return false
    34  		}
    35  		if c.substitute() == 0 {
    36  			return true
    37  		}
    38  		run += 1
    39  	}
    40  }
    41  
    42  // ResetSubstitution resets the configuration to the state,
    43  // before the substitution was applied
    44  func (c *Config) ResetSubstitution() {
    45  	c = &raw
    46  }
    47  
    48  // substitute tries to replace all substitutions in strings
    49  func (c *Config) substitute() int {
    50  	cnt := 0
    51  
    52  	envReplacer := regexp.MustCompile(`\${\w+}`)
    53  
    54  	r := strings.NewReplacer(
    55  		"${apis.google.uri}", c.Apis.Google.Uri,
    56  
    57  		"${db.pass}", c.Db.Pass,
    58  
    59  		"${db.uri}", c.Db.Uri,
    60  
    61  		"${db.user}", c.Db.User,
    62  
    63  		"${longdesc.de}", c.LongDesc.De,
    64  
    65  		"${longdesc.en}", c.LongDesc.En,
    66  
    67  		"${project}", c.Project,
    68  
    69  		"${server.host}", c.Server.Host,
    70  
    71  		"${version}", c.Version,
    72  	)
    73  
    74  	if strings.Contains(c.Apis.Google.Uri, "${") {
    75  		cnt += 1
    76  		c.Apis.Google.Uri = r.Replace(c.Apis.Google.Uri)
    77  		if !strings.Contains(c.Apis.Google.Uri, "${") {
    78  			cnt -= 1
    79  		} else {
    80  			c.Apis.Google.Uri = envReplacer.ReplaceAllStringFunc(c.Apis.Google.Uri, func(in string) (out string) {
    81  				out = in
    82  				envName := in[2 : len(in)-1]
    83  				if envVal, found := os.LookupEnv(envName); found {
    84  					out = envVal
    85  				}
    86  				return
    87  			})
    88  		}
    89  	}
    90  
    91  	if strings.Contains(c.Db.Pass, "${") {
    92  		cnt += 1
    93  		c.Db.Pass = r.Replace(c.Db.Pass)
    94  		if !strings.Contains(c.Db.Pass, "${") {
    95  			cnt -= 1
    96  		} else {
    97  			c.Db.Pass = envReplacer.ReplaceAllStringFunc(c.Db.Pass, func(in string) (out string) {
    98  				out = in
    99  				envName := in[2 : len(in)-1]
   100  				if envVal, found := os.LookupEnv(envName); found {
   101  					out = envVal
   102  				}
   103  				return
   104  			})
   105  		}
   106  	}
   107  
   108  	if strings.Contains(c.Db.Uri, "${") {
   109  		cnt += 1
   110  		c.Db.Uri = r.Replace(c.Db.Uri)
   111  		if !strings.Contains(c.Db.Uri, "${") {
   112  			cnt -= 1
   113  		} else {
   114  			c.Db.Uri = envReplacer.ReplaceAllStringFunc(c.Db.Uri, func(in string) (out string) {
   115  				out = in
   116  				envName := in[2 : len(in)-1]
   117  				if envVal, found := os.LookupEnv(envName); found {
   118  					out = envVal
   119  				}
   120  				return
   121  			})
   122  		}
   123  	}
   124  
   125  	if strings.Contains(c.Db.User, "${") {
   126  		cnt += 1
   127  		c.Db.User = r.Replace(c.Db.User)
   128  		if !strings.Contains(c.Db.User, "${") {
   129  			cnt -= 1
   130  		} else {
   131  			c.Db.User = envReplacer.ReplaceAllStringFunc(c.Db.User, func(in string) (out string) {
   132  				out = in
   133  				envName := in[2 : len(in)-1]
   134  				if envVal, found := os.LookupEnv(envName); found {
   135  					out = envVal
   136  				}
   137  				return
   138  			})
   139  		}
   140  	}
   141  
   142  	if strings.Contains(c.LongDesc.De, "${") {
   143  		cnt += 1
   144  		c.LongDesc.De = r.Replace(c.LongDesc.De)
   145  		if !strings.Contains(c.LongDesc.De, "${") {
   146  			cnt -= 1
   147  		} else {
   148  			c.LongDesc.De = envReplacer.ReplaceAllStringFunc(c.LongDesc.De, func(in string) (out string) {
   149  				out = in
   150  				envName := in[2 : len(in)-1]
   151  				if envVal, found := os.LookupEnv(envName); found {
   152  					out = envVal
   153  				}
   154  				return
   155  			})
   156  		}
   157  	}
   158  
   159  	if strings.Contains(c.LongDesc.En, "${") {
   160  		cnt += 1
   161  		c.LongDesc.En = r.Replace(c.LongDesc.En)
   162  		if !strings.Contains(c.LongDesc.En, "${") {
   163  			cnt -= 1
   164  		} else {
   165  			c.LongDesc.En = envReplacer.ReplaceAllStringFunc(c.LongDesc.En, func(in string) (out string) {
   166  				out = in
   167  				envName := in[2 : len(in)-1]
   168  				if envVal, found := os.LookupEnv(envName); found {
   169  					out = envVal
   170  				}
   171  				return
   172  			})
   173  		}
   174  	}
   175  
   176  	if strings.Contains(c.Project, "${") {
   177  		cnt += 1
   178  		c.Project = r.Replace(c.Project)
   179  		if !strings.Contains(c.Project, "${") {
   180  			cnt -= 1
   181  		} else {
   182  			c.Project = envReplacer.ReplaceAllStringFunc(c.Project, func(in string) (out string) {
   183  				out = in
   184  				envName := in[2 : len(in)-1]
   185  				if envVal, found := os.LookupEnv(envName); found {
   186  					out = envVal
   187  				}
   188  				return
   189  			})
   190  		}
   191  	}
   192  
   193  	if strings.Contains(c.Server.Host, "${") {
   194  		cnt += 1
   195  		c.Server.Host = r.Replace(c.Server.Host)
   196  		if !strings.Contains(c.Server.Host, "${") {
   197  			cnt -= 1
   198  		} else {
   199  			c.Server.Host = envReplacer.ReplaceAllStringFunc(c.Server.Host, func(in string) (out string) {
   200  				out = in
   201  				envName := in[2 : len(in)-1]
   202  				if envVal, found := os.LookupEnv(envName); found {
   203  					out = envVal
   204  				}
   205  				return
   206  			})
   207  		}
   208  	}
   209  
   210  	if strings.Contains(c.Version, "${") {
   211  		cnt += 1
   212  		c.Version = r.Replace(c.Version)
   213  		if !strings.Contains(c.Version, "${") {
   214  			cnt -= 1
   215  		} else {
   216  			c.Version = envReplacer.ReplaceAllStringFunc(c.Version, func(in string) (out string) {
   217  				out = in
   218  				envName := in[2 : len(in)-1]
   219  				if envVal, found := os.LookupEnv(envName); found {
   220  					out = envVal
   221  				}
   222  				return
   223  			})
   224  		}
   225  	}
   226  
   227  	return cnt
   228  }