Skip to content

Commit

Permalink
feat: updates values table generation allowing for non-empty lists/ma…
Browse files Browse the repository at this point in the history
…ps to be documented

  Fixes #9
  • Loading branch information
norwoodj committed Jul 29, 2019
1 parent 5d75e92 commit 4dc1378
Show file tree
Hide file tree
Showing 10 changed files with 683 additions and 121 deletions.
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
helm-docs:
cd cmd/helm-docs && go build
mv cmd/helm-docs/helm-docs .

.PHONY: fmt
fmt:
go fmt ./...

.PHONY: test
test:
go test -v ./...

.PHONY: clean
clean:
rm helm-docs
13 changes: 10 additions & 3 deletions cmd/helm-docs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/spf13/viper"
)

func retrieveInfoAndPrintDocumentation(chartDirectory string, waitGroup *sync.WaitGroup) {
func retrieveInfoAndPrintDocumentation(chartDirectory string, waitGroup *sync.WaitGroup, dryRun bool) {
defer waitGroup.Done()
chartDocumentationInfo, err := helm.ParseChartInformation(chartDirectory)

Expand All @@ -21,7 +21,7 @@ func retrieveInfoAndPrintDocumentation(chartDirectory string, waitGroup *sync.Wa
return
}

document.PrintDocumentation(chartDocumentationInfo, viper.GetBool("dry-run"))
document.PrintDocumentation(chartDocumentationInfo, dryRun)

}

Expand All @@ -35,11 +35,18 @@ func helmDocs(_ *cobra.Command, _ []string) {
}

log.Infof("Found Chart directories [%s]", strings.Join(chartDirs, ", "))
dryRun := viper.GetBool("dry-run")
waitGroup := sync.WaitGroup{}

for _, c := range chartDirs {
waitGroup.Add(1)
go retrieveInfoAndPrintDocumentation(c, &waitGroup)

// On dry runs all output goes to stdout, and so as to not jumble things, generate serially
if dryRun {
retrieveInfoAndPrintDocumentation(c, &waitGroup, dryRun)
} else {
go retrieveInfoAndPrintDocumentation(c, &waitGroup, dryRun)
}
}

waitGroup.Wait()
Expand Down
9 changes: 8 additions & 1 deletion example-charts/nginx-ingress/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@ controller:
# controller.persistentVolumeClaims -- List of persistent volume claims to create
persistentVolumeClaims: []

# controller.extraVolumes -- Add additional volumes to be mounted into the ingress controller container
extraVolumes:
- name: config-volume
configMap:
# controller.extraVolumes[0].configMap.name -- Uses the name of the configmap created by this chart
name: nginx-ingress-config

# controller.livenessProbe -- Configure the healthcheck for the ingress controller
livenessProbe:
httpGet:
# controller.livenessProbe.httpGet.path -- This is the liveness check endpoint
path: /healthz
port: 8080

# controller.ingressClass -- Name of the ingress class to route through this controller
ingressClass: nginx

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ require (
github.com/spf13/cobra v0.0.5
github.com/spf13/pflag v1.0.3
github.com/spf13/viper v1.4.0
github.com/stretchr/testify v1.2.2
gopkg.in/yaml.v2 v2.2.2
)
11 changes: 8 additions & 3 deletions pkg/document/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,19 @@ func PrintDocumentation(chartDocumentationInfo helm.ChartDocumentationInfo, dryR

chartDocumentationTemplate, err := newChartDocumentationTemplate(chartDocumentationInfo)
if err != nil {
log.Warnf("Error generating templates for chart %s: %s", chartDocumentationInfo.ChartDirectory, err)
log.Warnf("Error generating gotemplates for chart %s: %s", chartDocumentationInfo.ChartDirectory, err)
return
}

chartTemplateDataObject := getChartTemplateData(chartDocumentationInfo)
err = chartDocumentationTemplate.Execute(outputFile, chartTemplateDataObject)
chartTemplateDataObject, err := getChartTemplateData(chartDocumentationInfo)
if err != nil {
log.Warnf("Error generating template data for chart %s: %s", chartDocumentationInfo.ChartDirectory, err)
return
}

err = chartDocumentationTemplate.Execute(outputFile, chartTemplateDataObject)
if err != nil {
log.Warnf("Error generating documentation for chart %s: %s", chartDocumentationInfo.ChartDirectory, err)
return
}
}
15 changes: 12 additions & 3 deletions pkg/document/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,20 @@ type chartTemplateData struct {
Values []valueRow
}

func getChartTemplateData(chartDocumentationInfo helm.ChartDocumentationInfo) chartTemplateData {
valuesTableRows := createValueRows("", chartDocumentationInfo.ChartValues, chartDocumentationInfo.ChartValuesDescriptions)
func getChartTemplateData(chartDocumentationInfo helm.ChartDocumentationInfo) (chartTemplateData, error) {
valuesTableRows, err := createValueRowsFromObject(
"",
chartDocumentationInfo.ChartValuesObject,
chartDocumentationInfo.ChartValuesDescriptions,
true,
)

if err != nil {
return chartTemplateData{}, err
}

return chartTemplateData{
ChartDocumentationInfo: chartDocumentationInfo,
Values: valuesTableRows,
}
}, nil
}
50 changes: 50 additions & 0 deletions pkg/document/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package document

import (
"fmt"
"github.com/norwoodj/helm-docs/pkg/helm"
)

type jsonableMap map[string]interface{}

func convertMapKeyToString(key interface{}) string {
switch key.(type) {
case string:
return key.(string)
case int:
return fmt.Sprintf("int(%d)", key)
case float64:
return fmt.Sprintf("float(%f)", key)
case bool:
return fmt.Sprintf("bool(%t)", key)
}

return fmt.Sprintf("?(%+v)", key)
}

// 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 interface{}) interface{} {
switch values.(type) {
case helm.ChartValuesObject:
convertedMap := make(jsonableMap)

for key, value := range values.(helm.ChartValuesObject) {
convertedMap[convertMapKeyToString(key)] = convertHelmValuesToJsonable(value)
}

return convertedMap

case helm.ChartValuesList:
convertedList := make(helm.ChartValuesList, 0)

for _, value := range values.(helm.ChartValuesList) {
convertedList = append(convertedList, convertHelmValuesToJsonable(value))
}

return convertedList

default:
return values
}
}
Loading

0 comments on commit 4dc1378

Please sign in to comment.