|
1 | 1 | package deploymentrepo |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + _ "embed" |
4 | 5 | "fmt" |
5 | 6 | "os" |
6 | 7 | "path/filepath" |
@@ -65,7 +66,7 @@ func TemplateDir(templateDirectory string, repo *git.Repository) error { |
65 | 66 | return fmt.Errorf("failed to execute template %s: %w", relativePath, err) |
66 | 67 | } |
67 | 68 |
|
68 | | - fileInWorkTree, errInWalk = workTree.Filesystem.OpenFile(relativePath, os.O_WRONLY|os.O_CREATE, 0644) |
| 69 | + fileInWorkTree, errInWalk = workTree.Filesystem.OpenFile(relativePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) |
69 | 70 | if errInWalk != nil { |
70 | 71 | return fmt.Errorf("failed to open file in worktree %s: %w", relativePath, err) |
71 | 72 | } |
@@ -95,3 +96,76 @@ func TemplateDir(templateDirectory string, repo *git.Repository) error { |
95 | 96 |
|
96 | 97 | return nil |
97 | 98 | } |
| 99 | + |
| 100 | +var ( |
| 101 | + //go:embed templates/clusterProvider.yaml |
| 102 | + clusterProviderTemplate string |
| 103 | + //go:embed templates/serviceProvider.yaml |
| 104 | + serviceProviderTemplate string |
| 105 | + //go:embed templates/platformService.yaml |
| 106 | + platformServiceTemplate string |
| 107 | +) |
| 108 | + |
| 109 | +func TemplateClusterProvider(name, image string, repo *git.Repository) error { |
| 110 | + return templateProvider(name, image, clusterProviderTemplate, "cluster-providers", repo) |
| 111 | +} |
| 112 | + |
| 113 | +func TemplateServiceProvider(name, image string, repo *git.Repository) error { |
| 114 | + return templateProvider(name, image, serviceProviderTemplate, "service-providers", repo) |
| 115 | +} |
| 116 | + |
| 117 | +func TemplatePlatformService(name, image string, repo *git.Repository) error { |
| 118 | + return templateProvider(name, image, platformServiceTemplate, "platform-services", repo) |
| 119 | +} |
| 120 | + |
| 121 | +func templateProvider(name, image, templateSource, dir string, repo *git.Repository) error { |
| 122 | + logger := log.GetLogger() |
| 123 | + providerPath := filepath.Join(dir, name+".yaml") |
| 124 | + |
| 125 | + logger.Debugf("Creating provider %s with image %s in path %s", name, image, providerPath) |
| 126 | + |
| 127 | + te := template.NewTemplateExecution() |
| 128 | + templateInput := map[string]interface{}{ |
| 129 | + "values": map[string]interface{}{ |
| 130 | + "name": name, |
| 131 | + "image": map[string]interface{}{ |
| 132 | + "location": image, |
| 133 | + }, |
| 134 | + }, |
| 135 | + } |
| 136 | + |
| 137 | + workTree, err := repo.Worktree() |
| 138 | + if err != nil { |
| 139 | + return fmt.Errorf("failed to get worktree: %w", err) |
| 140 | + } |
| 141 | + |
| 142 | + logger.Tracef("Template input: %v", templateInput) |
| 143 | + |
| 144 | + fileInWorkTree, err := workTree.Filesystem.OpenFile(providerPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) |
| 145 | + if err != nil { |
| 146 | + return fmt.Errorf("failed to open file %s in worktree: %w", providerPath, err) |
| 147 | + } |
| 148 | + |
| 149 | + defer func(pathInRepo billy.File) { |
| 150 | + err := pathInRepo.Close() |
| 151 | + if err != nil { |
| 152 | + _, _ = fmt.Fprintf(os.Stderr, "failed to close file in worktree: %v\n", err) |
| 153 | + } |
| 154 | + }(fileInWorkTree) |
| 155 | + |
| 156 | + templateResult, err := te.Execute(providerPath, templateSource, templateInput) |
| 157 | + if err != nil { |
| 158 | + return fmt.Errorf("failed to execute templateSource for cluster provider %s: %w", name, err) |
| 159 | + } |
| 160 | + |
| 161 | + _, err = fileInWorkTree.Write(templateResult) |
| 162 | + if err != nil { |
| 163 | + return fmt.Errorf("failed to write to file %s in worktree: %w", providerPath, err) |
| 164 | + } |
| 165 | + |
| 166 | + if _, err = workTree.Add(providerPath); err != nil { |
| 167 | + return fmt.Errorf("failed to add provider %s file to git index: %w", providerPath, err) |
| 168 | + } |
| 169 | + |
| 170 | + return nil |
| 171 | +} |
0 commit comments