@@ -34,6 +34,7 @@ import (
3434 "github.com/spf13/pflag"
3535 "k8s.io/apimachinery/pkg/api/meta"
3636 "k8s.io/apimachinery/pkg/runtime/schema"
37+ "sigs.k8s.io/yaml"
3738)
3839
3940type Config struct {
@@ -55,13 +56,25 @@ type Config struct {
5556 Image string `json:"image,omitempty"`
5657}
5758
58- func (c Config ) StringMap () map [ string ] any {
59+ func (c Config ) ToJson () [] byte {
5960 raw , err := json .Marshal (c )
6061 if err != nil {
6162 panic (err )
6263 }
64+ return raw
65+ }
66+
67+ func (c Config ) ToYaml () []byte {
68+ raw , err := yaml .Marshal (c )
69+ if err != nil {
70+ panic (err )
71+ }
72+ return raw
73+ }
74+
75+ func (c Config ) ToStringMap () map [string ]any {
6376 var result map [string ]any
64- if err := json .Unmarshal (raw , & result ); err != nil {
77+ if err := json .Unmarshal (c . ToJson () , & result ); err != nil {
6578 panic (err )
6679 }
6780 return result
@@ -136,6 +149,10 @@ func main() {
136149 errlog .Fatal (err )
137150 }
138151
152+ if err := createProjectFile (& config , outputDir ); err != nil {
153+ errlog .Fatal (err )
154+ }
155+
139156 if err := processTemplates (subFS (templates , "templates" ), & config , outputDir ); err != nil {
140157 errlog .Fatal (err )
141158 }
@@ -224,14 +241,28 @@ func validateAndDefaultConfig(config *Config) error {
224241 return nil
225242}
226243
244+ func createProjectFile (config * Config , outputDir string ) error {
245+ projectFile , err := os .OpenFile (outputDir + "/.project" , os .O_RDWR | os .O_CREATE | os .O_EXCL , 0644 )
246+ if err != nil {
247+ return err
248+ }
249+ if _ , err := projectFile .Write (config .ToYaml ()); err != nil {
250+ return err
251+ }
252+ if err := projectFile .Close (); err != nil {
253+ return err
254+ }
255+ return nil
256+ }
257+
227258func processTemplates (fsys FS , config * Config , outputDir string ) error {
228259 entries , err := fsys .ReadDir ("." )
229260 if err != nil {
230261 return err
231262 }
232263 for _ , entry := range entries {
233264 path := entry .Name ()
234- outputPath := outputDir + "/" + substitutePath (path , config .StringMap ())
265+ outputPath := outputDir + "/" + substitutePath (path , config .ToStringMap ())
235266 if err != nil {
236267 return err
237268 }
@@ -246,7 +277,7 @@ func processTemplates(fsys FS, config *Config, outputDir string) error {
246277 if err != nil {
247278 return err
248279 }
249- output , err = renderTemplate (path , tpl , config .StringMap ())
280+ output , err = renderTemplate (path , tpl , config .ToStringMap ())
250281 if err != nil {
251282 return err
252283 }
0 commit comments