Skip to content

Commit

Permalink
Merge pull request #1 from RemingtonReackhof/fix-nil-values
Browse files Browse the repository at this point in the history
* style: run go fmt
* fix: generate docs for nil values and include types for them
* chore: update readme to include simple instructions on nil values
  • Loading branch information
norwoodj authored Jun 14, 2019
2 parents 6c0fb93 + 6707016 commit 2140a87
Show file tree
Hide file tree
Showing 9 changed files with 394 additions and 355 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
helm-docs
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,12 @@ controller:
And the descriptions will be picked up and put in the table in the README. The comment need not be near the parameter it
explains, although this is probably preferable.
### nil values
If you would like to define a key for a value, but leave the default empty, you can still specify a description for it as well as a type. Like so:
```yaml
controller:
# controller.replicas -- (int) Number of nginx-ingress pods to load balance between
replicas:
```
This could be useful when wanting to enforce user-defined values for the chart, where there are no sensible defaults.
38 changes: 19 additions & 19 deletions cmd/helm-docs/chart_finder.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
package main

import (
"os"
"path/filepath"
"strings"
"os"
"path/filepath"
"strings"
)

var ignoreDirectories = map[string]bool{
".git": true,
"templates": true,
".git": true,
"templates": true,
}

func findChartDirectories() []string {
var chartDirs []string
var chartDirs []string

filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if info.IsDir() && ignoreDirectories[path] {
return filepath.SkipDir
}
if info.IsDir() && ignoreDirectories[path] {
return filepath.SkipDir
}

if strings.HasSuffix(path, "Chart.yaml") {
chartDirs = append(chartDirs, filepath.Dir(path))
}
if strings.HasSuffix(path, "Chart.yaml") {
chartDirs = append(chartDirs, filepath.Dir(path))
}

return nil
})
return nil
})

return chartDirs
return chartDirs
}
179 changes: 89 additions & 90 deletions cmd/helm-docs/chart_info.go
Original file line number Diff line number Diff line change
@@ -1,149 +1,148 @@
package main

import (
"bufio"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"os"
"path"
"regexp"
"bufio"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"os"
"path"
"regexp"
)

var valuesDescriptionRegex = regexp.MustCompile("# (.*) -- (.*)")

type ChartMetaMaintainer struct {
Email string
Name string
Email string
Name string
}

type ChartMeta struct {
ApiVersion string `yaml:"apiVersion"`
Name string
Description string
Version string
Home string
Sources []string
Engine string
Maintainers []ChartMetaMaintainer
ApiVersion string `yaml:"apiVersion"`
Name string
Description string
Version string
Home string
Sources []string
Engine string
Maintainers []ChartMetaMaintainer
}

type ChartRequirementsItem struct {
Name string
Version string
Repository string
Name string
Version string
Repository string
}

type ChartRequirements struct {
Dependencies []ChartRequirementsItem
Dependencies []ChartRequirementsItem
}

type ChartValues map[interface{}]interface{}


func getYamlFileContents(filename string, debug bool) ([]byte, error) {
if _, err := os.Stat(filename); os.IsNotExist(err) {
return nil, err
}
if _, err := os.Stat(filename); os.IsNotExist(err) {
return nil, err
}

yamlFileContents, err := ioutil.ReadFile(filename)
yamlFileContents, err := ioutil.ReadFile(filename)

if err != nil {
panic(err)
}
if err != nil {
panic(err)
}

if debug {
log.Printf("Reading %s file contents:\n%s\n", filename, yamlFileContents)
}
if debug {
log.Printf("Reading %s file contents:\n%s\n", filename, yamlFileContents)
}

return []byte(yamlFileContents), nil
return []byte(yamlFileContents), nil
}

func yamlLoadAndCheck(yamlFileContents []byte, out interface{}) {
err := yaml.Unmarshal(yamlFileContents, out)
err := yaml.Unmarshal(yamlFileContents, out)

if err != nil {
panic(err)
}
if err != nil {
panic(err)
}
}

func isErrorInReadingNecessaryFile(filePath string, loadError error) bool {
if loadError != nil {
if os.IsNotExist(loadError) {
log.Printf("Required chart file %s missing. Skipping documentation for chart", filePath)
return true
} else {
log.Printf("Error occurred in reading chart file %s. Skipping documentation for chart", filePath)
return true
}
}

return false
if loadError != nil {
if os.IsNotExist(loadError) {
log.Printf("Required chart file %s missing. Skipping documentation for chart", filePath)
return true
} else {
log.Printf("Error occurred in reading chart file %s. Skipping documentation for chart", filePath)
return true
}
}

return false
}

func parseChartFile(chartDirectory string, debug bool) (ChartMeta, error) {
chartYamlPath := path.Join(chartDirectory, "Chart.yaml")
chartMeta := ChartMeta{}
yamlFileContents, err := getYamlFileContents(chartYamlPath, debug)
chartYamlPath := path.Join(chartDirectory, "Chart.yaml")
chartMeta := ChartMeta{}
yamlFileContents, err := getYamlFileContents(chartYamlPath, debug)

if isErrorInReadingNecessaryFile(chartYamlPath, err) {
return chartMeta, err
}
if isErrorInReadingNecessaryFile(chartYamlPath, err) {
return chartMeta, err
}

yamlLoadAndCheck(yamlFileContents, &chartMeta)
return chartMeta, nil
yamlLoadAndCheck(yamlFileContents, &chartMeta)
return chartMeta, nil
}

