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

add hooks and store write functionality #874

Merged
merged 18 commits into from
Jan 8, 2025
Merged
71 changes: 66 additions & 5 deletions cmd/terraform.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
package cmd

import (
"context"
"fmt"
"strings"

"github.com/samber/lo"
"github.com/spf13/cobra"

e "github.com/cloudposse/atmos/internal/exec"
cfg "github.com/cloudposse/atmos/pkg/config"
h "github.com/cloudposse/atmos/pkg/hooks"
"github.com/cloudposse/atmos/pkg/schema"
u "github.com/cloudposse/atmos/pkg/utils"
)

type contextKey string

// terraformCmd represents the base command for all terraform sub-commands
var terraformCmd = &cobra.Command{
Use: "terraform",
Aliases: []string{"tf"},
Short: "Execute Terraform commands",
Long: `This command executes Terraform commands`,
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: true},
Run: func(cmd *cobra.Command, args []string) {
// Check Atmos configuration
//checkAtmosConfig()

PreRun: func(cmd *cobra.Command, args []string) {
var argsAfterDoubleDash []string
var finalArgs = args

Expand All @@ -28,11 +33,21 @@ var terraformCmd = &cobra.Command{
finalArgs = lo.Slice(args, 0, doubleDashIndex)
argsAfterDoubleDash = lo.Slice(args, doubleDashIndex+1, len(args))
}

info, err := e.ProcessCommandLineArgs("terraform", cmd, finalArgs, argsAfterDoubleDash)
if err != nil {
u.LogErrorAndExit(schema.AtmosConfiguration{}, err)
}

ctx := context.WithValue(context.Background(), contextKey("atmos_info"), info)
RootCmd.SetContext(ctx)

// Check Atmos configuration
checkAtmosConfig()
},
Run: func(cmd *cobra.Command, args []string) {
info := RootCmd.Context().Value(contextKey("atmos_info")).(schema.ConfigAndStacksInfo)

// Exit on help
if info.NeedHelp {
// Check for the latest Atmos release on GitHub and print update message
Expand All @@ -42,10 +57,56 @@ var terraformCmd = &cobra.Command{
// Check Atmos configuration
checkAtmosConfig()

err = e.ExecuteTerraform(info)
err := e.ExecuteTerraform(info)
if err != nil {
u.LogErrorAndExit(schema.AtmosConfiguration{}, err)
}

},
PostRun: func(cmd *cobra.Command, args []string) {
info := RootCmd.Context().Value(contextKey("atmos_info")).(schema.ConfigAndStacksInfo)
atmosConfig, err := cfg.InitCliConfig(schema.ConfigAndStacksInfo{}, false)
if err != nil {
u.LogErrorAndExit(atmosConfig, err)
}

sections, err := e.ExecuteDescribeComponent(info.ComponentFromArg, info.Stack, true)
if err != nil {
u.LogErrorAndExit(atmosConfig, err)
}

if info.SubCommand == "apply" || info.SubCommand == "deploy" {
hooks := h.Hooks{}
hooks, err = hooks.ConvertToHooks(sections["hooks"].(map[string]any))
if err != nil {
u.LogErrorAndExit(atmosConfig, fmt.Errorf("invalid hooks section %v", sections["hooks"]))
}

for _, hook := range hooks {
if strings.ToLower(hook.Command) == "store" {
u.LogInfo(atmosConfig, fmt.Sprintf("\nexecuting 'after-terraform-apply' hook '%s' with command '%s'", hook.Name, hook.Command))
for key, value := range hook.Outputs {
var outputValue any
outputKey := strings.TrimPrefix(value, ".")

if strings.Index(value, ".") == 0 {
outputValue = e.GetTerraformOutput(&atmosConfig, info.Stack, info.ComponentFromArg, outputKey, true)
} else {
outputValue = value
}

store := atmosConfig.Stores[hook.Name]
u.LogInfo(atmosConfig, fmt.Sprintf(" storing terraform output '%s' in store '%s' with key '%s' and value %v", outputKey, hook.Name, key, outputValue))
err := store.Set(info.Stack, info.ComponentFromArg, key, outputValue)

if err != nil {
u.LogErrorAndExit(atmosConfig, err)
}
}
}
}

}
},
}

Expand Down
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ require (
github.com/stretchr/testify v1.10.0
github.com/zclconf/go-cty v1.15.1
golang.org/x/term v0.27.0
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
mvdan.cc/sh/v3 v3.10.0
)
Expand Down Expand Up @@ -179,6 +180,8 @@ require (
github.com/joho/godotenv v1.4.0 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
Expand Down Expand Up @@ -209,6 +212,7 @@ require (
github.com/prometheus/procfs v0.15.1 // indirect
github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/rogpeppe/go-internal v1.13.1 // indirect
github.com/rs/zerolog v1.26.1 // indirect
github.com/ryanuber/go-glob v1.0.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
Expand Down Expand Up @@ -265,6 +269,7 @@ require (
google.golang.org/genproto/googleapis/rpc v0.0.0-20241209162323-e6fa225c2576 // indirect
google.golang.org/grpc v1.69.2 // indirect
google.golang.org/protobuf v1.35.2 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/square/go-jose.v2 v2.6.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
Expand Down
Loading
Loading