|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "bufio" |
5 | | - "fmt" |
6 | | - "io/ioutil" |
7 | | - "os" |
8 | | - "regexp" |
9 | | - "strings" |
10 | | - |
11 | | - "gopkg.in/yaml.v3" |
| 4 | + "bufio" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "os" |
| 8 | + "regexp" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "gopkg.in/yaml.v3" |
12 | 12 | ) |
13 | 13 |
|
14 | 14 | var version = "dev" |
15 | 15 |
|
16 | 16 | func main() { |
17 | 17 |
|
18 | | - if os.Args[1] == "-v" { |
19 | | - fmt.Println("Jinjafier version:", version) |
20 | | - os.Exit(0) |
| 18 | + if len(os.Args) == 2 && os.Args[1] == "-v" { |
| 19 | + fmt.Println("Jinjafier version:", version) |
| 20 | + os.Exit(0) |
| 21 | + } |
| 22 | + |
| 23 | + if len(os.Args) != 2 { |
| 24 | + fmt.Println("Usage: jinjafier <java_properties_file>") |
| 25 | + os.Exit(1) |
| 26 | + } |
| 27 | + |
| 28 | + filename := os.Args[1] |
| 29 | + file, err := os.Open(filename) |
| 30 | + if err != nil { |
| 31 | + fmt.Println(err) |
| 32 | + os.Exit(1) |
| 33 | + } |
| 34 | + defer file.Close() |
| 35 | + |
| 36 | + scanner := bufio.NewScanner(file) |
| 37 | + jinjaTemplate := "" |
| 38 | + envTemplate := "" |
| 39 | + yamlMap := make(map[string]string) |
| 40 | + |
| 41 | + for scanner.Scan() { |
| 42 | + line := scanner.Text() |
| 43 | + if strings.HasPrefix(line, "#") { |
| 44 | + // Add comments to both templates |
| 45 | + jinjaTemplate += line + "\n" |
| 46 | + envTemplate += line + "\n" |
| 47 | + } else if strings.Contains(line, "=") { |
| 48 | + split := strings.SplitN(line, "=", 2) // Split at the first "=" only |
| 49 | + key := split[0] |
| 50 | + value := split[1] |
| 51 | + |
| 52 | + // Convert key to lowercase and replace dots and camel case with underscores |
| 53 | + re := regexp.MustCompile("([a-z0-9])([A-Z])") |
| 54 | + key = re.ReplaceAllString(key, "${1}_${2}") |
| 55 | + key = strings.ReplaceAll(key, ".", "_") |
| 56 | + key = strings.ReplaceAll(key, "-", "_") |
| 57 | + key = strings.ToUpper(key) |
| 58 | + |
| 59 | + // Add to jinja template |
| 60 | + jinjaTemplate += fmt.Sprintf("%s={{ %s }}\n", split[0], key) |
| 61 | + |
| 62 | + // Add to env template |
| 63 | + envTemplate += fmt.Sprintf("%s={{ %s }}\n", key, key) |
| 64 | + |
| 65 | + // Add to yaml map |
| 66 | + yamlMap[key] = value |
| 67 | + } else { |
| 68 | + // Add non-comment, non-key-value lines to the Jinja template |
| 69 | + jinjaTemplate += line + "\n" |
21 | 70 | } |
22 | | - |
23 | | - if len(os.Args) != 2 { |
24 | | - fmt.Println("Usage: jinjafier <java_properties_file>") |
25 | | - os.Exit(1) |
26 | | - } |
27 | | - |
28 | | - filename := os.Args[1] |
29 | | - file, err := os.Open(filename) |
30 | | - if err != nil { |
31 | | - fmt.Println(err) |
32 | | - os.Exit(1) |
33 | | - } |
34 | | - defer file.Close() |
35 | | - |
36 | | - scanner := bufio.NewScanner(file) |
37 | | - jinjaTemplate := "" |
38 | | - yamlMap := make(map[string]string) |
39 | | - |
40 | | - for scanner.Scan() { |
41 | | - line := scanner.Text() |
42 | | - if strings.HasPrefix(line, "#") { |
43 | | - jinjaTemplate += line + "\n" |
44 | | - } else if strings.Contains(line, "=") { |
45 | | - split := strings.SplitN(line, "=", 2) // Split at the first "=" only |
46 | | - key := split[0] |
47 | | - value := split[1] |
48 | | - |
49 | | - // Convert key to lowercase and replace dots and camel case with underscores |
50 | | - re := regexp.MustCompile("([a-z0-9])([A-Z])") |
51 | | - key = re.ReplaceAllString(key, "${1}_${2}") |
52 | | - key = strings.ReplaceAll(key, ".", "_") |
53 | | - key = strings.ReplaceAll(key, "-", "_") |
54 | | - key = strings.ToUpper(key) |
55 | | - |
56 | | - // Add to jinja template |
57 | | - jinjaTemplate += fmt.Sprintf("%s={{ %s }}\n", split[0], key) |
58 | | - |
59 | | - // Add to yaml map |
60 | | - yamlMap[key] = value |
61 | | - } else { |
62 | | - jinjaTemplate += line + "\n" |
63 | | - } |
64 | | - } |
65 | | - |
66 | | - // Write jinja template to file |
67 | | - err = ioutil.WriteFile(strings.ReplaceAll(filename, ".properties", ".properties.j2"), []byte(jinjaTemplate), 0644) |
68 | | - if err != nil { |
69 | | - fmt.Println(err) |
70 | | - os.Exit(1) |
71 | | - } |
72 | | - |
73 | | - // Convert yaml map to yaml |
74 | | - yamlData, err := yaml.Marshal(&yamlMap) |
75 | | - if err != nil { |
76 | | - fmt.Println(err) |
77 | | - os.Exit(1) |
78 | | - } |
79 | | - |
80 | | - // Write yaml to file |
81 | | - err = ioutil.WriteFile(strings.ReplaceAll(filename, ".properties", ".properties.yml"), yamlData, 0644) |
82 | | - if err != nil { |
83 | | - fmt.Println(err) |
84 | | - os.Exit(1) |
85 | | - } |
| 71 | + } |
| 72 | + |
| 73 | + // Write jinja template to file |
| 74 | + err = ioutil.WriteFile(strings.ReplaceAll(filename, ".properties", ".properties.j2"), []byte(jinjaTemplate), 0644) |
| 75 | + if err != nil { |
| 76 | + fmt.Println(err) |
| 77 | + os.Exit(1) |
| 78 | + } |
| 79 | + |
| 80 | + // Write env template to file |
| 81 | + err = ioutil.WriteFile(strings.ReplaceAll(filename, ".properties", ".properties.env.j2"), []byte(envTemplate), 0644) |
| 82 | + if err != nil { |
| 83 | + fmt.Println(err) |
| 84 | + os.Exit(1) |
| 85 | + } |
| 86 | + |
| 87 | + // Convert yaml map to yaml |
| 88 | + yamlData, err := yaml.Marshal(&yamlMap) |
| 89 | + if err != nil { |
| 90 | + fmt.Println(err) |
| 91 | + os.Exit(1) |
| 92 | + } |
| 93 | + |
| 94 | + // Write yaml to file |
| 95 | + err = ioutil.WriteFile(strings.ReplaceAll(filename, ".properties", ".properties.yml"), yamlData, 0644) |
| 96 | + if err != nil { |
| 97 | + fmt.Println(err) |
| 98 | + os.Exit(1) |
| 99 | + } |
86 | 100 | } |
87 | | - |
|
0 commit comments