Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: additional atmos docs parameters for specifying width, using auto-styling and color profile, and preserving new lines #757

Merged
merged 12 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 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,40 @@ 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 {
screenWidth = termWidth
}
}

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 +81,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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ require (
github.com/spf13/viper v1.19.0
github.com/stretchr/testify v1.9.0
github.com/zclconf/go-cty v1.15.0
golang.org/x/term v0.25.0
gopkg.in/yaml.v3 v3.0.1
mvdan.cc/sh/v3 v3.10.0
)
Expand Down Expand Up @@ -242,7 +243,6 @@ require (
golang.org/x/oauth2 v0.22.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/term v0.25.0 // indirect
golang.org/x/text v0.19.0 // indirect
golang.org/x/time v0.7.0 // indirect
golang.org/x/tools v0.22.0 // indirect
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