main.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // This program generates env config scripts from config.Config struct tags for
  2. // unix and windows platforms.
  3. // Usage: go run ./cmd/config_generator [platform] [filename] [value_tag]
  4. // Example: go run ./cmd/config_generator unix settings.basic.env basic
  5. package main
  6. import (
  7. "fmt"
  8. "io"
  9. "os"
  10. "reflect"
  11. "strings"
  12. "github.com/mk6i/open-oscar-server/config"
  13. "github.com/mitchellh/go-wordwrap"
  14. )
  15. var platformKeywords = map[string]struct {
  16. comment string
  17. assignment string
  18. }{
  19. "windows": {
  20. comment: "rem ",
  21. assignment: "set ",
  22. },
  23. "unix": {
  24. comment: "# ",
  25. assignment: "export ",
  26. },
  27. }
  28. func main() {
  29. args := os.Args[1:]
  30. if len(args) < 3 {
  31. fmt.Fprintln(os.Stderr, "usage: go run cmd/config_generator [platform] [filename] [value_tag]")
  32. os.Exit(1)
  33. }
  34. platform := args[0]
  35. filename := args[1]
  36. valueTag := args[2] // e.g., "basic", "ssl"
  37. keywords, ok := platformKeywords[platform]
  38. if !ok {
  39. fmt.Fprintf(os.Stderr, "unable to find platform `%s`\n", platform)
  40. os.Exit(1)
  41. }
  42. fmt.Println("writing to", filename)
  43. f, err := os.Create(filename)
  44. if err != nil {
  45. fmt.Fprintf(os.Stderr, "error creating file: %s\n", err.Error())
  46. os.Exit(1)
  47. }
  48. defer f.Close()
  49. configType := reflect.TypeOf(config.Config{})
  50. for i := 0; i < configType.NumField(); i++ {
  51. field := configType.Field(i)
  52. // Check if field is optional and has empty value
  53. required := field.Tag.Get("required")
  54. val := field.Tag.Get(valueTag) // Use the specified value tag
  55. // Skip optional fields with empty values
  56. if required == "false" && val == "" {
  57. continue
  58. }
  59. comment := field.Tag.Get("description")
  60. if err := writeComment(f, comment, 80, keywords.comment); err != nil {
  61. fmt.Fprintf(os.Stderr, "error writing to file: %s\n", err.Error())
  62. os.Exit(1)
  63. }
  64. varName := field.Tag.Get("envconfig")
  65. if err := writeAssignment(f, keywords.assignment, varName, val); err != nil {
  66. fmt.Fprintf(os.Stderr, "error writing to file: %s\n", err.Error())
  67. os.Exit(1)
  68. }
  69. }
  70. }
  71. func writeComment(w io.Writer, comment string, width uint, keyword string) error {
  72. // adjust wrapping threshold to accommodate comment keyword length
  73. width = width - uint(len(keyword))
  74. comment = wordwrap.WrapString(comment, width)
  75. // prepend lines with comment keyword
  76. comment = strings.ReplaceAll(comment, "\n", fmt.Sprintf("\n%s", keyword))
  77. _, err := fmt.Fprintf(w, "%s%s\n", keyword, comment)
  78. return err
  79. }
  80. func writeAssignment(w io.Writer, keyword string, varName string, val string) error {
  81. _, err := fmt.Fprintf(w, "%s%s=%s\n\n", keyword, varName, val)
  82. return err
  83. }