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

     1  // Code generated by genfig plugin 'update_from_env' on 2019-07-24T22:16:50+02:00; DO NOT EDIT.
     2  
     3  package config
     4  
     5  import (
     6  	"encoding/json"
     7  	"fmt"
     8  	"os"
     9  	"strconv"
    10  )
    11  
    12  var (
    13  	_ = os.LookupEnv
    14  	_ = fmt.Sprintf
    15  	_ = json.Marshal
    16  )
    17  
    18  func (c *Config) UpdateFromEnv() []error {
    19  	var val string
    20  	_ = val
    21  	var exists bool
    22  	_ = exists
    23  	var errors = []error{}
    24  
    25  	if val, exists = os.LookupEnv("APIS_GOOGLE_URI"); exists {
    26  		c.Apis.Google.Uri = val
    27  	}
    28  
    29  	if val, exists = os.LookupEnv("DB_PASS"); exists {
    30  		c.Db.Pass = val
    31  	}
    32  
    33  	if val, exists = os.LookupEnv("DB_URI"); exists {
    34  		c.Db.Uri = val
    35  	}
    36  
    37  	if val, exists = os.LookupEnv("DB_USER"); exists {
    38  		c.Db.User = val
    39  	}
    40  
    41  	if val, exists = os.LookupEnv("LONGDESC_DE"); exists {
    42  		c.LongDesc.De = val
    43  	}
    44  
    45  	if val, exists = os.LookupEnv("LONGDESC_EN"); exists {
    46  		c.LongDesc.En = val
    47  	}
    48  
    49  	if val, exists = os.LookupEnv("PROJECT"); exists {
    50  		c.Project = val
    51  	}
    52  
    53  	if val, exists = os.LookupEnv("RANDOMIZER_THRESHOLD"); exists {
    54  		if v, err := parseFloat64(val); err == nil {
    55  			c.Randomizer.Threshold = v
    56  		} else {
    57  			errors = append(errors, fmt.Errorf("Genfig: could not parse float64 from CONFIG_RANDOMIZER_THRESHOLD ('%s')\n", val))
    58  		}
    59  	}
    60  
    61  	if val, exists = os.LookupEnv("SECRETS"); exists {
    62  		if v, err := parseStringSlice(val); err == nil {
    63  			c.Secrets = v
    64  		} else {
    65  			errors = append(errors, fmt.Errorf("Genfig: could not parse []string from CONFIG_SECRETS ('%s')\n", val))
    66  		}
    67  	}
    68  
    69  	if val, exists = os.LookupEnv("SERVER_HOST"); exists {
    70  		c.Server.Host = val
    71  	}
    72  
    73  	if val, exists = os.LookupEnv("SERVER_PORT"); exists {
    74  		if v, err := parseInt64(val); err == nil {
    75  			c.Server.Port = v
    76  		} else {
    77  			errors = append(errors, fmt.Errorf("Genfig: could not parse int64 from CONFIG_SERVER_PORT ('%s')\n", val))
    78  		}
    79  	}
    80  
    81  	if val, exists = os.LookupEnv("VERSION"); exists {
    82  		c.Version = val
    83  	}
    84  
    85  	if val, exists = os.LookupEnv("WIP"); exists {
    86  		if v, err := parseBool(val); err == nil {
    87  			c.Wip = v
    88  		} else {
    89  			errors = append(errors, fmt.Errorf("Genfig: could not parse bool from CONFIG_WIP ('%s')\n", val))
    90  		}
    91  	}
    92  
    93  	if len(errors) == 0 {
    94  		return nil
    95  	} else {
    96  		return errors
    97  	}
    98  }
    99  
   100  // these are wrappers, so that they can
   101  // a) be referenced easily be the code generator and
   102  // b) be replaces easily by you (or me)
   103  func parseInt64(s string) (i int64, err error) {
   104  	i, err = strconv.ParseInt(s, 10, 0)
   105  	return
   106  }
   107  
   108  func parseFloat64(s string) (f float64, err error) {
   109  	f, err = strconv.ParseFloat(s, 0)
   110  	return
   111  }
   112  
   113  func parseBool(s string) (b bool, err error) {
   114  	b, err = strconv.ParseBool(s)
   115  	return
   116  }
   117  
   118  func parseStringSlice(s string) (a []string, err error) {
   119  	err = json.Unmarshal([]byte(s), &a)
   120  	return
   121  }
   122  
   123  func parseInt64Slice(s string) (a []int64, err error) {
   124  	err = json.Unmarshal([]byte(s), &a)
   125  	return
   126  }
   127  
   128  func parseFloat64Slice(s string) (a []float64, err error) {
   129  	err = json.Unmarshal([]byte(s), &a)
   130  	return
   131  }
   132  
   133  func parseInterfaceSlice(s string) (a []interface{}, err error) {
   134  	err = json.Unmarshal([]byte(s), &a)
   135  	return
   136  }