Skip to content

Commit

Permalink
cdep: add terraform workspace types (#771)
Browse files Browse the repository at this point in the history
  • Loading branch information
julesjcraske committed Apr 10, 2024
1 parent 1eb088b commit d111576
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 12 deletions.
5 changes: 2 additions & 3 deletions tools/cdep/app/publish_slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import (
"os/exec"
"strings"

"github.com/cuvva/cuvva-public-go/tools/cdep/parsers"

"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/sns"
"github.com/cuvva/cuvva-public-go/tools/cdep/parsers"
)

func (a App) PublishToSlack(ctx context.Context, req *parsers.Params, commitMessage string, updatedFiles []string, repoPath string) error {
user, err := exec.CommandContext(ctx, "git", "config", "user.name").Output()
user, err := exec.CommandContext(ctx, "git", "config", "user.name").CombinedOutput()
if err != nil {
fmt.Println(string(user))
return err
Expand Down
32 changes: 27 additions & 5 deletions tools/cdep/app/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (a App) Update(ctx context.Context, req *parsers.Params, overruleChecks []s

log.Info("fetching config repo")

if out, err := exec.CommandContext(ctx, "git", "-C", repoPath, "fetch", "--all").Output(); err != nil {
if out, err := exec.CommandContext(ctx, "git", "-C", repoPath, "fetch", "--all").CombinedOutput(); err != nil {
fmt.Println(string(out))
return err
}
Expand All @@ -69,14 +69,18 @@ func (a App) Update(ctx context.Context, req *parsers.Params, overruleChecks []s

defaultRef := fmt.Sprintf("refs/heads/%s", cdep.DefaultBranch)
if ref.Name().String() != defaultRef {
return cher.New("config_not_on_master", nil)
if !slicecontains.String(overruleChecks, "config_not_on_master") {
return cher.New("config_not_on_master", nil)
}

log.Warn("config_not_on_master overruled")
}

log.Info("pulling config repo from remote")

if out, err := exec.CommandContext(ctx, "git", "-C", repoPath, "pull", "origin", cdep.DefaultBranch).Output(); err != nil {
if out, err := exec.CommandContext(ctx, "git", "-C", repoPath, "pull").CombinedOutput(); err != nil {
fmt.Println(string(out))
return err
return fmt.Errorf("git pull: %w", err)
}

wt, err := configRepo.Worktree()
Expand Down Expand Up @@ -152,6 +156,24 @@ func (a App) Update(ctx context.Context, req *parsers.Params, overruleChecks []s
updatedFiles = append(updatedFiles, shorthandPath)
}
}
case "terra": // terraform
for _, workspace := range req.Items {
p := paths.GetPathForTerra(repoPath, req.System, env, workspace)

if _, err := os.Stat(p); err != nil {
log.Warn(err)
}

changed, err := a.AddToConfig(p, req.Branch, latestHash)
if err != nil {
return err
}

if changed {
shorthandPath := path.Join(req.System, env, "terra", workspace+".json")
updatedFiles = append(updatedFiles, shorthandPath)
}
}
default:
return cher.New("unexpected_type", cher.M{"type": req.Type})
}
Expand Down Expand Up @@ -188,7 +210,7 @@ func (a App) Update(ctx context.Context, req *parsers.Params, overruleChecks []s

log.Info("pushing commit to config repo")

if out, err := exec.CommandContext(ctx, "git", "-C", repoPath, "push", "origin", cdep.DefaultBranch).Output(); err != nil {
if out, err := exec.CommandContext(ctx, "git", "-C", repoPath, "push", "origin", "HEAD").CombinedOutput(); err != nil {
fmt.Println(string(out))
return fmt.Errorf("config git push: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions tools/cdep/app/update_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (a App) UpdateDefault(ctx context.Context, req *parsers.Params, overruleChe

log.Info("fetching config repo")

if out, err := exec.CommandContext(ctx, "git", "-C", repoPath, "fetch", "--all").Output(); err != nil {
if out, err := exec.CommandContext(ctx, "git", "-C", repoPath, "fetch", "--all").CombinedOutput(); err != nil {
fmt.Println(string(out))
return err
}
Expand Down Expand Up @@ -64,7 +64,7 @@ func (a App) UpdateDefault(ctx context.Context, req *parsers.Params, overruleChe

log.Info("pulling config repo from remote")

if out, err := exec.CommandContext(ctx, "git", "-C", repoPath, "pull", "origin", cdep.DefaultBranch).Output(); err != nil {
if out, err := exec.CommandContext(ctx, "git", "-C", repoPath, "pull", "origin", cdep.DefaultBranch).CombinedOutput(); err != nil {
fmt.Println(string(out))
return err
}
Expand Down Expand Up @@ -170,7 +170,7 @@ func (a App) UpdateDefault(ctx context.Context, req *parsers.Params, overruleChe

log.Info("pushing commit to config repo")

if out, err := exec.CommandContext(ctx, "git", "-C", repoPath, "push", "origin", cdep.DefaultBranch).Output(); err != nil {
if out, err := exec.CommandContext(ctx, "git", "-C", repoPath, "push", "origin", cdep.DefaultBranch).CombinedOutput(); err != nil {
fmt.Println(string(out))
return err
}
Expand Down
2 changes: 1 addition & 1 deletion tools/cdep/parsers/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// exceptions of services that start with "service-"
var exceptions = map[string]struct{}{
"web-underwriter": {},
"web-mid": {},
"web-mid": {},
}

func Parse(args []string, branch string, prodSys bool, message string) (*Params, error) {
Expand Down
9 changes: 9 additions & 0 deletions tools/cdep/paths/terra.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package paths

import (
"path"
)

func GetPathForTerra(repo, system, env, workspace string) string {
return path.Join(repo, system, env, "terra", workspace+".json")
}
3 changes: 3 additions & 0 deletions tools/cdep/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var Types = map[string]struct{}{
"lambdas": {},
"service": {},
"services": {},
"terra": {},
}

func ParseTypeArg(in string) (string, error) {
Expand All @@ -26,6 +27,8 @@ func ParseTypeArg(in string) (string, error) {
return "service", nil
case "lambda", "lambdas":
return "lambda", nil
case "terra":
return "terra", nil
default:
return "", cher.New("impossible", nil)
}
Expand Down

0 comments on commit d111576

Please sign in to comment.