Skip to content

Commit

Permalink
Merge pull request #64 from gchiesa/62-support-for-automatic-ignore-f…
Browse files Browse the repository at this point in the history
…iles-in-ska-upstream

feat: implement support for automatically add ignorePaths in generated ska-config
  • Loading branch information
gchiesa authored Sep 28, 2024
2 parents 42b18ab + 0f94080 commit a4b5b72
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 23 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
#
Expand Down
14 changes: 13 additions & 1 deletion internal/configuration/localconfigservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...) //nolint:gocritic
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
Expand Down
11 changes: 10 additions & 1 deletion internal/configuration/upstreamconfigservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package processor
package filetreeprocessor

import (
"github.com/apex/log"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package processor
package filetreeprocessor

import (
"bytes"
Expand Down Expand Up @@ -107,20 +107,20 @@ func (tp *FileTreeProcessor) loadMultiparts() error {

// if it's file we copy the file to the destination
if !info.IsDir() {
multipart, err := multipart.NewMultipartFromFile(absPath, relPath)
m, err := multipart.NewMultipartFromFile(absPath, relPath)
if err != nil {
return err
}

if err = multipart.ParseParts(); err != nil { //nolint:gocritic
if err = m.ParseParts(); err != nil { //nolint:gocritic
return err
}
files, err := multipart.PartsToFiles()
files, err := m.PartsToFiles()
if err != nil {
return err
}
logger.WithFields(log.Fields{"parts": files, "multipart": relPath}).Debug("generating Parts from Multipart.")
tp.multiparts = append(tp.multiparts, multipart)
tp.multiparts = append(tp.multiparts, m)
} else {
logger.WithFields(log.Fields{"filePath": relPath}).Debug("skipping because is a directory.")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package processor
package filetreeprocessor

import (
"github.com/apex/log"
Expand Down
59 changes: 59 additions & 0 deletions internal/stringprocessor/stringprocessor.go
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 11 additions & 0 deletions internal/stringprocessor/type.go
Original file line number Diff line number Diff line change
@@ -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
}
21 changes: 15 additions & 6 deletions pkg/skaffolder/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -115,10 +116,18 @@ func (s *SkaCreate) Create() error {
return err
}

// render the ignore entries in the upstream configuration
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
Expand Down
25 changes: 17 additions & 8 deletions pkg/skaffolder/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -113,10 +114,18 @@ func (s *SkaUpdate) Update() error {
return err
}

// render the ignore entries in the upstream configuration
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).
err = localConfig.WithVariables(vars).
WithBlueprintUpstream(blueprintProvider.RemoteURI()).
WithExtendIgnorePaths(localConfig.IgnorePaths()).
WithExtendIgnorePaths(skaConfigIgnorePaths).
WriteConfig(s.BaseURI)
if err != nil {
return err
Expand Down

0 comments on commit a4b5b72

Please sign in to comment.