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

     1  // Code generated by genfig plugin 'substitutor' on 2019-07-24T22:16:50+02:00; DO NOT EDIT.
     2  
     3  package config
     4  
     5  import (
     6  	"strings"
     7  )
     8  
     9  var _ = strings.Contains
    10  
    11  const (
    12  	maxSubstitutionIteraions = 5
    13  )
    14  
    15  var (
    16  	raw Config
    17  )
    18  
    19  // Substitute replaces all.
    20  // The return value informs, whether all substitutions could be
    21  // applied within {maxSubstitutionIteraions} or not
    22  func (c *Config) Substitute() bool {
    23  	c.ResetSubstitution()
    24  
    25  	// backup the "raw" configuration
    26  	raw = *c
    27  
    28  	run := 0
    29  	for {
    30  		if run == maxSubstitutionIteraions {
    31  			return false
    32  		}
    33  		if c.substitute() == 0 {
    34  			return true
    35  		}
    36  		run += 1
    37  	}
    38  }
    39  
    40  // ResetSubstitution resets the configuration to the state,
    41  // before the substitution was applied
    42  func (c *Config) ResetSubstitution() {
    43  	c = &raw
    44  }
    45  
    46  // substitute tries to replace all substitutions in strings
    47  func (c *Config) substitute() int {
    48  	cnt := 0
    49  
    50  	r := strings.NewReplacer(
    51  		"${apis.google.uri}", c.Apis.Google.Uri,
    52  
    53  		"${db.pass}", c.Db.Pass,
    54  
    55  		"${db.uri}", c.Db.Uri,
    56  
    57  		"${db.user}", c.Db.User,
    58  
    59  		"${longdesc.de}", c.LongDesc.De,
    60  
    61  		"${longdesc.en}", c.LongDesc.En,
    62  
    63  		"${project}", c.Project,
    64  
    65  		"${server.host}", c.Server.Host,
    66  
    67  		"${version}", c.Version,
    68  	)
    69  
    70  	if strings.Contains(c.Apis.Google.Uri, "${") {
    71  		cnt += 1
    72  		c.Apis.Google.Uri = r.Replace(c.Apis.Google.Uri)
    73  		if !strings.Contains(c.Apis.Google.Uri, "${") {
    74  			cnt -= 1
    75  		}
    76  	}
    77  
    78  	if strings.Contains(c.Db.Pass, "${") {
    79  		cnt += 1
    80  		c.Db.Pass = r.Replace(c.Db.Pass)
    81  		if !strings.Contains(c.Db.Pass, "${") {
    82  			cnt -= 1
    83  		}
    84  	}
    85  
    86  	if strings.Contains(c.Db.Uri, "${") {
    87  		cnt += 1
    88  		c.Db.Uri = r.Replace(c.Db.Uri)
    89  		if !strings.Contains(c.Db.Uri, "${") {
    90  			cnt -= 1
    91  		}
    92  	}
    93  
    94  	if strings.Contains(c.Db.User, "${") {
    95  		cnt += 1
    96  		c.Db.User = r.Replace(c.Db.User)
    97  		if !strings.Contains(c.Db.User, "${") {
    98  			cnt -= 1
    99  		}
   100  	}
   101  
   102  	if strings.Contains(c.LongDesc.De, "${") {
   103  		cnt += 1
   104  		c.LongDesc.De = r.Replace(c.LongDesc.De)
   105  		if !strings.Contains(c.LongDesc.De, "${") {
   106  			cnt -= 1
   107  		}
   108  	}
   109  
   110  	if strings.Contains(c.LongDesc.En, "${") {
   111  		cnt += 1
   112  		c.LongDesc.En = r.Replace(c.LongDesc.En)
   113  		if !strings.Contains(c.LongDesc.En, "${") {
   114  			cnt -= 1
   115  		}
   116  	}
   117  
   118  	if strings.Contains(c.Project, "${") {
   119  		cnt += 1
   120  		c.Project = r.Replace(c.Project)
   121  		if !strings.Contains(c.Project, "${") {
   122  			cnt -= 1
   123  		}
   124  	}
   125  
   126  	if strings.Contains(c.Server.Host, "${") {
   127  		cnt += 1
   128  		c.Server.Host = r.Replace(c.Server.Host)
   129  		if !strings.Contains(c.Server.Host, "${") {
   130  			cnt -= 1
   131  		}
   132  	}
   133  
   134  	if strings.Contains(c.Version, "${") {
   135  		cnt += 1
   136  		c.Version = r.Replace(c.Version)
   137  		if !strings.Contains(c.Version, "${") {
   138  			cnt -= 1
   139  		}
   140  	}
   141  
   142  	return cnt
   143  }