|
| 1 | +/* |
| 2 | +Copyright The ORAS Authors. |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +package root |
| 17 | + |
| 18 | +import ( |
| 19 | + "fmt" |
| 20 | + "log" |
| 21 | + "path/filepath" |
| 22 | + "strings" |
| 23 | + |
| 24 | + "github.com/spf13/cobra" |
| 25 | + "github.com/spf13/cobra/doc" |
| 26 | +) |
| 27 | + |
| 28 | +const docsDesc = ` |
| 29 | +Generate documentation files for ORAS. |
| 30 | +
|
| 31 | +This command can generate documentation for ORAS in the following formats: |
| 32 | +
|
| 33 | +- Markdown |
| 34 | +- Man pages |
| 35 | +- Bash autocompletions |
| 36 | +` |
| 37 | + |
| 38 | +type docsOptions struct { |
| 39 | + dest string |
| 40 | + docTypeString string |
| 41 | + topCmd *cobra.Command |
| 42 | + generateHeaders bool |
| 43 | +} |
| 44 | + |
| 45 | +func docsCmd() *cobra.Command { |
| 46 | + o := &docsOptions{} |
| 47 | + |
| 48 | + cmd := &cobra.Command{ |
| 49 | + Use: "docs", |
| 50 | + Short: "Generate documentation for ORAS", |
| 51 | + Long: docsDesc, |
| 52 | + Hidden: true, |
| 53 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 54 | + o.topCmd = cmd.Root() |
| 55 | + return o.run() |
| 56 | + }, |
| 57 | + } |
| 58 | + |
| 59 | + f := cmd.Flags() |
| 60 | + f.StringVar(&o.dest, "dir", "./", "directory to which documentation is written") |
| 61 | + f.StringVar(&o.docTypeString, "type", "markdown", "the type of documentation to generate (markdown, man, bash)") |
| 62 | + f.BoolVar(&o.generateHeaders, "generate-headers", false, "generate standard headers for markdown files") |
| 63 | + |
| 64 | + _ = cmd.RegisterFlagCompletionFunc("type", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 65 | + return []string{"bash", "man", "markdown"}, cobra.ShellCompDirectiveDefault |
| 66 | + }) |
| 67 | + |
| 68 | + return cmd |
| 69 | +} |
| 70 | + |
| 71 | +func (o *docsOptions) run() error { |
| 72 | + switch o.docTypeString { |
| 73 | + case "markdown", "mdown", "md": |
| 74 | + if o.generateHeaders { |
| 75 | + standardLinks := func(s string) string { return s } |
| 76 | + |
| 77 | + hdrFunc := func(filename string) string { |
| 78 | + base := filepath.Base(filename) |
| 79 | + name := strings.TrimSuffix(base, filepath.Ext(base)) |
| 80 | + title := strings.ReplaceAll(name, "_", " ") |
| 81 | + // Capitalize first letter of each word |
| 82 | + words := strings.Fields(title) |
| 83 | + for i, word := range words { |
| 84 | + if len(word) > 0 { |
| 85 | + words[i] = strings.ToUpper(word[:1]) + word[1:] |
| 86 | + } |
| 87 | + } |
| 88 | + title = strings.Join(words, " ") |
| 89 | + return fmt.Sprintf("---\ntitle: \"%s\"\n---\n", title) |
| 90 | + } |
| 91 | + |
| 92 | + if err := doc.GenMarkdownTreeCustom(o.topCmd, o.dest, hdrFunc, standardLinks); err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + } else { |
| 96 | + if err := doc.GenMarkdownTree(o.topCmd, o.dest); err != nil { |
| 97 | + return err |
| 98 | + } |
| 99 | + } |
| 100 | + case "man": |
| 101 | + header := &doc.GenManHeader{ |
| 102 | + Title: "ORAS", |
| 103 | + Section: "1", |
| 104 | + } |
| 105 | + if err := doc.GenManTree(o.topCmd, header, o.dest); err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + case "bash": |
| 109 | + completionFile := filepath.Join(o.dest, "completions.bash") |
| 110 | + if err := o.topCmd.GenBashCompletionFile(completionFile); err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + default: |
| 114 | + return fmt.Errorf("unknown doc type %q. Try 'markdown' or 'man'", o.docTypeString) |
| 115 | + } |
| 116 | + |
| 117 | + log.Printf("Documentation successfully written to %s\n", o.dest) |
| 118 | + |
| 119 | + return nil |
| 120 | +} |
0 commit comments