2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-05-11 03:20:37 -04:00
|
|
|
// Clidocgen generates a Markdown page describing all CLI commands.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
|
|
|
|
"github.com/edgelesssys/constellation/cli/cmd"
|
2022-05-24 05:56:40 -04:00
|
|
|
"github.com/spf13/cobra"
|
2022-05-11 03:20:37 -04:00
|
|
|
"github.com/spf13/cobra/doc"
|
|
|
|
)
|
|
|
|
|
2022-05-24 04:04:42 -04:00
|
|
|
var seeAlsoRegexp = regexp.MustCompile(`(?s)### SEE ALSO\n.+?\n\n`)
|
|
|
|
|
2022-05-11 03:20:37 -04:00
|
|
|
func main() {
|
2022-05-24 05:56:40 -04:00
|
|
|
cobra.EnableCommandSorting = false
|
2022-05-11 03:20:37 -04:00
|
|
|
rootCmd := cmd.NewRootCmd()
|
|
|
|
rootCmd.DisableAutoGenTag = true
|
|
|
|
|
|
|
|
// Generate Markdown for all commands.
|
|
|
|
cmdList := &bytes.Buffer{}
|
|
|
|
body := &bytes.Buffer{}
|
2022-08-17 10:58:36 -04:00
|
|
|
for _, c := range allSubCommands(rootCmd) {
|
2022-05-11 03:20:37 -04:00
|
|
|
name := c.Name()
|
2022-09-05 04:34:46 -04:00
|
|
|
fullName, level := determineFullNameAndLevel(c)
|
|
|
|
|
|
|
|
// First two arguments are used to create indentation for nested commands (2 spaces per level).
|
|
|
|
fmt.Fprintf(cmdList, "%*s* [%v](#constellation-%v): %v\n", 2*level, "", name, fullName, c.Short)
|
2022-05-11 03:20:37 -04:00
|
|
|
if err := doc.GenMarkdown(c, body); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove "see also" sections. They list parent and child commands, which is not interesting for us.
|
2022-05-24 04:04:42 -04:00
|
|
|
cleanedBody := seeAlsoRegexp.ReplaceAll(body.Bytes(), nil)
|
2022-05-11 03:20:37 -04:00
|
|
|
|
|
|
|
fmt.Printf("Commands:\n\n%s\n%s", cmdList, cleanedBody)
|
|
|
|
}
|
2022-08-17 10:58:36 -04:00
|
|
|
|
|
|
|
func allSubCommands(cmd *cobra.Command) []*cobra.Command {
|
|
|
|
var all []*cobra.Command
|
|
|
|
for _, c := range cmd.Commands() {
|
|
|
|
all = append(all, c)
|
|
|
|
all = append(all, allSubCommands(c)...)
|
|
|
|
}
|
|
|
|
return all
|
|
|
|
}
|
2022-09-05 04:34:46 -04:00
|
|
|
|
|
|
|
func determineFullNameAndLevel(cmd *cobra.Command) (string, int) {
|
|
|
|
// Traverse the command tree upwards and determine the full name and level of the command.
|
|
|
|
name := cmd.Name()
|
|
|
|
level := 0
|
|
|
|
for cmd.HasParent() && cmd.Parent().Name() != "constellation" {
|
|
|
|
cmd = cmd.Parent()
|
|
|
|
name = cmd.Name() + "-" + name // Use '-' as separator since we pipe it into a Markdown link.
|
|
|
|
level++
|
|
|
|
}
|
|
|
|
return name, level
|
|
|
|
}
|