Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: the github action needs the org's slug #16

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions cmd/list/orgs/orgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,19 @@ import (

var OrgsCmd = &cobra.Command{
Use: "orgs",
Short: "List managed organizations.",
Long: `List managed organizations. This command will list all organizations.\n
found in the state file.`,
Short: "List managed organizations's slugs.",
Long: `This command reads the "providers.hcl" files in the "providers" directory and lists the organization slugs that are managed by the tool.`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("requires the path of the \"organizations\" directory")
return errors.New("requires the path of the \"providers\" directory")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {

orgsDir := args[0]

orgs, err := functions.FindManagedOrgs(orgsDir)
orgs, err := functions.FindManagedOrgSlugs(orgsDir)
if err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
58 changes: 41 additions & 17 deletions internal/pkg/functions/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,59 @@ func findOrgsFromFilenames(hclFiles []string) map[string][]string {
return names
}

// List all of the organizations managed by the tool
func FindManagedOrgs(orgsDir string) ([]string, error) {
// Get the list of the dir names in the directory
dirs, err := os.ReadDir(orgsDir)

// List all of the organizations managed by the tool's slugs
func FindManagedOrgSlugs(orgsDir string) ([]string, error) {

orgFiles, err := findConfigFiles(orgsDir, "providers.hcl")
if err != nil {
log.Fatalf("Error in os.ReadDir: %s", err)
return nil, err
log.Fatalf("Error in findOrgFiles: %s", err)
return make([]string, 0), err
}

// Walk the orgFiles and get all the providers.hcl files
var orgs []string
for _, dir := range dirs {
orgs = append(orgs, dir.Name())
for _, file := range orgFiles {
log.Printf("Working on file: %s\n", file)

hclFile := terragrunt.HCLFile {
Path: file,
}

locals := hclFile.GetLocalsMap()

// If the locals map has an `organization_name` key, then it is an org slug
if locals["organization_name"] != "" {
orgs = append(orgs, locals["organization_name" ])
}
}

return orgs, nil
}

// List all of the relevant configs managed by the tool
// The first parameter is the root directory to search in
// The second parameter is the file name pattern to match
func findConfigFiles(rootDir string, fileNamePattern ...string) ([]string, error) {

// There should be 1 or 0 file name patterns to match
patternString := ""
if len(fileNamePattern) == 1 {
patternString = fileNamePattern[0]
}

// List all of the organizations + repository configs managed by the tool
func findOrgFiles(rootDir string) (map[string][]string, error) {

// Get the list of HCL files in the root directory
// hclFiles, err := config.FindConfigFilesInPath(rootDir, options)
var hclFiles []string
err := filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

// if the file ends with "repositories/terragrunt.hcl", then it is a repository fil
if strings.HasSuffix(path, "repositories/terragrunt.hcl") {
// Find files that match the fileNamePattern. Default to "repositories/terragrunt.hcl"
if patternString == "" {
patternString = "repositories/terragrunt.hcl"
}
if strings.HasSuffix(path, patternString) {
hclFiles = append(hclFiles, path)
}

Expand All @@ -63,14 +85,16 @@ func findOrgFiles(rootDir string) (map[string][]string, error) {
return nil, err
}

orgFiles := findOrgsFromFilenames(hclFiles)
return orgFiles, nil
return hclFiles, nil
}


// List all of the repositories managed by the tool
func FindManagedRepos(reposDir string) (status.OrgSet, error) {
orgFiles, err := findOrgFiles(reposDir)
files, err := findConfigFiles(reposDir)

orgFiles := findOrgsFromFilenames(files)

if err != nil {
log.Fatalf("Error in findOrgFiles: %s", err)
return status.OrgSet{}, err
Expand Down
55 changes: 47 additions & 8 deletions internal/pkg/types/terragrunt/terragrunt.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,38 @@ func getRepositoryMap(repoList []map[string]interface{}) (map[string]status.Repo
return repos, nil
}

func replaceLocals(contents string) string {
// Return the locals block from the HCL file as a slice of string slices
func getLocalsBlock(contents string) [][]string {
// The locals are in the form of locals = { key = value }
// Then, they are referred to as local.key in the configuration
// This function replaces the locals with their values

// Use regex to find the locals block
lre := regexp.MustCompile(`locals\s*{\n*((.*[^}])\n)+}`)
locals := lre.FindString(contents)

if locals == "" {
log.Printf("locals not found")
return contents
fmt.Printf("locals not found")
return make([][]string, 0)
}

// Use regex to find the key-value pairs in the locals block
kvre := regexp.MustCompile(`(.*[^=])=(.*(?:(:?\n.*[^\]])*])*)`)
matches := kvre.FindAllStringSubmatch(locals, -1)
// Use regex to find the key-value pairs in the locals block
kvre := regexp.MustCompile(`(.*[^=])=(.*(?:(:?\n.*[^\]])*])*)`)
matches := kvre.FindAllStringSubmatch(locals, -1)

// Clean up the matches a little
for i, match := range matches {
matches[i][1] = strings.Trim(match[1], " \"")
matches[i][2] = strings.Trim(match[2], " \"")
}

return matches
}


// The locals are in the form of locals = { key = value }
// Then, they are referred to as local.key in the configuration
// This function replaces the locals with their values
func replaceLocals(contents string) string {
matches := getLocalsBlock(contents)

// Replace the locals with their values
for _, match := range matches {
Expand Down Expand Up @@ -186,6 +201,30 @@ func (h *HCLFile) GetInputsFromFile() (status.Inputs, error) {
return inputs, nil
}


// Given the string content of an HCL file, return a map of locals
func (h *HCLFile) GetLocalsMap() map[string]string {

// If the path is not set, return an empty map
if h.Path == "" {
return make(map[string]string)
}

// If the path is set, read the file and return the locals
content, err := afero.ReadFile(fs, h.Path)
if err != nil {
log.Fatalf(`GetLocalsMap: unable to read config file: %s`, h.Path)
return make(map[string]string)
}

macthes := getLocalsBlock(string(content))
locals := make(map[string]string)
for _, match := range macthes {
locals[match[1]] = match[2]
}
return locals
}

func NewTerragruntPlanFile(name string, modulePath string, moduleDir string, outputFilePath string) (*PlanFile, error) {
// If there is a file conflict with the output file, create a new file with a "copy_" prefix
if _, err := fs.Stat(outputFilePath); err == nil {
Expand Down