func parseChartRequirementsFile(chartDirectory string, debug bool) (ChartRequirements, error) {
requirementsPath := path.Join(chartDirectory, "requirements.yaml")
if _, err := os.Stat(requirementsPath); os.IsNotExist(err) {
return ChartRequirements{Dependencies: []ChartRequirementsItem{}}, nil
}
requirementsPath := path.Join(chartDirectory, "requirements.yaml")
if _, err := os.Stat(requirementsPath); os.IsNotExist(err) {
return ChartRequirements{Dependencies: []ChartRequirementsItem{}}, nil
}

chartRequirements := ChartRequirements{}
yamlFileContents, err := getYamlFileContents(requirementsPath, debug)
chartRequirements := ChartRequirements{}
yamlFileContents, err := getYamlFileContents(requirementsPath, debug)

if isErrorInReadingNecessaryFile(requirementsPath, err) {
return chartRequirements, err
}
if isErrorInReadingNecessaryFile(requirementsPath, err) {
return chartRequirements, err
}

yamlLoadAndCheck(yamlFileContents, &chartRequirements)
return chartRequirements, nil
yamlLoadAndCheck(yamlFileContents, &chartRequirements)
return chartRequirements, nil
}

func parseValuesFile(chartDirectory string, debug bool) (ChartValues, error) {
valuesPath := path.Join(chartDirectory, "values.yaml")
values := ChartValues{}
yamlFileContents, err := getYamlFileContents(valuesPath, debug)
valuesPath := path.Join(chartDirectory, "values.yaml")
values := ChartValues{}
yamlFileContents, err := getYamlFileContents(valuesPath, debug)

if isErrorInReadingNecessaryFile(valuesPath, err) {
return values, err
}
if isErrorInReadingNecessaryFile(valuesPath, err) {
return values, err
}

yamlLoadAndCheck(yamlFileContents, &values)
return values, nil
yamlLoadAndCheck(yamlFileContents, &values)
return values, nil
}

func parseValuesFileComments(chartDirectory string) (map[string]string, error) {
valuesPath := path.Join(chartDirectory, "values.yaml")
valuesFile, err := os.Open(valuesPath)
valuesPath := path.Join(chartDirectory, "values.yaml")
valuesFile, err := os.Open(valuesPath)

if isErrorInReadingNecessaryFile(valuesPath, err) {
return map[string]string{}, err
}
if isErrorInReadingNecessaryFile(valuesPath, err) {
return map[string]string{}, err
}

defer valuesFile.Close()
defer valuesFile.Close()

keyToDescriptions := make(map[string]string)
scanner := bufio.NewScanner(valuesFile)
keyToDescriptions := make(map[string]string)
scanner := bufio.NewScanner(valuesFile)

for scanner.Scan() {
match := valuesDescriptionRegex.FindStringSubmatch(scanner.Text())
for scanner.Scan() {
match := valuesDescriptionRegex.FindStringSubmatch(scanner.Text())

if len(match) > 2 {
keyToDescriptions[match[1]] = match[2]
}
}
if len(match) > 2 {
keyToDescriptions[match[1]] = match[2]
}
}

return keyToDescriptions, nil
return keyToDescriptions, nil
}
51 changes: 25 additions & 26 deletions cmd/helm-docs/command_line.go
Original file line number Diff line number Diff line change
@@ -1,46 +1,45 @@
package main

import (
"fmt"
"os"
flag "github.com/spf13/pflag"
"fmt"
flag "github.com/spf13/pflag"
"os"
)

type HelmDocArgs struct {
help bool
debug bool
dryRun bool
help bool
debug bool
dryRun bool
}


const debugHelp = "Print debug output"
const dryRunHelp = "Don't actually render any markdown files with docs, juts print to stdout"
const helpHelp = "Print this help menu, then exist"

func printHelp() {
fmt.Println("Usage:")
fmt.Println(" helm-doc [options]")
fmt.Println()
fmt.Println("helm-doc reads the values file of a chart in the root of your repository and creates a table of the values")
fmt.Println()
fmt.Println("Options:")
fmt.Println(fmt.Sprintf(" --debug %s", debugHelp))
fmt.Println(fmt.Sprintf(" --dry-run %s", dryRunHelp))
fmt.Println(fmt.Sprintf(" --help %s", helpHelp))
fmt.Println("Usage:")
fmt.Println(" helm-doc [options]")
fmt.Println()
fmt.Println("helm-doc reads the values file of a chart in the root of your repository and creates a table of the values")
fmt.Println()
fmt.Println("Options:")
fmt.Println(fmt.Sprintf(" --debug %s", debugHelp))
fmt.Println(fmt.Sprintf(" --dry-run %s", dryRunHelp))
fmt.Println(fmt.Sprintf(" --help %s", helpHelp))
}

func parseCommandLine() HelmDocArgs {
var args HelmDocArgs
var args HelmDocArgs

flag.BoolVar(&args.debug, "debug", false, debugHelp)
flag.BoolVar(&args.dryRun, "dry-run", false, dryRunHelp)
flag.BoolVar(&args.help, "help", false, helpHelp)
flag.Parse()
flag.BoolVar(&args.debug, "debug", false, debugHelp)
flag.BoolVar(&args.dryRun, "dry-run", false, dryRunHelp)
flag.BoolVar(&args.help, "help", false, helpHelp)
flag.Parse()

if args.help {
printHelp()
os.Exit(0)
}
if args.help {
printHelp()
os.Exit(0)
}

return args
return args
}
Loading

0 comments on commit 2140a87

Please sign in to comment.