env.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package env
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "os"
  7. "sort"
  8. "github.com/joho/godotenv"
  9. )
  10. const KEIJI_USERNAME = "KEIJI_USERNAME"
  11. const KEIJI_PASSWORD = "KEIJI_PASSWORD"
  12. const IMAGE_STORE = "IMAGE_STORE"
  13. const WEB_ROOT = "WEB_ROOT"
  14. const DOMAIN_NAME = "DOMAIN_NAME"
  15. const HOST_PORT = "HOST_PORT"
  16. const HOST_ADDR = "HOST_ADDR"
  17. const USE_SSL = "USE_SSL"
  18. const CHAIN = "CHAIN"
  19. const KEY = "KEY"
  20. var OPTION_VARS = map[string]string{
  21. IMAGE_STORE: "#the location for keiji to store the images uploaded (string)",
  22. WEB_ROOT: "#the location to pull HTML and various web assets from. Only if using 'keiji -content fs' (string)",
  23. CHAIN: "#the path to the SSL public key chain (string)",
  24. KEY: "#the path to the SSL private key (string)",
  25. }
  26. var REQUIRED_VARS = map[string]string{
  27. HOST_PORT: "#the port to run the server on (int)",
  28. HOST_ADDR: "#the address for the server to listen on (string)",
  29. DOMAIN_NAME: "#the servers domain name, i.e. 'aetherial.dev', or 'localhost' (string)",
  30. USE_SSL: "#chose to use SSL or not (boolean)",
  31. KEIJI_USERNAME: "#the administrator username to login with",
  32. KEIJI_PASSWORD: "#the password for the administrator accounit",
  33. }
  34. type EnvNotSet struct {
  35. NotSet string
  36. }
  37. func (e *EnvNotSet) Error() string {
  38. return fmt.Sprintf("Environment variable: '%s' was not set.", e.NotSet)
  39. }
  40. /*
  41. Write out a blank .env configuration with the the required configuration (uncommented) and the
  42. optional configuration (commented out)
  43. :param path: the path to write the template to
  44. */
  45. func WriteTemplate(wtr io.Writer) error {
  46. outReqArr := make([]string, len(REQUIRED_VARS))
  47. outOptVar := make([]string, len(OPTION_VARS))
  48. i := 0
  49. for k := range REQUIRED_VARS {
  50. outReqArr[i] = k
  51. i++
  52. }
  53. i = 0
  54. for k := range OPTION_VARS {
  55. outOptVar[i] = k
  56. i++
  57. }
  58. sort.Strings(outReqArr)
  59. sort.Strings(outOptVar)
  60. var out string
  61. out = out + "####### Required Configuration #######\n"
  62. for i := range outReqArr {
  63. k := REQUIRED_VARS[outReqArr[i]]
  64. v := outReqArr[i]
  65. fmt.Println(k, v)
  66. out = out + fmt.Sprintf("%s\n%s=\n", v, k)
  67. }
  68. out = out + "\n####### Optional Configuration #######\n"
  69. for i := range outOptVar {
  70. out = out + fmt.Sprintf("# %s\n# %s=\n", OPTION_VARS[outOptVar[i]], outOptVar[i])
  71. }
  72. msg := []byte(out)
  73. _, err := io.Copy(wtr, bytes.NewBuffer(msg))
  74. if err != nil {
  75. return err
  76. }
  77. return nil
  78. }
  79. /*
  80. verify all environment vars passed in are set
  81. :param vars: array of strings to verify
  82. */
  83. func LoadAndVerifyEnv(path string, vars map[string]string) error {
  84. err := godotenv.Load(path)
  85. if err != nil {
  86. return err
  87. }
  88. for k := range vars {
  89. if os.Getenv(k) == "" {
  90. return &EnvNotSet{NotSet: k}
  91. }
  92. }
  93. return nil
  94. }