Skip to content

Commit

Permalink
feat: add support for sorting based on presence in file
Browse files Browse the repository at this point in the history
  • Loading branch information
rbren authored and norwoodj committed Oct 6, 2020
1 parent adb421c commit de4bf77
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 108 deletions.
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
7 changes: 7 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,7 +49,13 @@ 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")
<<<<<<< HEAD
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")
=======
command.PersistentFlags().StringP("template-file", "t", "README.md.gotmpl", "gotemplate file path relative to each chart directory from which documentation will be generated")
command.PersistentFlags().StringP("sort-values-order", "s", "alpha", "order in which to print the values (alpha or file)")
>>>>>>> 18b313e... change flag

viper.AutomaticEnv()
viper.SetEnvPrefix("HELM_DOCS")
Expand Down
29 changes: 28 additions & 1 deletion 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 @@ -40,14 +45,36 @@ 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(
valuesTableRows, err := createValueRowsFromField(
"",
nil,
chartDocumentationInfo.ChartValues.Content[0],
chartDocumentationInfo.ChartValuesDescriptions,
true,
)

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 true
} else if valuesTableRows[i].Column < valuesTableRows[j].Column {
return true
}

return false
})
} 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
})
}

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

0 comments on commit de4bf77

Please sign in to comment.