Skip to content

Commit

Permalink
Added internal command for generating markdown documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
kristofferahl committed Sep 11, 2021
1 parent d8e08af commit 8ea1727
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
8 changes: 8 additions & 0 deletions cmd/centry/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,21 @@ 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",
UsageText: "",
Hidden: context.manifest.Config.HideInternalCommands,
Subcommands: []*cli.Command{
serveCmd.ToCLICommand(),
generateMarkdownCmd.ToCLICommand(),
},
})
runtime.cli.Commands = append(runtime.cli.Commands, internalCmd)
Expand Down
81 changes: 81 additions & 0 deletions cmd/centry/docs.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 8ea1727

Please sign in to comment.