8000 Updated config reference docs generation by applejag · Pull Request #160 · kubecolor/kubecolor · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Updated config reference docs generation #160

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

Merged
merged 1 commit into from
Aug 21, 2024
Merged
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
48 changes: 41 additions & 7 deletions internal/cmd/configdoc/print_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

import (
"fmt"
"io"
"os"
"strings"
"text/tabwriter"
"unicode"
"unicode/utf8"
)

type YAMLPrinter struct {
Expand All @@ -16,31 +21,34 @@
}

func (p *YAMLPrinter) printCategory(category Category, path []string) error {
tw := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)

Check warning on line 24 in internal/cmd/configdoc/print_yaml.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/configdoc/print_yaml.go#L24

Added line #L24 was not covered by tests
for _, field := range category.Fields {
newPath := append(path, field.Name)
switch field.Type {
case "Color":
p.printField(field, "color", newPath)
p.printField(tw, field, "color", newPath)

Check warning on line 29 in internal/cmd/configdoc/print_yaml.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/configdoc/print_yaml.go#L29

Added line #L29 was not covered by tests
case "ColorSlice":
p.printField(field, "color[]", newPath)
p.printField(tw, field, "color[]", newPath)

Check warning on line 31 in internal/cmd/configdoc/print_yaml.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/configdoc/print_yaml.go#L31

Added line #L31 was not covered by tests
default:
sub, ok := p.findCategory(field.Type)
if !ok {
return fmt.Errorf("invalid category field type: %q", field.Type)
}
fmt.Printf("%s%s:\n",
fmt.Fprintf(tw, "%s%s:\n",

Check warning on line 37 in internal/cmd/configdoc/print_yaml.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/configdoc/print_yaml.go#L37

Added line #L37 was not covered by tests
strings.Repeat(" ", len(path)),
strings.ToLower(field.Name),
p.formatName(field),

Check warning on line 39 in internal/cmd/configdoc/print_yaml.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/configdoc/print_yaml.go#L39

Added line #L39 was not covered by tests
)
tw.Flush()

Check warning on line 41 in internal/cmd/configdoc/print_yaml.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/configdoc/print_yaml.go#L41

Added line #L41 was not covered by tests
if err := p.printCategory(sub, newPath); err != nil {
return err
}
}
}
tw.Flush()

Check warning on line 47 in internal/cmd/configdoc/print_yaml.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/configdoc/print_yaml.go#L47

Added line #L47 was not covered by tests
return nil
}

func (p *YAMLPrinter) printField(field Field, typeString string, path []string) error {
func (p *YAMLPrinter) printField(w io.Writer, field Field, typeString string, path []string) error {

Check warning on line 51 in internal/cmd/configdoc/print_yaml.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/configdoc/print_yaml.go#L51

Added line #L51 was not covered by tests
fallback := p.formatFallback(field)
desc := strings.ReplaceAll(field.Comment, "\n", " ")
if fallback != "" {
Expand All @@ -53,7 +61,7 @@
desc = " " + desc
}

fmt.Printf("%s%s: %s # (%s)%s\n",
fmt.Fprintf(w, "%s%s: %s\t# (%s)%s\n",

Check warning on line 64 in internal/cmd/configdoc/print_yaml.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/configdoc/print_yaml.go#L64

Added line #L64 was not covered by tests
strings.Repeat(" ", len(path)-1),
p.formatName(field),
p.viper.GetString(strings.Join(path, ".")),
Expand All @@ -77,11 +85,37 @@
}

func (p *YAMLPrinter) formatName(field Field) string {
lower := strings.ToLower(field.Name)
lower := camelCase(field.Name)

Check warning on line 88 in internal/cmd/configdoc/print_yaml.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/configdoc/print_yaml.go#L88

Added line #L88 was not covered by tests

switch lower {
case "true", "false", "null":
return fmt.Sprintf(`"%s"`, lower)
default:
return lower
}
}

func camelCase(s string) string {
if isAllUppercase(s) {
return strings.ToLower(s)

Check warning on line 100 in internal/cmd/configdoc/print_yaml.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/configdoc/print_yaml.go#L98-L100

Added lines #L98 - L100 were not covered by tests
}
return firstLetterLowercase(s)

Check warning on line 102 in internal/cmd/configdoc/print_yaml.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/configdoc/print_yaml.go#L102

Added line #L102 was not covered by tests
}

func firstLetterLowercase(s string) string {
var sb strings.Builder
sb.Grow(len(s))
firstRune, size := utf8.DecodeRuneInString(s)
sb.WriteRune(unicode.ToLower(firstRune))
sb.WriteString(s[size:])
return sb.String()

Check warning on line 111 in internal/cmd/configdoc/print_yaml.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/configdoc/print_yaml.go#L105-L111

Added lines #L105 - L111 were not covered by tests
}

func isAllUppercase(s string) bool {
for _, r := range s {
if unicode.IsLower(r) {
return false

Check warning on line 117 in internal/cmd/configdoc/print_yaml.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/configdoc/print_yaml.go#L114-L117

Added lines #L114 - L117 were not covered by tests
}
}
return true

Check warning on line 120 in internal/cmd/configdoc/print_yaml.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/configdoc/print_yaml.go#L120

Added line #L120 was not covered by tests
}
0