Skip to content

Commit

Permalink
Add atmos CLI config path and atmos base path parameters to the c…
Browse files Browse the repository at this point in the history
…omponent processor (#209)

* Add atmos CLI config path and atmos base path parameters

* Add atmos CLI config path and atmos base path parameters

* Add atmos CLI config path and atmos base path parameters
  • Loading branch information
aknysh committed Oct 3, 2022
1 parent 4be7d76 commit 58f78e9
Show file tree
Hide file tree
Showing 16 changed files with 64 additions and 25 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Expand Up @@ -25,7 +25,7 @@ func init() {
// InitConfig finds and merges CLI configurations in the following order:
// system dir, home dir, current dir, ENV vars, command-line arguments
// Here we need the custom commands from the config
err := c.InitConfig()
err := c.InitConfig(c.ConfigAndStacksInfo{})
if err != nil {
u.PrintErrorToStdErrorAndExit(err)
}
Expand Down
5 changes: 3 additions & 2 deletions internal/exec/aws_eks_update_kubeconfig.go
Expand Up @@ -120,10 +120,12 @@ func ExecuteAwsEksUpdateKubeconfig(kubeconfigContext c.AwsEksUpdateKubeconfigCon

shellCommandWorkingDir := ""

var configAndStacksInfo c.ConfigAndStacksInfo

if !requiredParamsProvided {
// If stack is not provided, calculate the stack name from the context (tenant, environment, stage)
if kubeconfigContext.Stack == "" {
err := c.InitConfig()
err := c.InitConfig(configAndStacksInfo)
if err != nil {
return err
}
Expand All @@ -147,7 +149,6 @@ func ExecuteAwsEksUpdateKubeconfig(kubeconfigContext c.AwsEksUpdateKubeconfigCon
}

var err error
var configAndStacksInfo c.ConfigAndStacksInfo
configAndStacksInfo.ComponentFromArg = kubeconfigContext.Component
configAndStacksInfo.Stack = kubeconfigContext.Stack

Expand Down
2 changes: 1 addition & 1 deletion internal/exec/describe_config.go
Expand Up @@ -16,7 +16,7 @@ func ExecuteDescribeConfig(cmd *cobra.Command, args []string) error {
return err
}

err = c.InitConfig()
err = c.InitConfig(c.ConfigAndStacksInfo{})
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/exec/utils.go
Expand Up @@ -192,7 +192,7 @@ func processArgsConfigAndStacks(componentType string, cmd *cobra.Command, args [
// FindStacksMap processes stack config and returns a map of all stacks
func FindStacksMap(configAndStacksInfo c.ConfigAndStacksInfo, checkStack bool) (map[string]any, error) {
// Process and merge CLI configurations
err := c.InitConfig()
err := c.InitConfig(configAndStacksInfo)
if err != nil {
return nil, err
}
Expand Down
5 changes: 3 additions & 2 deletions internal/exec/validate_stacks.go
Expand Up @@ -13,12 +13,13 @@ import (

// ExecuteValidateStacks executes `validate stacks` command
func ExecuteValidateStacks(cmd *cobra.Command, args []string) error {
err := c.InitConfig()
var configAndStacksInfo c.ConfigAndStacksInfo

err := c.InitConfig(configAndStacksInfo)
if err != nil {
return err
}

var configAndStacksInfo c.ConfigAndStacksInfo
err = c.ProcessConfig(configAndStacksInfo, false)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion internal/exec/vendor_utils.go
Expand Up @@ -28,7 +28,7 @@ const (
func ExecuteVendorCommand(cmd *cobra.Command, args []string, vendorCommand string) error {
// InitConfig finds and merges CLI configurations in the following order:
// system dir, home dir, current dir, ENV vars, command-line arguments
err := c.InitConfig()
err := c.InitConfig(c.ConfigAndStacksInfo{})
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/exec/worflow.go
Expand Up @@ -22,7 +22,7 @@ func ExecuteWorkflow(cmd *cobra.Command, args []string) error {

// InitConfig finds and merges CLI configurations in the following order:
// system dir, home dir, current dir, ENV vars, command-line arguments
err := c.InitConfig()
err := c.InitConfig(c.ConfigAndStacksInfo{})
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/atlantis/atlantis_generate_repo_config_test.go
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestAtlantisGenerateRepoConfig(t *testing.T) {
err := c.InitConfig()
err := c.InitConfig(c.ConfigAndStacksInfo{})
assert.Nil(t, err)

err = utils.PrintAsYAML(c.Config)
Expand Down
2 changes: 1 addition & 1 deletion pkg/aws/aws_eks_update_kubeconfig_test.go
Expand Up @@ -10,7 +10,7 @@ import (

func TestClusterNamePattern(t *testing.T) {
// InitConfig finds and processes `atmos.yaml` CLI config
err := c.InitConfig()
err := c.InitConfig(c.ConfigAndStacksInfo{})
assert.Nil(t, err)

// Define variables for a component in a stack
Expand Down
21 changes: 18 additions & 3 deletions pkg/component/component_processor.go
Expand Up @@ -8,10 +8,18 @@ import (
)

// ProcessComponentInStack accepts a component and a stack name and returns the component configuration in the stack
func ProcessComponentInStack(component string, stack string) (map[string]any, error) {
func ProcessComponentInStack(
component string,
stack string,
atmosCliConfigPath string,
atmosBasePath string,
) (map[string]any, error) {

var configAndStacksInfo c.ConfigAndStacksInfo
configAndStacksInfo.ComponentFromArg = component
configAndStacksInfo.Stack = stack
configAndStacksInfo.AtmosCliConfigPath = atmosCliConfigPath
configAndStacksInfo.AtmosBasePath = atmosBasePath

configAndStacksInfo.ComponentType = "terraform"
configAndStacksInfo, err := e.ProcessStacks(configAndStacksInfo, true)
Expand All @@ -34,9 +42,16 @@ func ProcessComponentFromContext(
tenant string,
environment string,
stage string,
atmosCliConfigPath string,
atmosBasePath string,
) (map[string]any, error) {

err := c.InitConfig()
var configAndStacksInfo c.ConfigAndStacksInfo
configAndStacksInfo.ComponentFromArg = component
configAndStacksInfo.AtmosCliConfigPath = atmosCliConfigPath
configAndStacksInfo.AtmosBasePath = atmosBasePath

err := c.InitConfig(configAndStacksInfo)
if err != nil {
u.PrintErrorToStdError(err)
return nil, err
Expand All @@ -54,5 +69,5 @@ func ProcessComponentFromContext(
return nil, err
}

return ProcessComponentInStack(component, stack)
return ProcessComponentInStack(component, stack, atmosCliConfigPath, atmosBasePath)
}
12 changes: 6 additions & 6 deletions pkg/component/component_processor_test.go
Expand Up @@ -16,7 +16,7 @@ func TestComponentProcessor(t *testing.T) {
var tenant1Ue2DevTestTestComponent map[string]any
component = "test/test-component"
stack = "tenant1-ue2-dev"
tenant1Ue2DevTestTestComponent, err = ProcessComponentInStack(component, stack)
tenant1Ue2DevTestTestComponent, err = ProcessComponentInStack(component, stack, "", "")
assert.Nil(t, err)
tenant1Ue2DevTestTestComponentBackend := tenant1Ue2DevTestTestComponent["backend"].(map[any]any)
tenant1Ue2DevTestTestComponentRemoteStateBackend := tenant1Ue2DevTestTestComponent["remote_state_backend"].(map[any]any)
Expand Down Expand Up @@ -49,7 +49,7 @@ func TestComponentProcessor(t *testing.T) {
tenant := "tenant1"
environment := "ue2"
stage := "dev"
tenant1Ue2DevTestTestComponent2, err = ProcessComponentFromContext(component, namespace, tenant, environment, stage)
tenant1Ue2DevTestTestComponent2, err = ProcessComponentFromContext(component, namespace, tenant, environment, stage, "", "")
assert.Nil(t, err)
tenant1Ue2DevTestTestComponentBackend2 := tenant1Ue2DevTestTestComponent2["backend"].(map[any]any)
tenant1Ue2DevTestTestComponentRemoteStateBackend2 := tenant1Ue2DevTestTestComponent2["remote_state_backend"].(map[any]any)
Expand Down Expand Up @@ -84,7 +84,7 @@ func TestComponentProcessor(t *testing.T) {
var tenant1Ue2DevTestTestComponentOverrideComponent map[string]any
component = "test/test-component-override"
stack = "tenant1-ue2-dev"
tenant1Ue2DevTestTestComponentOverrideComponent, err = ProcessComponentInStack(component, stack)
tenant1Ue2DevTestTestComponentOverrideComponent, err = ProcessComponentInStack(component, stack, "", "")
assert.Nil(t, err)
tenant1Ue2DevTestTestComponentOverrideComponentBackend := tenant1Ue2DevTestTestComponentOverrideComponent["backend"].(map[any]any)
tenant1Ue2DevTestTestComponentOverrideComponentBaseComponent := tenant1Ue2DevTestTestComponentOverrideComponent["component"].(string)
Expand Down Expand Up @@ -119,7 +119,7 @@ func TestComponentProcessor(t *testing.T) {
tenant = "tenant1"
environment = "ue2"
stage = "dev"
tenant1Ue2DevTestTestComponentOverrideComponent2, err = ProcessComponentFromContext(component, namespace, tenant, environment, stage)
tenant1Ue2DevTestTestComponentOverrideComponent2, err = ProcessComponentFromContext(component, namespace, tenant, environment, stage, "", "")
assert.Nil(t, err)
tenant1Ue2DevTestTestComponentOverrideComponent2Backend := tenant1Ue2DevTestTestComponentOverrideComponent2["backend"].(map[any]any)
tenant1Ue2DevTestTestComponentOverrideComponent2Workspace := tenant1Ue2DevTestTestComponentOverrideComponent2["workspace"].(string)
Expand All @@ -137,7 +137,7 @@ func TestComponentProcessor(t *testing.T) {
tenant = "tenant1"
environment = "ue2"
stage = "test-1"
tenant1Ue2Test1TestTestComponentOverrideComponent2, err = ProcessComponentFromContext(component, namespace, tenant, environment, stage)
tenant1Ue2Test1TestTestComponentOverrideComponent2, err = ProcessComponentFromContext(component, namespace, tenant, environment, stage, "", "")
assert.Nil(t, err)
tenant1Ue2Test1TestTestComponentOverrideComponent2Backend := tenant1Ue2DevTestTestComponentOverrideComponent2["backend"].(map[any]any)
tenant1Ue2Test1TestTestComponentOverrideComponent2Workspace := tenant1Ue2Test1TestTestComponentOverrideComponent2["workspace"].(string)
Expand All @@ -148,7 +148,7 @@ func TestComponentProcessor(t *testing.T) {
var tenant1Ue2DevTestTestComponentOverrideComponent3 map[string]any
component = "test/test-component-override-3"
stack = "tenant1-ue2-dev"
tenant1Ue2DevTestTestComponentOverrideComponent3, err = ProcessComponentInStack(component, stack)
tenant1Ue2DevTestTestComponentOverrideComponent3, err = ProcessComponentInStack(component, stack, "", "")
assert.Nil(t, err)
tenant1Ue2DevTestTestComponentOverrideComponent3Deps := tenant1Ue2DevTestTestComponentOverrideComponent3["deps"].([]string)
assert.Equal(t, 20, len(tenant1Ue2DevTestTestComponentOverrideComponent3Deps))
Expand Down
24 changes: 22 additions & 2 deletions pkg/config/config.go
Expand Up @@ -26,7 +26,7 @@ var (
// InitConfig finds and merges CLI configurations in the following order: system dir, home dir, current dir, ENV vars, command-line arguments
// https://dev.to/techschoolguru/load-config-from-file-environment-variables-in-golang-with-viper-2j2d
// https://medium.com/@bnprashanth256/reading-configuration-files-and-environment-variables-in-go-golang-c2607f912b63
func InitConfig() error {
func InitConfig(configAndStacksInfo ConfigAndStacksInfo) error {
// Config is loaded from the following locations (from lower to higher priority):
// system dir (`/usr/local/etc/atmos` on Linux, `%LOCALAPPDATA%/atmos` on Windows)
// home dir (~/.atmos)
Expand Down Expand Up @@ -125,6 +125,21 @@ func InitConfig() error {
}
}

// Process config from the path specified in the Terraform provider (which calls into the atmos code)
if configAndStacksInfo.AtmosCliConfigPath != "" {
configFilePath5 := configAndStacksInfo.AtmosCliConfigPath
if len(configFilePath5) > 0 {
configFile5 := path.Join(configFilePath5, g.ConfigFileName)
found, err = processConfigFile(configFile5, v)
if err != nil {
return err
}
if found {
configFound = true
}
}
}

if !configFound {
return errors.New("\n'atmos.yaml' CLI config files not found in any of the searched paths: system dir, home dir, current dir, ENV vars." +
"\nYou can download a sample config and adapt it to your requirements from " +
Expand All @@ -138,6 +153,11 @@ func InitConfig() error {
return err
}

// Process the base path specified in the Terraform provider (which calls into the atmos code)
if configAndStacksInfo.AtmosBasePath != "" {
Config.BasePath = configAndStacksInfo.AtmosBasePath
}

Config.Initialized = true
return nil
}
Expand Down Expand Up @@ -331,7 +351,7 @@ func ProcessConfigForSpacelift() error {
// https://medium.com/@bnprashanth256/reading-configuration-files-and-environment-variables-in-go-golang-c2607f912b63
func processConfigFile(path string, v *viper.Viper) (bool, error) {
if !u.FileExists(path) {
u.PrintInfoVerbose(fmt.Sprintf("No config file atmos.yaml found in path '%s'.", path))
u.PrintInfoVerbose(fmt.Sprintf("No config file 'atmos.yaml' found in path '%s'.", path))
return false, nil
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/config/schema.go
Expand Up @@ -140,6 +140,8 @@ type ConfigAndStacksInfo struct {
JsonSchemaDir string
OpaDir string
CueDir string
AtmosCliConfigPath string
AtmosBasePath string
}

type WorkflowStep struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/generate/terraform_generate_varfiles_test.go
Expand Up @@ -12,7 +12,7 @@ import (
)

func TestTerraformGenerateVarfiles(t *testing.T) {
err := c.InitConfig()
err := c.InitConfig(c.ConfigAndStacksInfo{})
assert.Nil(t, err)

tempDir, err := os.MkdirTemp("", strconv.FormatInt(time.Now().Unix(), 10))
Expand Down
2 changes: 1 addition & 1 deletion pkg/spacelift/spacelift_stack_processor.go
Expand Up @@ -39,7 +39,7 @@ func CreateSpaceliftStacks(

return TransformStackConfigToSpaceliftStacks(stacks, stackConfigPathTemplate, "", processImports)
} else {
err := c.InitConfig()
err := c.InitConfig(c.ConfigAndStacksInfo{})
if err != nil {
u.PrintErrorToStdError(err)
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/vender/component_vendor_test.go
Expand Up @@ -10,7 +10,7 @@ import (
)

func TestVendorComponentPullCommand(t *testing.T) {
err := c.InitConfig()
err := c.InitConfig(c.ConfigAndStacksInfo{})
assert.Nil(t, err)

componentType := "terraform"
Expand Down

0 comments on commit 58f78e9

Please sign in to comment.