diff --git a/README.md b/README.md index 1997884..7ed42f2 100644 --- a/README.md +++ b/README.md @@ -138,6 +138,13 @@ ignorePaths: - .git - .idea +# list of path (can be templated) that will be pre-populated in the final ska-config +skaConfig: + ignorePaths: + - "idea/*" + - test-file-to-be-ignored-{{.testFileName}}.txt + - "*.ignored" + # this is the section that SKA will consume to generate the input form. # each input supports the following information: # diff --git a/internal/configuration/localconfigservice.go b/internal/configuration/localconfigservice.go index 4379d32..a21f2fd 100644 --- a/internal/configuration/localconfigservice.go +++ b/internal/configuration/localconfigservice.go @@ -78,10 +78,22 @@ func (cs *LocalConfigService) WithExcludeMatchingFiles(ignorePaths []string) *Lo return cs } -func (cs *LocalConfigService) GetIgnorePaths() []string { +func (cs *LocalConfigService) IgnorePaths() []string { return cs.app.Config.IgnorePaths } +func (cs *LocalConfigService) WithIgnorePaths(ignorePaths []string) *LocalConfigService { + cs.app.Config.IgnorePaths = ignorePaths + return cs +} + +func (cs *LocalConfigService) WithExtendIgnorePaths(ignorePaths []string) *LocalConfigService { + newPaths := append(cs.app.Config.IgnorePaths, ignorePaths...) + slices.Sort(newPaths) + cs.app.Config.IgnorePaths = slices.Compact(newPaths) + return cs +} + func (cs *LocalConfigService) WithVariables(variables map[string]interface{}) *LocalConfigService { cs.app.State.Variables = variables return cs diff --git a/internal/configuration/upstreamconfigservice.go b/internal/configuration/upstreamconfigservice.go index f2b3dce..f035bb9 100644 --- a/internal/configuration/upstreamconfigservice.go +++ b/internal/configuration/upstreamconfigservice.go @@ -23,9 +23,14 @@ type UpstreamConfigInput struct { Default string `yaml:"default,omitempty"` } +type SkaConfig struct { + IgnorePaths []string `yaml:"ignorePaths"` +} + type config struct { IgnorePaths []string `yaml:"ignorePaths"` Inputs []UpstreamConfigInput `yaml:"inputs,omitempty"` + SkaConfig SkaConfig `yaml:"skaConfig,omitempty"` } func NewUpstreamConfigService() *UpstreamConfigService { @@ -55,10 +60,14 @@ func (ucs *UpstreamConfigService) LoadFromPath(path string) (*UpstreamConfigServ return ucs, nil } -func (ucs *UpstreamConfigService) GetIgnorePaths() []string { +func (ucs *UpstreamConfigService) UpstreamIgnorePaths() []string { return ucs.config.IgnorePaths } +func (ucs *UpstreamConfigService) SkaConfigIgnorePaths() []string { + return ucs.config.SkaConfig.IgnorePaths +} + func (ucs *UpstreamConfigService) GetInputs() []UpstreamConfigInput { return ucs.config.Inputs } diff --git a/internal/processor/filetreeprocessor.go b/internal/filetreeprocessor/filetreeprocessor.go similarity index 98% rename from internal/processor/filetreeprocessor.go rename to internal/filetreeprocessor/filetreeprocessor.go index 8374b90..e29e27a 100644 --- a/internal/processor/filetreeprocessor.go +++ b/internal/filetreeprocessor/filetreeprocessor.go @@ -1,4 +1,4 @@ -package processor +package filetreeprocessor import ( "github.com/apex/log" diff --git a/internal/processor/internal.go b/internal/filetreeprocessor/internal.go similarity index 99% rename from internal/processor/internal.go rename to internal/filetreeprocessor/internal.go index 9697bc1..112ec11 100644 --- a/internal/processor/internal.go +++ b/internal/filetreeprocessor/internal.go @@ -1,4 +1,4 @@ -package processor +package filetreeprocessor import ( "bytes" diff --git a/internal/processor/type.go b/internal/filetreeprocessor/type.go similarity index 94% rename from internal/processor/type.go rename to internal/filetreeprocessor/type.go index 8c2734b..8416a20 100644 --- a/internal/processor/type.go +++ b/internal/filetreeprocessor/type.go @@ -1,4 +1,4 @@ -package processor +package filetreeprocessor import ( "github.com/apex/log" diff --git a/internal/stringprocessor/stringprocessor.go b/internal/stringprocessor/stringprocessor.go new file mode 100644 index 0000000..a06ac51 --- /dev/null +++ b/internal/stringprocessor/stringprocessor.go @@ -0,0 +1,59 @@ +package stringprocessor + +import ( + "bytes" + "github.com/apex/log" + "github.com/gchiesa/ska/internal/templateprovider" +) + +func NewStringProcessor(options ...func(stringProcessor *StringProcessor)) *StringProcessor { + logCtx := log.WithFields(log.Fields{ + "pkg": "string-processor", + }) + + sp := &StringProcessor{ + template: nil, + log: logCtx, + } + // configure options + for _, opt := range options { + opt(sp) + } + return sp +} + +func WithTemplateService(ts templateprovider.TemplateService) func(sp *StringProcessor) { + return func(sp *StringProcessor) { + sp.template = ts + } +} + +func (sp *StringProcessor) Render(text string, withVariables map[string]interface{}) (string, error) { + logger := sp.log.WithFields(log.Fields{"method": "Render"}) + + if err := sp.template.FromString(text); err != nil { + return "", err + } + + // render the template + buff := bytes.NewBufferString("") + if err := sp.template.Execute(buff, withVariables); err != nil { + if sp.template.IsMissingKeyError(err) { + logger.WithFields(log.Fields{"error": err.Error()}).Errorf("missing variable while rendering string: %v", err) + } + return "", err + } + return buff.String(), nil +} + +func (sp *StringProcessor) RenderSliceOfStrings(text []string, variables map[string]interface{}) ([]string, error) { + var result []string + for _, entry := range text { + renderedEntry, err := sp.Render(entry, variables) + if err != nil { + return nil, err + } + result = append(result, renderedEntry) + } + return result, nil +} diff --git a/internal/stringprocessor/type.go b/internal/stringprocessor/type.go new file mode 100644 index 0000000..e4a4a1d --- /dev/null +++ b/internal/stringprocessor/type.go @@ -0,0 +1,11 @@ +package stringprocessor + +import ( + "github.com/apex/log" + "github.com/gchiesa/ska/internal/templateprovider" +) + +type StringProcessor struct { + template templateprovider.TemplateService + log *log.Entry +} diff --git a/pkg/skaffolder/create.go b/pkg/skaffolder/create.go index d1d0c86..f1c0441 100644 --- a/pkg/skaffolder/create.go +++ b/pkg/skaffolder/create.go @@ -5,7 +5,8 @@ import ( "github.com/apex/log" "github.com/gchiesa/ska/internal/configuration" "github.com/gchiesa/ska/internal/contentprovider" - "github.com/gchiesa/ska/internal/processor" + "github.com/gchiesa/ska/internal/filetreeprocessor" + "github.com/gchiesa/ska/internal/stringprocessor" "github.com/gchiesa/ska/internal/templateprovider" "github.com/gchiesa/ska/internal/tui" ) @@ -79,12 +80,12 @@ func (s *SkaCreate) Create() error { return fmt.Errorf("unknown template engine") } - fileTreeProcessor := processor.NewFileTreeProcessor(blueprintProvider.WorkingDir(), s.DestinationPath, - processor.WithTemplateService(templateService), - processor.WithSourceIgnorePaths(upstreamConfig.GetIgnorePaths()), - processor.WithDestinationIgnorePaths(localConfig.GetIgnorePaths())) + fileTreeProcessor := filetreeprocessor.NewFileTreeProcessor(blueprintProvider.WorkingDir(), s.DestinationPath, + filetreeprocessor.WithTemplateService(templateService), + filetreeprocessor.WithSourceIgnorePaths(upstreamConfig.UpstreamIgnorePaths()), + filetreeprocessor.WithDestinationIgnorePaths(localConfig.IgnorePaths())) - defer func(fileTreeProcessor *processor.FileTreeProcessor) { + defer func(fileTreeProcessor *filetreeprocessor.FileTreeProcessor) { _ = fileTreeProcessor.Cleanup() }(fileTreeProcessor) @@ -115,10 +116,19 @@ func (s *SkaCreate) Create() error { return err } + // render the ignore entries in the upstream configuration + var skaConfigIgnorePaths []string + sp := stringprocessor.NewStringProcessor(stringprocessor.WithTemplateService(templateService)) + skaConfigIgnorePaths, err = sp.RenderSliceOfStrings(upstreamConfig.SkaConfigIgnorePaths(), vars) + if err != nil { + return err + } + // save the config err = localConfig. WithVariables(vars). WithBlueprintUpstream(blueprintProvider.RemoteURI()). + WithIgnorePaths(skaConfigIgnorePaths). WriteConfig(s.DestinationPath) if err != nil { return err diff --git a/pkg/skaffolder/update.go b/pkg/skaffolder/update.go index b6be690..99c7b38 100644 --- a/pkg/skaffolder/update.go +++ b/pkg/skaffolder/update.go @@ -5,7 +5,8 @@ import ( "github.com/apex/log" "github.com/gchiesa/ska/internal/configuration" "github.com/gchiesa/ska/internal/contentprovider" - "github.com/gchiesa/ska/internal/processor" + "github.com/gchiesa/ska/internal/filetreeprocessor" + "github.com/gchiesa/ska/internal/stringprocessor" "github.com/gchiesa/ska/internal/templateprovider" "github.com/gchiesa/ska/internal/tui" ) @@ -70,12 +71,12 @@ func (s *SkaUpdate) Update() error { return fmt.Errorf("unknown template engine") } - fileTreeProcessor := processor.NewFileTreeProcessor(blueprintProvider.WorkingDir(), s.BaseURI, - processor.WithTemplateService(templateService), - processor.WithSourceIgnorePaths(upstreamConfig.GetIgnorePaths()), - processor.WithDestinationIgnorePaths(localConfig.GetIgnorePaths())) + fileTreeProcessor := filetreeprocessor.NewFileTreeProcessor(blueprintProvider.WorkingDir(), s.BaseURI, + filetreeprocessor.WithTemplateService(templateService), + filetreeprocessor.WithSourceIgnorePaths(upstreamConfig.UpstreamIgnorePaths()), + filetreeprocessor.WithDestinationIgnorePaths(localConfig.IgnorePaths())) - defer func(fileTreeProcessor *processor.FileTreeProcessor) { + defer func(fileTreeProcessor *filetreeprocessor.FileTreeProcessor) { _ = fileTreeProcessor.Cleanup() }(fileTreeProcessor) @@ -113,10 +114,20 @@ func (s *SkaUpdate) Update() error { return err } + // render the ignore entries in the upstream configuration + var skaConfigIgnorePaths []string + sp := stringprocessor.NewStringProcessor(stringprocessor.WithTemplateService(templateService)) + skaConfigIgnorePaths, err = sp.RenderSliceOfStrings(upstreamConfig.SkaConfigIgnorePaths(), vars) + if err != nil { + return err + } + // save the config err = localConfig. WithVariables(vars). WithBlueprintUpstream(blueprintProvider.RemoteURI()). + WithExtendIgnorePaths(localConfig.IgnorePaths()). + WithExtendIgnorePaths(skaConfigIgnorePaths). WriteConfig(s.BaseURI) if err != nil { return err