From 1250929be3fdf19d61c6121895dc39b3a2d33653 Mon Sep 17 00:00:00 2001 From: RoseSecurity <72598486+RoseSecurity@users.noreply.github.com> Date: Mon, 11 Nov 2024 12:04:52 -0500 Subject: [PATCH 1/2] feat: additional `atmos docs` parameters for specifying width, using auto-styling and color profile, and preserving new lines (#757) * feat: add auto-styling instead of dark mode for docs rendering, add an additional parameter for specifying the width of markdown outputs, and maintain a consistent color profile * feat: dynamically detect terminal width * feat: add additional comment on parameter option * feat: move width out of globals * feat: add max-width and docs parameters to atmos.yaml schema * feat: update variable names to adhere to usage * feat: add additional logic for maxwidth * refactor variable name * fix: word wrap to terminal size and add better error message * fix: adjusted rendering based on terminal size by subtracting 2 characters which appears to be a subtle padding effect at the terminal boundaries --- cmd/docs.go | 41 ++++++++++++++++++++++++++++++++++------- pkg/schema/schema.go | 5 +++++ 2 files changed, 39 insertions(+), 7 deletions(-) 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/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 { From d502eb04e122760b9384ff6b6a111635f7d1abf7 Mon Sep 17 00:00:00 2001 From: Pulak Kanti Bhowmick Date: Mon, 11 Nov 2024 23:29:31 +0600 Subject: [PATCH 2/2] change PS1 to show that atmos is in the `atmos terraform shell` mode (#761) * add atmos in PS1 Signed-off-by: Pulak Kanti Bhowmick * add review suggestions Signed-off-by: Pulak Kanti Bhowmick * Update internal/exec/shell_utils.go Co-authored-by: Erik Osterman (CEO @ Cloud Posse) * fix for zsh Signed-off-by: Pulak Kanti Bhowmick * compare only shell name Signed-off-by: Pulak Kanti Bhowmick --------- Signed-off-by: Pulak Kanti Bhowmick Co-authored-by: Erik Osterman (CEO @ Cloud Posse) Co-authored-by: Andriy Knysh --- internal/exec/shell_utils.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) 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))