Skip to content

Commit

Permalink
Update stack processor (#68)
Browse files Browse the repository at this point in the history
* Update stack processor

* Update stack processor

* Update stack processor

* Update stack processor
  • Loading branch information
aknysh authored Nov 2, 2021
1 parent 9d62a1e commit d8f4fcf
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 7 deletions.
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
SHELL := /bin/bash
GOOS=darwin
#GOOS=linux
GOARCH=amd64
VERSION=test2

# List of targets the `readme` target should call before generating the readme
export README_DEPS ?= docs/targets.md
Expand All @@ -8,3 +12,9 @@ export README_DEPS ?= docs/targets.md
## Lint terraform code
lint:
$(SELF) terraform/install terraform/get-modules terraform/get-plugins terraform/lint terraform/validate

build:
env GOOS=${GOOS} GOARCH=${GOARCH} go build -o build/atmos -v -ldflags "-X 'github.com/cloudposse/atmos/cmd.Version=${VERSION}'"

deps:
go mod download
3 changes: 2 additions & 1 deletion internal/config/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"github.com/bmatcuk/doublestar"
g "github.com/cloudposse/atmos/internal/globals"
s "github.com/cloudposse/atmos/pkg/stack"
u "github.com/cloudposse/atmos/pkg/utils"
"github.com/fatih/color"
"os"
Expand Down Expand Up @@ -32,7 +33,7 @@ func findAllStackConfigsInPaths(
}

// Find all matches in the glob
matches, err := doublestar.Glob(pathWithExt)
matches, err := s.GetGlobMatches(pathWithExt)
if err != nil {
return nil, nil, false, err
}
Expand Down
1 change: 0 additions & 1 deletion pkg/component/component_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ func ProcessComponentInStack(component string, stack string) (map[string]interfa
}

if tenantFound == true && environmentFound == true && stageFound == true {
stack = stackName
break
}
}
Expand Down
8 changes: 3 additions & 5 deletions pkg/stack/stack_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package stack

import (
"fmt"
"github.com/bmatcuk/doublestar"
g "github.com/cloudposse/atmos/internal/globals"
c "github.com/cloudposse/atmos/pkg/convert"
m "github.com/cloudposse/atmos/pkg/merge"
"github.com/cloudposse/atmos/pkg/utils"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
"io/ioutil"
"path"
"path/filepath"
"sort"
Expand Down Expand Up @@ -116,12 +114,12 @@ func ProcessYAMLConfigFile(

var configs []map[interface{}]interface{}

stackYamlConfig, err := ioutil.ReadFile(filePath)
stackYamlConfig, err := getFileContent(filePath)
if err != nil {
return nil, nil, err
}

stackMapConfig, err := c.YAMLToMapOfInterfaces(string(stackYamlConfig))
stackMapConfig, err := c.YAMLToMapOfInterfaces(stackYamlConfig)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -151,7 +149,7 @@ func ProcessYAMLConfigFile(
}

// Find all import matches in the glob
importMatches, err := doublestar.Glob(impWithExtPath)
importMatches, err := GetGlobMatches(impWithExtPath)
if err != nil {
return nil, nil, err
}
Expand Down
43 changes: 43 additions & 0 deletions pkg/stack/stack_processor_utils.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package stack

import (
"fmt"
"github.com/bmatcuk/doublestar"
g "github.com/cloudposse/atmos/internal/globals"
"github.com/cloudposse/atmos/pkg/utils"
"io/ioutil"
"os"
"path"
"path/filepath"
"sort"
"strings"
"sync"
)

var (
getFileContentSyncMap = sync.Map{}
getGlobMatchesSyncMap = sync.Map{}
)

// FindComponentStacks finds all infrastructure stack config files where the component or the base component is defined
Expand Down Expand Up @@ -196,3 +205,37 @@ func CreateComponentStackMap(basePath string, filePath string) (map[string]map[s

return componentStackMap, nil
}

// getFileContent tries to read and return the file content from the sync map if it exists in the map,
// otherwise it reads the file, stores its content in the map and returns the content
func getFileContent(filePath string) (string, error) {
existingContent, found := getFileContentSyncMap.Load(filePath)
if found == true && existingContent != nil {
return fmt.Sprintf("%s", existingContent), nil
}

content, err := ioutil.ReadFile(filePath)
if err != nil {
return "", err
}
getFileContentSyncMap.Store(filePath, content)

return string(content), nil
}

// GetGlobMatches tries to read and return the Glob matches content from the sync map if it exists in the map,
// otherwise it finds and returns all files matching the pattern, stores the files in the map and returns the files
func GetGlobMatches(pattern string) ([]string, error) {
existingMatches, found := getGlobMatchesSyncMap.Load(pattern)
if found == true && existingMatches != nil {
return strings.Split(fmt.Sprintf("%s", existingMatches), ","), nil
}

matches, err := doublestar.Glob(pattern)
if err != nil {
return nil, err
}
getGlobMatchesSyncMap.Store(pattern, strings.Join(matches, ","))

return matches, nil
}

0 comments on commit d8f4fcf

Please sign in to comment.