From 8ea172717d70e843e16d0edb541919a11eb2800f Mon Sep 17 00:00:00 2001 From: Kristoffer Ahl Date: Sat, 11 Sep 2021 12:54:43 +0200 Subject: [PATCH] Added internal command for generating markdown documentation. --- cmd/centry/commands.go | 8 +++++ cmd/centry/docs.go | 81 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 cmd/centry/docs.go diff --git a/cmd/centry/commands.go b/cmd/centry/commands.go index 0a6b12a..f8cf320 100644 --- a/cmd/centry/commands.go +++ b/cmd/centry/commands.go @@ -30,6 +30,13 @@ func registerInternalCommands(runtime *Runtime) { "command": "serve", }), } + generateMarkdownCmd := &GenerateMarkdownCommand{ + CLI: runtime.cli, + Manifest: context.manifest, + Log: context.log.GetLogger().WithFields(logrus.Fields{ + "command": "generate-markdown", + }), + } internalCmd := withCommandDefaults(&cli.Command{ Name: "internal", Usage: "Internal centry commands", @@ -37,6 +44,7 @@ func registerInternalCommands(runtime *Runtime) { Hidden: context.manifest.Config.HideInternalCommands, Subcommands: []*cli.Command{ serveCmd.ToCLICommand(), + generateMarkdownCmd.ToCLICommand(), }, }) runtime.cli.Commands = append(runtime.cli.Commands, internalCmd) diff --git a/cmd/centry/docs.go b/cmd/centry/docs.go new file mode 100644 index 0000000..026b1fd --- /dev/null +++ b/cmd/centry/docs.go @@ -0,0 +1,81 @@ +package main + +import ( + "bufio" + "fmt" + "os" + + "github.com/kristofferahl/go-centry/internal/pkg/config" + "github.com/sirupsen/logrus" + "github.com/urfave/cli/v2" +) + +// GenerateMarkdownCommand is a Command implementation that generates markdown documentation +type GenerateMarkdownCommand struct { + CLI *cli.App + Manifest *config.Manifest + Log *logrus.Entry +} + +// ToCLICommand returns a CLI command +func (sc *GenerateMarkdownCommand) ToCLICommand() *cli.Command { + return withCommandDefaults(&cli.Command{ + Name: "generate-markdown", + Usage: "Generate markdown documentation", + UsageText: "", + Hidden: false, + Action: func(c *cli.Context) error { + ec := sc.Run(c.Path("file")) + if ec > 0 { + return cli.Exit("failed to generate markdown documentation", ec) + } + return nil + }, + Flags: []cli.Flag{ + &cli.PathFlag{ + Name: "file", + }, + }, + }) +} + +// Run generates markdown documentation +func (sc *GenerateMarkdownCommand) Run(path string) int { + sc.Log.Debugf("generating markdown documenation") + + md, err := sc.CLI.ToMarkdown() + if err != nil { + sc.Log.Error(err) + return 1 + } + + if path == "" { + fmt.Print(md) + sc.Log.Debugf("generated markdown documenation to stdout") + } else { + file, err := os.Create(path) + if err != nil { + sc.Log.Error(err) + return 1 + } + defer file.Close() + + w := bufio.NewWriter(file) + bc, err := w.WriteString(md) + if err != nil { + sc.Log.Error(err) + return 1 + } + sc.Log.Tracef("wrote %d bytes", bc) + + err = w.Flush() + if err != nil { + sc.Log.Error(err) + return 1 + } + + sc.Log.Infof("generated markdown documenation to file (path=%s)", path) + } + + return 0 +}