|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "github.com/random-dwi/helm-doc/generator" |
| 7 | + "github.com/random-dwi/helm-doc/helm" |
| 8 | + "github.com/random-dwi/helm-doc/output" |
| 9 | + "github.com/random-dwi/helm-doc/writer" |
| 10 | + "github.com/spf13/cobra" |
| 11 | + "k8s.io/helm/pkg/chartutil" |
| 12 | + "log" |
| 13 | + "os" |
| 14 | +) |
| 15 | + |
| 16 | +var version string |
| 17 | +var buildTime string |
| 18 | +var gitCommit string |
| 19 | + |
| 20 | +var rootCmd = &cobra.Command{ |
| 21 | + Use: "doc [flags] CHART", |
| 22 | + Short: fmt.Sprintf("generate doc for a helm chart"), |
| 23 | + Long: fmt.Sprintf("helm plugin to generate documentation for helm charts.\nversion: %s buildTime: %s gitCommit: %s", version, buildTime, gitCommit), |
| 24 | + RunE: run, |
| 25 | +} |
| 26 | + |
| 27 | +var flags generator.CommandFlags |
| 28 | + |
| 29 | +func HelmDocCommand(streams output.IOStreams) *cobra.Command { |
| 30 | + rootCmd.SetOutput(streams.Out) |
| 31 | + return rootCmd |
| 32 | +} |
| 33 | + |
| 34 | +func defaultKeyring() string { |
| 35 | + return os.ExpandEnv("$HOME/.gnupg/pubring.gpg") |
| 36 | +} |
| 37 | + |
| 38 | +func init() { |
| 39 | + |
| 40 | + f := rootCmd.Flags() |
| 41 | + f.BoolVarP(&flags.VerifyExamples, "verify-examples", "", true, "verify presence of examples for configs without default value") |
| 42 | + f.BoolVarP(&flags.VerifyValues, "verify-values", "", true, "verify all default values are documented") |
| 43 | + f.BoolVarP(&flags.VerifyDependencies, "verify-dependencies", "", false, "verify dependencies are documented") |
| 44 | + f.StringVar(&flags.Version, "version", "", "Specify the exact chart version to use. If this is not specified, the latest version is used") |
| 45 | + f.StringVar(&flags.RepoURL, "repo", "", "Chart repository url where to locate the requested chart") |
| 46 | + f.StringVar(&flags.Username, "username", "", "Chart repository username where to locate the requested chart") |
| 47 | + f.StringVar(&flags.Password, "password", "", "Chart repository password where to locate the requested chart") |
| 48 | + f.StringVar(&flags.Keyring, "keyring", defaultKeyring(), "Location of public keys used for verification") |
| 49 | + f.StringVar(&flags.CertFile, "cert-file", "", "Identify HTTPS client using this SSL certificate file") |
| 50 | + f.StringVar(&flags.KeyFile, "key-file", "", "Identify HTTPS client using this SSL key file") |
| 51 | + f.StringVar(&flags.CaFile, "ca-file", "", "Verify certificates of HTTPS-enabled servers using this CA bundle") |
| 52 | + f.BoolVar(&flags.Verify, "verify", false, "Verify the package before using it") |
| 53 | + f.BoolVar(&flags.Devel, "devel", false, "Use development versions, too. Equivalent to version '>0.0.0-0'. If --version is set, this is ignored.") |
| 54 | + |
| 55 | + if os.Getenv("HELM_DEBUG") == "1" { |
| 56 | + flags.Verbose = true |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func run(cmd *cobra.Command, args []string) error { |
| 61 | + if len(args) < 1 { |
| 62 | + return errors.New("chart is required") |
| 63 | + } |
| 64 | + |
| 65 | + if flags.Verbose { |
| 66 | + output.DebugLogger = log.New(os.Stderr, "[doc] ", log.LstdFlags) |
| 67 | + } |
| 68 | + |
| 69 | + output.Debugf("helm home: %s", os.Getenv("HELM_HOME")) |
| 70 | + |
| 71 | + output.Debugf("Original chart version: %q", flags.Version) |
| 72 | + if flags.Version == "" && flags.Devel { |
| 73 | + output.Debugf("setting version to >0.0.0-0") |
| 74 | + flags.Version = ">0.0.0-0" |
| 75 | + } |
| 76 | + |
| 77 | + cp, err := helm.LocateChartPath(flags.RepoURL, flags.Username, flags.Password, args[0], flags.Version, flags.Verify, flags.Keyring, |
| 78 | + flags.CertFile, flags.KeyFile, flags.CaFile) |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + var chartPath = cp |
| 83 | + |
| 84 | + output.Debugf("ChartPath is: %s", chartPath) |
| 85 | + |
| 86 | + c, err := chartutil.Load(chartPath) |
| 87 | + if err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + |
| 91 | + var gen writer.DocumentationWriter = writer.NewMarkdownWriter(os.Stdout) |
| 92 | + |
| 93 | + var dependencyNames []string |
| 94 | + |
| 95 | + for _, dependency := range c.Dependencies { |
| 96 | + dependencyNames = append(dependencyNames, dependency.Metadata.Name) |
| 97 | + } |
| 98 | + |
| 99 | + output.Debugf("generating docs for %s:%s", c.Metadata.Name, c.Metadata.Version) |
| 100 | + docs, err := generator.GenerateDocs(c, dependencyNames, flags) |
| 101 | + |
| 102 | + gen.WriteMetaData(c.Metadata, 1) |
| 103 | + gen.WriteDocs(docs) |
| 104 | + |
| 105 | + if len(c.Dependencies) > 0 { |
| 106 | + gen.WriteChapter("Dependencies", 2) |
| 107 | + } |
| 108 | + |
| 109 | + for _, dependency := range c.Dependencies { |
| 110 | + output.Debugf("generating docs for %s:%s", dependency.Metadata.Name, dependency.Metadata.Version) |
| 111 | + docs, err := generator.GenerateDocs(dependency, nil, flags) |
| 112 | + |
| 113 | + if err != nil { |
| 114 | + if flags.VerifyDependencies { |
| 115 | + output.Failf("%v", err) |
| 116 | + } else { |
| 117 | + output.Warnf("%v", err) |
| 118 | + } |
| 119 | + } else { |
| 120 | + gen.WriteMetaData(dependency.Metadata, 3) |
| 121 | + gen.WriteDocs(docs) |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return nil |
| 126 | +} |
0 commit comments