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

Emit Friendly Message When Component Docs Not Found #1032

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all 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
28 changes: 13 additions & 15 deletions cmd/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

"github.com/charmbracelet/glamour"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log"

Check failure on line 12 in cmd/docs.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] cmd/docs.go#L12

import "github.com/charmbracelet/log" imported without alias but must be with alias "log" according to config (importas)
Raw output
cmd/docs.go:12:2: import "github.com/charmbracelet/log" imported without alias but must be with alias "log" according to config (importas)
	"github.com/charmbracelet/log"
	^
"github.com/spf13/cobra"
"golang.org/x/term"

Expand Down Expand Up @@ -45,7 +46,7 @@
maxWidth := atmosConfig.Settings.Terminal.MaxWidth
if maxWidth == 0 && atmosConfig.Settings.Docs.MaxWidth > 0 {
maxWidth = atmosConfig.Settings.Docs.MaxWidth
u.LogWarning("'settings.docs.max-width' is deprecated and will be removed in a future version. Please use 'settings.terminal.max_width' instead")
log.Warn("'settings.docs.max-width' is deprecated and will be removed in a future version. Please use 'settings.terminal.max_width' instead")

Check warning on line 49 in cmd/docs.go

View check run for this annotation

Codecov / codecov/patch

cmd/docs.go#L49

Added line #L49 was not covered by tests
}
defaultWidth := 120
screenWidth := defaultWidth
Expand All @@ -67,24 +68,21 @@
componentPath := filepath.Join(atmosConfig.BasePath, atmosConfig.Components.Terraform.BasePath, info.Component)
componentPathExists, err := u.IsDirectory(componentPath)
if err != nil {
u.PrintErrorMarkdownAndExit("", err, "")
log.Debug(err)
u.PrintErrorMarkdownAndExit("", fmt.Errorf("Component not found"), "")

Check failure on line 72 in cmd/docs.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] cmd/docs.go#L72

do not define dynamic errors, use wrapped static errors instead: "fmt.Errorf(\"Component not found\")" (err113)
Raw output
cmd/docs.go:72:37: do not define dynamic errors, use wrapped static errors instead: "fmt.Errorf(\"Component not found\")" (err113)
				u.PrintErrorMarkdownAndExit("", fmt.Errorf("Component not found"), "")
				                                ^

Check warning on line 72 in cmd/docs.go

View check run for this annotation

Codecov / codecov/patch

cmd/docs.go#L71-L72

Added lines #L71 - L72 were not covered by tests
}
if !componentPathExists {
u.PrintErrorMarkdownAndExit("", fmt.Errorf("Component `%s` not found in path: `%s`", info.Component, componentPath), "")
u.PrintErrorMarkdownAndExit("", fmt.Errorf("Component not found"), "")

Check failure on line 75 in cmd/docs.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] cmd/docs.go#L75

do not define dynamic errors, use wrapped static errors instead: "fmt.Errorf(\"Component not found\")" (err113)
Raw output
cmd/docs.go:75:37: do not define dynamic errors, use wrapped static errors instead: "fmt.Errorf(\"Component not found\")" (err113)
				u.PrintErrorMarkdownAndExit("", fmt.Errorf("Component not found"), "")
				                                ^

Check warning on line 75 in cmd/docs.go

View check run for this annotation

Codecov / codecov/patch

cmd/docs.go#L75

Added line #L75 was not covered by tests
}

readmePath := filepath.Join(componentPath, "README.md")
if _, err := os.Stat(readmePath); err != nil {
if os.IsNotExist(err) {
u.LogErrorAndExit(fmt.Errorf("No README found for component: %s", info.Component))
} else {
u.LogErrorAndExit(fmt.Errorf("Component %s not found", info.Component))
}
u.PrintErrorMarkdownAndExit("", fmt.Errorf("Documentation is missing for the component `%s`. Consider adding a README.md to provide more context and details.", info.Component), "")

Check failure on line 80 in cmd/docs.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] cmd/docs.go#L80

error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
Raw output
cmd/docs.go:80:48: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
				u.PrintErrorMarkdownAndExit("", fmt.Errorf("Documentation is missing for the component `%s`. Consider adding a README.md to provide more context and details.", info.Component), "")
				                                           ^

Check warning on line 80 in cmd/docs.go

View check run for this annotation

Codecov / codecov/patch

cmd/docs.go#L80

Added line #L80 was not covered by tests
}

readmeContent, err := os.ReadFile(readmePath)
if err != nil {
u.LogErrorAndExit(err)
u.PrintErrorMarkdownAndExit("", err, "")

Check warning on line 85 in cmd/docs.go

View check run for this annotation

Codecov / codecov/patch

cmd/docs.go#L85

Added line #L85 was not covered by tests
}

r, err := glamour.NewTermRenderer(
Expand All @@ -94,22 +92,22 @@
glamour.WithWordWrap(screenWidth),
)
if err != nil {
u.LogErrorAndExit(fmt.Errorf("failed to initialize markdown renderer: %w", err))
u.PrintErrorMarkdownAndExit("", fmt.Errorf("failed to initialize markdown renderer: %w", err), "")

Check warning on line 95 in cmd/docs.go

View check run for this annotation

Codecov / codecov/patch

cmd/docs.go#L95

Added line #L95 was not covered by tests
}

componentDocs, err := r.Render(string(readmeContent))
if err != nil {
u.LogErrorAndExit(err)
u.PrintErrorMarkdownAndExit("", err, "")

Check warning on line 100 in cmd/docs.go

View check run for this annotation

Codecov / codecov/patch

cmd/docs.go#L100

Added line #L100 was not covered by tests
}

pager := atmosConfig.Settings.Terminal.Pager
if !pager && atmosConfig.Settings.Docs.Pagination {
pager = atmosConfig.Settings.Docs.Pagination
u.LogWarning("'settings.docs.pagination' is deprecated and will be removed in a future version. Please use 'settings.terminal.pager' instead")
log.Warn("'settings.docs.pagination' is deprecated and will be removed in a future version. Please use 'settings.terminal.pager' instead")

Check warning on line 106 in cmd/docs.go

View check run for this annotation

Codecov / codecov/patch

cmd/docs.go#L106

Added line #L106 was not covered by tests
}

if err := u.DisplayDocs(componentDocs, pager); err != nil {
u.LogErrorAndExit(fmt.Errorf("failed to display documentation: %w", err))
u.PrintErrorMarkdownAndExit("", fmt.Errorf("failed to display documentation: %w", err), "")

Check warning on line 110 in cmd/docs.go

View check run for this annotation

Codecov / codecov/patch

cmd/docs.go#L110

Added line #L110 was not covered by tests
}

return
Expand All @@ -119,7 +117,7 @@
var err error

if os.Getenv("GO_TEST") == "1" {
u.LogDebug("Skipping browser launch in test environment")
log.Debug("Skipping browser launch in test environment")

Check warning on line 120 in cmd/docs.go

View check run for this annotation

Codecov / codecov/patch

cmd/docs.go#L120

Added line #L120 was not covered by tests
} else {
switch runtime.GOOS {
case "linux":
Expand All @@ -133,7 +131,7 @@
}

if err != nil {
u.LogErrorAndExit(err)
u.PrintErrorMarkdownAndExit("", err, "")

Check warning on line 134 in cmd/docs.go

View check run for this annotation

Codecov / codecov/patch

cmd/docs.go#L134

Added line #L134 was not covered by tests
}
}

Expand Down