Skip to content

Commit

Permalink
add templating support and add login shell when prompt is not there
Browse files Browse the repository at this point in the history
Signed-off-by: Pulak Kanti Bhowmick <pkbhowmick007@gmail.com>
  • Loading branch information
pkbhowmick committed Nov 16, 2024
1 parent 8d06b14 commit e4ce923
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 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 @@ -158,7 +159,24 @@ func execTerraformShellCommand(

hasCustomShellPrompt := cliConfig.Components.Terraform.Shell.Prompt != ""
if hasCustomShellPrompt {
componentEnvList = append(componentEnvList, fmt.Sprintf("PS1=%s", cliConfig.Components.Terraform.Shell.Prompt))
// 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)")
Expand Down Expand Up @@ -204,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

0 comments on commit e4ce923

Please sign in to comment.