Skip to content

Commit

Permalink
feat: additional atmos docs parameters for specifying width, using …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
RoseSecurity authored Nov 11, 2024
1 parent ffb64eb commit 1250929
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 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
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 1250929

Please sign in to comment.