Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
atmos describe stacks
command (#133)
* Add `describe stacks` command * Add `describe stacks` command * Add `describe stacks` command * Add `describe stacks` command * Add `describe stacks` command * Add `describe stacks` command * Add `describe stacks` command * Add `describe stacks` command * Add `describe stacks` command * Add `describe stacks` command * Add `describe stacks` command * Add `describe stacks` command * Add `describe stacks` command * Add `describe stacks` command * Add `describe stacks` command * Add `describe stacks` command * Add `describe stacks` command * Add `describe stacks` command * Add `describe stacks` command
- Loading branch information
Showing
9 changed files
with
262 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package cmd | ||
|
||
import ( | ||
e "github.com/cloudposse/atmos/internal/exec" | ||
u "github.com/cloudposse/atmos/pkg/utils" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// describeComponentCmd describes configuration for components | ||
var describeStacksCmd = &cobra.Command{ | ||
Use: "stacks", | ||
Short: "Execute 'describe stacks' command", | ||
Long: `This command shows configuration for stacks and components in the stacks: atmos describe stacks <options>`, | ||
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: true}, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
err := e.ExecuteDescribeStacks(cmd, args) | ||
if err != nil { | ||
u.PrintErrorToStdErrorAndExit(err) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
describeStacksCmd.DisableFlagParsing = false | ||
describeStacksCmd.PersistentFlags().String("file", "", "Write the result to file: atmos describe stacks --file=stacks.yaml") | ||
describeStacksCmd.PersistentFlags().String("format", "yaml", "Specify output format: atmos describe stacks --format=yaml/json ('yaml' is default)") | ||
describeStacksCmd.PersistentFlags().StringP("stack", "s", "", "Filter by a specific stack: atmos describe stacks -s <stack>") | ||
describeStacksCmd.PersistentFlags().String("components", "", "Filter by specific components: atmos describe stacks --components=<component1>,<component2>") | ||
describeStacksCmd.PersistentFlags().String("component-types", "", "Filter by specific component types: atmos describe stacks --component-types=terraform,helmfile. Supported component types: terraform, helmfile") | ||
describeStacksCmd.PersistentFlags().String("sections", "", "Output only these component sections: atmos describe stacks --sections=vars,settings. Available component sections: backend, backend_type, deps, env, inheritance, metadata, remote_state_backend, remote_state_backend_type, settings, vars") | ||
|
||
describeCmd.AddCommand(describeStacksCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
package exec | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
c "github.com/cloudposse/atmos/pkg/config" | ||
u "github.com/cloudposse/atmos/pkg/utils" | ||
"github.com/spf13/cobra" | ||
"strings" | ||
) | ||
|
||
// ExecuteDescribeStacks executes `describe stacks` command | ||
func ExecuteDescribeStacks(cmd *cobra.Command, args []string) error { | ||
flags := cmd.Flags() | ||
|
||
filterByStack, err := flags.GetString("stack") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
format, err := flags.GetString("format") | ||
if err != nil { | ||
return err | ||
} | ||
if format != "" && format != "yaml" && format != "json" { | ||
return errors.New(fmt.Sprintf("Invalid '--format' flag '%s'. Valid values are 'yaml' (default) and 'json'", format)) | ||
} | ||
if format == "" { | ||
format = "yaml" | ||
} | ||
|
||
file, err := flags.GetString("file") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
componentsCsv, err := flags.GetString("components") | ||
if err != nil { | ||
return err | ||
} | ||
var components []string | ||
if componentsCsv != "" { | ||
components = strings.Split(componentsCsv, ",") | ||
} | ||
|
||
componentTypesCsv, err := flags.GetString("component-types") | ||
if err != nil { | ||
return err | ||
} | ||
var componentTypes []string | ||
if componentTypesCsv != "" { | ||
componentTypes = strings.Split(componentTypesCsv, ",") | ||
} | ||
|
||
sectionsCsv, err := flags.GetString("sections") | ||
if err != nil { | ||
return err | ||
} | ||
var sections []string | ||
if sectionsCsv != "" { | ||
sections = strings.Split(sectionsCsv, ",") | ||
} | ||
|
||
var configAndStacksInfo c.ConfigAndStacksInfo | ||
configAndStacksInfo.Stack = filterByStack | ||
stacksMap, err := FindStacksMap(configAndStacksInfo, filterByStack != "") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
finalStacksMap := make(map[string]interface{}) | ||
|
||
for stackName, stackSection := range stacksMap { | ||
if filterByStack == "" || filterByStack == stackName { | ||
// Delete the stack-wide imports | ||
delete(stackSection.(map[interface{}]interface{}), "imports") | ||
|
||
if !u.MapKeyExists(finalStacksMap, stackName) { | ||
finalStacksMap[stackName] = make(map[string]interface{}) | ||
} | ||
|
||
if componentsSection, ok := stackSection.(map[interface{}]interface{})["components"].(map[string]interface{}); ok { | ||
if len(componentTypes) == 0 || u.SliceContainsString(componentTypes, "terraform") { | ||
if terraformSection, ok2 := componentsSection["terraform"].(map[string]interface{}); ok2 { | ||
for compName, comp := range terraformSection { | ||
if len(components) == 0 || u.SliceContainsString(components, compName) { | ||
if !u.MapKeyExists(finalStacksMap[stackName].(map[string]interface{}), "components") { | ||
finalStacksMap[stackName].(map[string]interface{})["components"] = make(map[string]interface{}) | ||
} | ||
if !u.MapKeyExists(finalStacksMap[stackName].(map[string]interface{})["components"].(map[string]interface{}), "terraform") { | ||
finalStacksMap[stackName].(map[string]interface{})["components"].(map[string]interface{})["terraform"] = make(map[string]interface{}) | ||
} | ||
if !u.MapKeyExists(finalStacksMap[stackName].(map[string]interface{})["components"].(map[string]interface{})["terraform"].(map[string]interface{}), compName) { | ||
finalStacksMap[stackName].(map[string]interface{})["components"].(map[string]interface{})["terraform"].(map[string]interface{})[compName] = make(map[string]interface{}) | ||
} | ||
|
||
for sectionName, section := range comp.(map[string]interface{}) { | ||
if len(sections) == 0 || u.SliceContainsString(sections, sectionName) { | ||
finalStacksMap[stackName].(map[string]interface{})["components"].(map[string]interface{})["terraform"].(map[string]interface{})[compName].(map[string]interface{})[sectionName] = section | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
if len(componentTypes) == 0 || u.SliceContainsString(componentTypes, "helmfile") { | ||
if helmfileSection, ok3 := componentsSection["helmfile"].(map[string]interface{}); ok3 { | ||
for compName, comp := range helmfileSection { | ||
if len(components) == 0 || u.SliceContainsString(components, compName) { | ||
if !u.MapKeyExists(finalStacksMap[stackName].(map[string]interface{}), "components") { | ||
finalStacksMap[stackName].(map[string]interface{})["components"] = make(map[string]interface{}) | ||
} | ||
if !u.MapKeyExists(finalStacksMap[stackName].(map[string]interface{})["components"].(map[string]interface{}), "helmfile") { | ||
finalStacksMap[stackName].(map[string]interface{})["components"].(map[string]interface{})["helmfile"] = make(map[string]interface{}) | ||
} | ||
if !u.MapKeyExists(finalStacksMap[stackName].(map[string]interface{})["components"].(map[string]interface{})["helmfile"].(map[string]interface{}), compName) { | ||
finalStacksMap[stackName].(map[string]interface{})["components"].(map[string]interface{})["helmfile"].(map[string]interface{})[compName] = make(map[string]interface{}) | ||
} | ||
|
||
for sectionName, section := range comp.(map[string]interface{}) { | ||
if len(sections) == 0 || u.SliceContainsString(sections, sectionName) { | ||
finalStacksMap[stackName].(map[string]interface{})["components"].(map[string]interface{})["helmfile"].(map[string]interface{})[compName].(map[string]interface{})[sectionName] = section | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
if format == "yaml" { | ||
if file == "" { | ||
err = u.PrintAsYAML(finalStacksMap) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
err = u.WriteToFileAsYAML(file, finalStacksMap, 0644) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} else if format == "json" { | ||
if file == "" { | ||
err = u.PrintAsJSON(finalStacksMap) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
err = u.WriteToFileAsJSON(file, finalStacksMap, 0644) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.