Skip to content

Commit

Permalink
Merge branch 'main' into update-atmos-component-template-func
Browse files Browse the repository at this point in the history
  • Loading branch information
aknysh authored Nov 12, 2024
2 parents 57fd95e + d502eb0 commit c94c016
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
41 changes: 34 additions & 7 deletions cmd/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -37,26 +39,41 @@ 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")
if _, err := os.Stat(readmePath); err != nil {
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))
}
}

Expand All @@ -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)
}
Expand Down
18 changes: 15 additions & 3 deletions internal/exec/shell_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down
5 changes: 5 additions & 0 deletions pkg/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit c94c016

Please sign in to comment.