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 4 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: 34 additions & 6 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,18 +39,33 @@ var docsCmd = &cobra.Command{
u.LogErrorAndExit(schema.CliConfiguration{}, err)
}

// Detect terminal width if width flag is not set
width, _ := cmd.Flags().GetUint("width")
if !cmd.Flags().Changed("width") {
if term.IsTerminal(int(os.Stdout.Fd())) {
w, _, err := term.GetSize(int(os.Stdout.Fd()))
RoseSecurity marked this conversation as resolved.
Show resolved Hide resolved
if err == nil {
width = uint(w)
}

if width > 120 {
width = 120
}
RoseSecurity marked this conversation as resolved.
Show resolved Hide resolved
RoseSecurity marked this conversation as resolved.
Show resolved Hide resolved

if width == 0 {
width = 80
}
}
}
RoseSecurity marked this conversation as resolved.
Show resolved Hide resolved

// 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")
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(int(width)),
)
if err != nil {
u.LogErrorAndExit(schema.CliConfiguration{}, err)
}

RoseSecurity marked this conversation as resolved.
Show resolved Hide resolved
componentDocs, err := r.Render(string(readmeContent))
if err != nil {
u.LogErrorAndExit(schema.CliConfiguration{}, err)
}
Expand Down Expand Up @@ -95,4 +122,5 @@ var docsCmd = &cobra.Command{

func init() {
RootCmd.AddCommand(docsCmd)
docsCmd.Flags().UintP("width", "w", 0, "Set word-wrap width (0 disables word wrapping). Influences output formatting, particularly for tables.")
RoseSecurity marked this conversation as resolved.
Show resolved Hide resolved
}
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.18.0 // indirect
golang.org/x/time v0.6.0 // indirect
golang.org/x/tools v0.22.0 // indirect
Expand Down