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 support for disabling custom terraform shell prompt #786

Merged
merged 5 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
28 changes: 27 additions & 1 deletion internal/exec/shell_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"path/filepath"
"runtime"
"strings"
"text/template"

"mvdan.cc/sh/v3/expand"
"mvdan.cc/sh/v3/interp"
Expand Down Expand Up @@ -155,7 +156,28 @@ func execTerraformShellCommand(
componentEnvList = append(componentEnvList, fmt.Sprintf("TF_CLI_ARGS_import=-var-file=%s", varFile))
componentEnvList = append(componentEnvList, fmt.Sprintf("TF_CLI_ARGS_destroy=-var-file=%s", varFile))
componentEnvList = append(componentEnvList, fmt.Sprintf("TF_CLI_ARGS_console=-var-file=%s", varFile))
componentEnvList = append(componentEnvList, fmt.Sprintf("PS1=atmos:%s/%s> ", stack, component))

hasCustomShellPrompt := cliConfig.Components.Terraform.Shell.Prompt != ""
if hasCustomShellPrompt {
// Template for the custom shell prompt
tmpl := cliConfig.Components.Terraform.Shell.Prompt

// Data for the template
data := struct {
Component string
Stack string
}{
Component: component,
Stack: stack,
}

// Parse and execute the template
var result bytes.Buffer
t := template.Must(template.New("shellPrompt").Parse(tmpl))
if err := t.Execute(&result, data); err == nil {
componentEnvList = append(componentEnvList, fmt.Sprintf("PS1=%s", result.String()))
}
}

u.LogDebug(cliConfig, "\nStarting a new interactive shell where you can execute all native Terraform commands (type 'exit' to go back)")
u.LogDebug(cliConfig, fmt.Sprintf("Component: %s\n", component))
Expand Down Expand Up @@ -200,6 +222,10 @@ func execTerraformShellCommand(
if shellName == "zsh" {
shellCommand = shellCommand + " -d -f -i"
}

if !hasCustomShellPrompt {
shellCommand = shellCommand + " -l"
}
}

u.LogDebug(cliConfig, fmt.Sprintf("Starting process: %s\n", shellCommand))
Expand Down
19 changes: 12 additions & 7 deletions pkg/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,18 @@ type TemplatesSettingsGomplate struct {
}

type Terraform struct {
BasePath string `yaml:"base_path" json:"base_path" mapstructure:"base_path"`
ApplyAutoApprove bool `yaml:"apply_auto_approve" json:"apply_auto_approve" mapstructure:"apply_auto_approve"`
AppendUserAgent string `yaml:"append_user_agent" json:"append_user_agent" mapstructure:"append_user_agent"`
DeployRunInit bool `yaml:"deploy_run_init" json:"deploy_run_init" mapstructure:"deploy_run_init"`
InitRunReconfigure bool `yaml:"init_run_reconfigure" json:"init_run_reconfigure" mapstructure:"init_run_reconfigure"`
AutoGenerateBackendFile bool `yaml:"auto_generate_backend_file" json:"auto_generate_backend_file" mapstructure:"auto_generate_backend_file"`
Command string `yaml:"command" json:"command" mapstructure:"command"`
BasePath string `yaml:"base_path" json:"base_path" mapstructure:"base_path"`
ApplyAutoApprove bool `yaml:"apply_auto_approve" json:"apply_auto_approve" mapstructure:"apply_auto_approve"`
AppendUserAgent string `yaml:"append_user_agent" json:"append_user_agent" mapstructure:"append_user_agent"`
DeployRunInit bool `yaml:"deploy_run_init" json:"deploy_run_init" mapstructure:"deploy_run_init"`
InitRunReconfigure bool `yaml:"init_run_reconfigure" json:"init_run_reconfigure" mapstructure:"init_run_reconfigure"`
AutoGenerateBackendFile bool `yaml:"auto_generate_backend_file" json:"auto_generate_backend_file" mapstructure:"auto_generate_backend_file"`
Command string `yaml:"command" json:"command" mapstructure:"command"`
Shell ShellConfig `yaml:"shell" json:"shell" mapstructure:"shell"`
}

type ShellConfig struct {
Prompt string `yaml:"prompt" json:"prompt" mapstructure:"prompt"`
}

type Helmfile struct {
Expand Down