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

Rb/use file order #65

Merged
merged 4 commits into from Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions Makefile
@@ -1,6 +1,5 @@
helm-docs:
cd cmd/helm-docs && go build
mv cmd/helm-docs/helm-docs .
go build github.com/norwoodj/helm-docs/cmd/helm-docs

.PHONY: fmt
fmt:
Expand Down
2 changes: 2 additions & 0 deletions cmd/helm-docs/command_line.go
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"strings"

"github.com/norwoodj/helm-docs/pkg/document"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -48,6 +49,7 @@ func newHelmDocsCommand(run func(cmd *cobra.Command, args []string)) (*cobra.Com
command.PersistentFlags().StringP("ignore-file", "i", ".helmdocsignore", "The filename to use as an ignore file to exclude chart directories")
command.PersistentFlags().StringP("log-level", "l", "info", logLevelUsage)
command.PersistentFlags().StringP("output-file", "o", "README.md", "markdown file path relative to each chart directory to which rendered documentation will be written")
command.PersistentFlags().StringP("sort-values-order", "s", document.AlphaNumSortOrder, fmt.Sprintf("order in which to sort the values table (\"%s\" or \"%s\")", document.AlphaNumSortOrder, document.FileSortOrder))
command.PersistentFlags().StringSliceP("template-files", "t", []string{"README.md.gotmpl"}, "gotemplate file paths relative to each chart directory from which documentation will be generated")

viper.AutomaticEnv()
Expand Down
50 changes: 43 additions & 7 deletions pkg/document/model.go
Expand Up @@ -2,9 +2,12 @@ package document

import (
"fmt"
"sort"
"strconv"

log "github.com/sirupsen/logrus"
"github.com/norwoodj/helm-docs/pkg/helm"
"github.com/spf13/viper"
"gopkg.in/yaml.v3"
)

Expand All @@ -15,6 +18,8 @@ type valueRow struct {
Default string
AutoDescription string
Description string
Column int
LineNumber int
}

type chartTemplateData struct {
Expand All @@ -23,6 +28,43 @@ type chartTemplateData struct {
Values []valueRow
}

func getSortedValuesTableRows(documentRoot *yaml.Node, chartValuesDescriptions map[string]helm.ChartValueDescription) ([]valueRow, error) {
valuesTableRows, err := createValueRowsFromField(
"",
nil,
documentRoot,
chartValuesDescriptions,
true,
)

if err != nil {
return nil, err
}

sortOrder := viper.GetString("sort-values-order")
if sortOrder == FileSortOrder {
sort.Slice(valuesTableRows, func(i, j int) bool {
if valuesTableRows[i].LineNumber == valuesTableRows[j].LineNumber {
return valuesTableRows[i].Column < valuesTableRows[j].Column
}

return valuesTableRows[i].LineNumber < valuesTableRows[i].LineNumber
})
} else if sortOrder == AlphaNumSortOrder {
sort.Slice(valuesTableRows, func(i, j int) bool {
return valuesTableRows[i].Key < valuesTableRows[j].Key
})
} else {
log.Warnf("Invalid sort order provided %s, defaulting to %s", sortOrder, AlphaNumSortOrder)
sort.Slice(valuesTableRows, func(i, j int) bool {
return valuesTableRows[i].Key < valuesTableRows[j].Key
})
}

return valuesTableRows, nil
}


func getChartTemplateData(chartDocumentationInfo helm.ChartDocumentationInfo, helmDocsVersion string) (chartTemplateData, error) {
// handle empty values file case
if chartDocumentationInfo.ChartValues.Kind == 0 {
Expand All @@ -40,13 +82,7 @@ func getChartTemplateData(chartDocumentationInfo helm.ChartDocumentationInfo, he
return chartTemplateData{}, fmt.Errorf("values file must resolve to a map, not %s", strconv.Itoa(int(chartDocumentationInfo.ChartValues.Kind)))
}

valuesTableRows, err := createValueRowsFromObject(
"",
nil,
chartDocumentationInfo.ChartValues.Content[0],
chartDocumentationInfo.ChartValuesDescriptions,
true,
)
valuesTableRows, err := getSortedValuesTableRows(chartDocumentationInfo.ChartValues.Content[0], chartDocumentationInfo.ChartValuesDescriptions)

if err != nil {
return chartTemplateData{}, err
Expand Down
7 changes: 5 additions & 2 deletions pkg/document/util.go
Expand Up @@ -5,14 +5,17 @@ import (
"gopkg.in/yaml.v3"
)

type jsonableMap map[string]interface{}
const (
AlphaNumSortOrder = "alphanum"
FileSortOrder = "file"
)

// The json library can only marshal maps with string keys, and so all of our lists and maps that go into documentation
// must be converted to have only string keys before marshalling
func convertHelmValuesToJsonable(values *yaml.Node) interface{} {
switch values.Kind {
case yaml.MappingNode:
convertedMap := make(jsonableMap)
convertedMap := make(map[string]interface{})

for i := 0; i < len(values.Content); i += 2 {
k := values.Content[i]
Expand Down