• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mlange-42 / modo / 12835912779

17 Jan 2025 07:54PM CUT coverage: 57.991% (+0.06%) from 57.927%
12835912779

Pull #58

github

web-flow
Merge 0eafdfbe6 into 45ab35eb0
Pull Request #58: Tweak CLI help, improve code structure

13 of 33 new or added lines in 6 files covered. (39.39%)

762 of 1314 relevant lines covered (57.99%)

6.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/main.go
1
package main
2

3
import (
4
        "fmt"
5
        "io"
6
        "os"
7
        "strings"
8

9
        "github.com/mlange-42/modo/document"
10
        "github.com/mlange-42/modo/format"
11
        "github.com/spf13/cobra"
12
)
13

14
func main() {
×
15
        if err := rootCommand().Execute(); err != nil {
×
16
                fmt.Println("Use 'modo --help' for help.")
×
17
                os.Exit(1)
×
18
        }
×
19
}
20

21
type args struct {
22
        file            string
23
        renderFormat    string
24
        caseInsensitive bool
25
        useExports      bool
26
        shortLinks      bool
27
        outDir          string
28
        templateDirs    []string
29
}
30

31
func rootCommand() *cobra.Command {
×
32
        var cliArgs args
×
33

×
34
        root := &cobra.Command{
×
35
                Use:   "modo OUT-PATH",
×
36
                Short: "Modo -- DocGen for Mojo.",
×
37
                Long: `Modo -- DocGen for Mojo.
×
38

×
NEW
39
Modo generates Markdown for static site generators (SSGs) from 'mojo doc' JSON output.`,
×
NEW
40
                Example: `  modo docs -i docs.json        # from a file
×
NEW
41
  mojo doc ./src | modo docs    # from 'mojo doc'`,
×
42
                Args:         cobra.ExactArgs(1),
×
43
                SilenceUsage: true,
×
44
                RunE: func(cmd *cobra.Command, args []string) error {
×
45
                        cliArgs.outDir = args[0]
×
46
                        return run(&cliArgs)
×
47
                },
×
48
        }
49

50
        root.Flags().StringVarP(&cliArgs.file, "input", "i", "", "'mojo doc' JSON file to process. Reads from STDIN if not specified.")
×
51
        root.Flags().StringVarP(&cliArgs.renderFormat, "format", "f", "plain", "Output format. One of (plain|mdbook|hugo).")
×
52
        root.Flags().BoolVarP(&cliArgs.useExports, "exports", "e", false, "Process according to 'Exports:' sections in packages.")
×
53
        root.Flags().BoolVar(&cliArgs.shortLinks, "short-links", false, "Render shortened link labels, stripping packages and modules.")
×
54
        root.Flags().BoolVar(&cliArgs.caseInsensitive, "case-insensitive", false, "Build for systems that are not case-sensitive regarding file names.\nAppends hyphen (-) to capitalized file names.")
×
55
        root.Flags().StringSliceVarP(&cliArgs.templateDirs, "templates", "t", []string{}, "Optional directories with templates for (partial) overwrite.\nSee folder assets/templates in the repository.")
×
56

×
57
        root.Flags().SortFlags = false
×
58
        root.MarkFlagFilename("input", "json")
×
59
        root.MarkFlagDirname("templates")
×
60

×
61
        return root
×
62
}
63

64
func run(args *args) error {
×
65
        if args.outDir == "" {
×
66
                return fmt.Errorf("no output path given")
×
67
        }
×
68

69
        docs, err := readDocs(args.file)
×
70
        if err != nil {
×
71
                return err
×
72
        }
×
73

74
        rFormat, err := format.GetFormat(args.renderFormat)
×
75
        if err != nil {
×
76
                return err
×
77
        }
×
78
        formatter := format.GetFormatter(rFormat)
×
NEW
79
        err = formatter.Render(docs, &document.Config{
×
80
                OutputDir:     args.outDir,
×
81
                TemplateDirs:  args.templateDirs,
×
82
                UseExports:    args.useExports,
×
83
                ShortLinks:    args.shortLinks,
×
84
                CaseSensitive: !args.caseInsensitive,
×
85
        })
×
86
        if err != nil {
×
87
                return err
×
88
        }
×
89

90
        return nil
×
91
}
92

93
func readDocs(file string) (*document.Docs, error) {
×
94
        data, err := read(file)
×
95
        if err != nil {
×
96
                return nil, err
×
97
        }
×
98

99
        if strings.HasSuffix(file, ".yaml") || strings.HasSuffix(file, ".yml") {
×
100
                return document.FromYaml(data)
×
101
        }
×
102

103
        return document.FromJson(data)
×
104
}
105

106
func read(file string) ([]byte, error) {
×
107
        if file == "" {
×
108
                return io.ReadAll(os.Stdin)
×
109
        } else {
×
110
                return os.ReadFile(file)
×
111
        }
×
112
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc