docs: add external docs links support

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
Tonis Tiigi
2021-03-23 11:07:57 -07:00
committed by Sebastiaan van Stijn
parent 363c0fdf4b
commit 4047bccf6c
3 changed files with 25 additions and 13 deletions

View File

@ -90,8 +90,18 @@ func genCmd(cmd *cobra.Command, dir string) error {
return nil
}
func makeLink(txt, link string) string {
return "[" + txt + "](#" + link + ")"
func makeLink(txt, link string, f *pflag.Flag, isAnchor bool) string {
link = "#" + link
annotations, ok := f.Annotations["docs.external.url"]
if ok && len(annotations) > 0 {
link = annotations[0]
} else {
if !isAnchor {
return txt
}
}
return "[" + txt + "](" + link + ")"
}
func cmdOutput(cmd *cobra.Command, old string) (string, error) {
@ -140,9 +150,7 @@ func cmdOutput(cmd *cobra.Command, old string) (string, error) {
fmt.Fprint(b, "| ")
if f.Shorthand != "" {
name := "`-" + f.Shorthand + "`"
if isLink {
name = makeLink(name, f.Name)
}
name = makeLink(name, f.Name, f, isLink)
fmt.Fprintf(b, "%s, ", name)
}
name := "`--" + f.Name
@ -150,9 +158,7 @@ func cmdOutput(cmd *cobra.Command, old string) (string, error) {
name += " " + f.Value.Type()
}
name += "`"
if isLink {
name = makeLink(name, f.Name)
}
name = makeLink(name, f.Name, f, isLink)
fmt.Fprintf(b, "%s | %s |\n", name, f.Usage)
})
fmt.Fprintln(b, "")