|
| 1 | +/* |
| 2 | + Copyright The containerd Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package climan |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "io/ioutil" |
| 22 | + "path/filepath" |
| 23 | + |
| 24 | + "github.com/pkg/errors" |
| 25 | + "github.com/urfave/cli" |
| 26 | +) |
| 27 | + |
| 28 | +var Command = cli.Command{ |
| 29 | + Name: "gen-man", |
| 30 | + Usage: "generate man pages for the cli application", |
| 31 | + Hidden: true, |
| 32 | + Flags: []cli.Flag{ |
| 33 | + cli.StringFlag{ |
| 34 | + Name: "format,f", |
| 35 | + Usage: "specify the format in (md:man)", |
| 36 | + Value: "md", |
| 37 | + }, |
| 38 | + cli.IntFlag{ |
| 39 | + Name: "section,s", |
| 40 | + Usage: "section of the man pages", |
| 41 | + Value: 1, |
| 42 | + }, |
| 43 | + }, |
| 44 | + Action: func(clix *cli.Context) (err error) { |
| 45 | + // clear out the usage as we use banners that do not display in man pages |
| 46 | + clix.App.Usage = "" |
| 47 | + dir := clix.Args().First() |
| 48 | + if dir == "" { |
| 49 | + return errors.New("directory argument is required") |
| 50 | + } |
| 51 | + var ( |
| 52 | + data string |
| 53 | + ext string |
| 54 | + ) |
| 55 | + switch clix.String("format") { |
| 56 | + case "man": |
| 57 | + data, err = clix.App.ToMan() |
| 58 | + default: |
| 59 | + data, err = clix.App.ToMarkdown() |
| 60 | + ext = "md" |
| 61 | + } |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + return ioutil.WriteFile(filepath.Join(dir, formatFilename(clix, clix.Int("section"), ext)), []byte(data), 0644) |
| 66 | + }, |
| 67 | +} |
| 68 | + |
| 69 | +func formatFilename(clix *cli.Context, section int, ext string) string { |
| 70 | + s := fmt.Sprintf("%s.%d", clix.App.Name, section) |
| 71 | + if ext != "" { |
| 72 | + s += "." + ext |
| 73 | + } |
| 74 | + return s |
| 75 | +} |
0 commit comments