Skip to content

Commit

Permalink
address changes
Browse files Browse the repository at this point in the history
  • Loading branch information
thesayyn committed May 16, 2023
1 parent d74130d commit eb595d9
Showing 1 changed file with 31 additions and 26 deletions.
57 changes: 31 additions & 26 deletions tools/expand_template/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,6 @@ import (
"golang.org/x/exp/maps"
)

var STATUS_REGEX = regexp.MustCompile(`(?m)^([^\s]+)\s+([^\n ]*)$`)

func parseStatusFile(statusFilePath string) (map[string]string, error) {
statusFile, err := os.ReadFile(statusFilePath)
if err != nil {
return nil, err
}

matches := STATUS_REGEX.FindAllStringSubmatch(string(statusFile), -1)

results := map[string]string{}

for _, match := range matches {
results[match[1]] = match[2]
}

return results, nil

}

func main() {
args := os.Args[1:]

Expand All @@ -52,19 +32,19 @@ func main() {
substitutions := map[string]string{}
substitutionJson, err := os.ReadFile(args[2])
if err != nil {
log.Fatal(err)
log.Fatal(fmt.Errorf("failed to read substitutions file: %w", err))
}
if err := json.Unmarshal(substitutionJson, &substitutions); err != nil {
log.Fatal(err)
log.Fatal(fmt.Errorf("failed to parse substitutions file: %w", err))
}

volatileStatus, err := parseStatusFile(args[3])
if err != nil {
log.Fatal(err)
log.Fatal(fmt.Errorf("failed to parse volatile status file: %w", err))
}
stableStatus, err := parseStatusFile(args[4])
if err != nil {
log.Fatal(err)
log.Fatal(fmt.Errorf("failed to parse stable status file: %w", err))
}

statuses := map[string]string{}
Expand All @@ -80,7 +60,7 @@ func main() {

contentBytes, err := os.ReadFile(args[0])
if err != nil {
log.Fatal(err)
log.Fatal(fmt.Errorf("failed to read the template file: %w", err))
}
content := string(contentBytes)

Expand All @@ -92,5 +72,30 @@ func main() {
if executable {
mode = 0o777
}
os.WriteFile(args[1], []byte(content), mode)
err = os.WriteFile(args[1], []byte(content), mode)
if err != nil {
log.Fatal(fmt.Errorf("failed to write output file: %w", err))
}
}

// captures every `KEY VALUE` line in the status file.
// for explanation see: https://regex101.com/r/B28NN7/1
var STATUS_REGEX = regexp.MustCompile(`(?m)^([^\s]+)\s+([^\n ]*)$`)

func parseStatusFile(statusFilePath string) (map[string]string, error) {
statusFile, err := os.ReadFile(statusFilePath)
if err != nil {
return nil, err
}

matches := STATUS_REGEX.FindAllStringSubmatch(string(statusFile), -1)

results := map[string]string{}

for _, match := range matches {
results[match[1]] = match[2]
}

return results, nil

}

0 comments on commit eb595d9

Please sign in to comment.