diff --git a/cmd/docs.go b/cmd/docs.go index ba014f546..547cd018a 100644 --- a/cmd/docs.go +++ b/cmd/docs.go @@ -8,7 +8,9 @@ import ( "runtime" "github.com/charmbracelet/glamour" + "github.com/charmbracelet/lipgloss" "github.com/spf13/cobra" + "golang.org/x/term" cfg "github.com/cloudposse/atmos/pkg/config" "github.com/cloudposse/atmos/pkg/schema" @@ -37,18 +39,33 @@ var docsCmd = &cobra.Command{ u.LogErrorAndExit(schema.CliConfiguration{}, err) } + // Detect terminal width if not specified in `atmos.yaml` + // The default screen width is 120 characters, but uses maxWidth if set and greater than zero + maxWidth := cliConfig.Settings.Docs.MaxWidth + defaultWidth := 120 + screenWidth := defaultWidth + + // Detect terminal width and use it by default if available + if term.IsTerminal(int(os.Stdout.Fd())) { + termWidth, _, err := term.GetSize(int(os.Stdout.Fd())) + if err == nil && termWidth > 0 { + // Adjusted for subtle padding effect at the terminal boundaries + screenWidth = termWidth - 2 + } + } + + if maxWidth > 0 { + screenWidth = min(maxWidth, screenWidth) + } + // Construct the full path to the Terraform component by combining the Atmos base path, Terraform base path, and component name componentPath := path.Join(cliConfig.BasePath, cliConfig.Components.Terraform.BasePath, info.Component) - componentPathExists, err := u.IsDirectory(componentPath) if err != nil { u.LogErrorAndExit(schema.CliConfiguration{}, err) } if !componentPathExists { - u.LogErrorAndExit(schema.CliConfiguration{}, fmt.Errorf("Component '%s' not found in path: '%s'", - info.Component, - componentPath, - )) + u.LogErrorAndExit(schema.CliConfiguration{}, fmt.Errorf("Component '%s' not found in path: '%s'", info.Component, componentPath)) } readmePath := path.Join(componentPath, "README.md") @@ -56,7 +73,7 @@ var docsCmd = &cobra.Command{ if os.IsNotExist(err) { u.LogErrorAndExit(schema.CliConfiguration{}, fmt.Errorf("No README found for component: %s", info.Component)) } else { - u.LogErrorAndExit(schema.CliConfiguration{}, err) + u.LogErrorAndExit(schema.CliConfiguration{}, fmt.Errorf("Component %s not found", info.Component)) } } @@ -65,7 +82,17 @@ var docsCmd = &cobra.Command{ u.LogErrorAndExit(schema.CliConfiguration{}, err) } - componentDocs, err := glamour.Render(string(readmeContent), "dark") + r, err := glamour.NewTermRenderer( + glamour.WithColorProfile(lipgloss.ColorProfile()), + glamour.WithAutoStyle(), + glamour.WithPreservedNewLines(), + glamour.WithWordWrap(screenWidth), + ) + if err != nil { + u.LogErrorAndExit(schema.CliConfiguration{}, fmt.Errorf("failed to initialize markdown renderer: %w", err)) + } + + componentDocs, err := r.Render(string(readmeContent)) if err != nil { u.LogErrorAndExit(schema.CliConfiguration{}, err) } diff --git a/internal/exec/shell_utils.go b/internal/exec/shell_utils.go index 9ddb12db7..c142c75cd 100644 --- a/internal/exec/shell_utils.go +++ b/internal/exec/shell_utils.go @@ -7,6 +7,7 @@ import ( "io" "os" "os/exec" + "path/filepath" "runtime" "strings" @@ -154,6 +155,7 @@ 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)) 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)) @@ -183,11 +185,21 @@ func execTerraformShellCommand( if len(shellCommand) == 0 { bashPath, err := exec.LookPath("bash") if err != nil { - return err + // Try fallback to sh if bash is not available + shPath, shErr := exec.LookPath("sh") + if shErr != nil { + return fmt.Errorf("no suitable shell found: %v", shErr) + } + shellCommand = shPath + } else { + shellCommand = bashPath } - shellCommand = bashPath } - shellCommand = shellCommand + " -l" + + shellName := filepath.Base(shellCommand) + if shellName == "zsh" { + shellCommand = shellCommand + " -d -f -i" + } } u.LogDebug(cliConfig, fmt.Sprintf("Starting process: %s\n", shellCommand)) diff --git a/pkg/schema/schema.go b/pkg/schema/schema.go index 41b677970..d9fefef3f 100644 --- a/pkg/schema/schema.go +++ b/pkg/schema/schema.go @@ -29,6 +29,11 @@ type CliConfiguration struct { type CliSettings struct { ListMergeStrategy string `yaml:"list_merge_strategy" json:"list_merge_strategy" mapstructure:"list_merge_strategy"` + Docs Docs `yaml:"docs,omitempty" json:"docs,omitempty" mapstructure:"docs"` +} + +type Docs struct { + MaxWidth int `yaml:"max-width" json:"max_width" mapstructure:"max-width"` } type Templates struct {