-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added internal command for generating markdown documentation.
- Loading branch information
1 parent
d8e08af
commit 8ea1727
Showing
2 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |