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

mlange-42 / modo / 12829003556

17 Jan 2025 12:24PM CUT coverage: 56.552% (+20.0%) from 36.563%
12829003556

Pull #54

github

web-flow
Merge 1a1b11cc2 into 41ff6f833
Pull Request #54: More unit tests

32 of 49 new or added lines in 4 files covered. (65.31%)

13 existing lines in 2 files now uncovered.

725 of 1282 relevant lines covered (56.55%)

6.69 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

×
39
Modo generates Markdown for static site generators (SSGs) from 'mojo doc' JSON output.
×
40

×
41
Usage:
×
42
  modo docs -i docs.json        # from a file
×
43
  mojo doc ./src | modo docs    # from 'mojo doc'
×
44
`,
×
45
                Args:         cobra.ExactArgs(1),
×
46
                SilenceUsage: true,
×
47
                RunE: func(cmd *cobra.Command, args []string) error {
×
48
                        cliArgs.outDir = args[0]
×
49
                        return run(&cliArgs)
×
50
                },
×
51
        }
52

53
        root.Flags().StringVarP(&cliArgs.file, "input", "i", "", "'mojo doc' JSON file to process. Reads from STDIN if not specified.")
×
54
        root.Flags().StringVarP(&cliArgs.renderFormat, "format", "f", "plain", "Output format. One of (plain|mdbook|hugo).")
×
55
        root.Flags().BoolVarP(&cliArgs.useExports, "exports", "e", false, "Process according to 'Exports:' sections in packages.")
×
56
        root.Flags().BoolVar(&cliArgs.shortLinks, "short-links", false, "Render shortened link labels, stripping packages and modules.")
×
57
        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.")
×
58
        root.Flags().StringSliceVarP(&cliArgs.templateDirs, "templates", "t", []string{}, "Optional directories with templates for (partial) overwrite.\nSee folder assets/templates in the repository.")
×
59

×
60
        root.Flags().SortFlags = false
×
61
        root.MarkFlagFilename("input", "json")
×
62
        root.MarkFlagDirname("templates")
×
63

×
64
        return root
×
65
}
66

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

72
        docs, err := readDocs(args.file)
×
73
        if err != nil {
×
74
                return err
×
75
        }
×
76

77
        rFormat, err := format.GetFormat(args.renderFormat)
×
78
        if err != nil {
×
79
                return err
×
80
        }
×
NEW
81
        formatter := format.GetFormatter(rFormat)
×
NEW
82

×
83
        if args.caseInsensitive {
×
84
                document.CaseSensitiveSystem = false
×
85
        }
×
86

NEW
87
        err = document.Render(docs, &document.Config{
×
NEW
88
                OutputDir:    args.outDir,
×
NEW
89
                TemplateDirs: args.templateDirs,
×
NEW
90
                RenderFormat: formatter,
×
NEW
91
                UseExports:   args.useExports,
×
NEW
92
                ShortLinks:   args.shortLinks,
×
NEW
93
        })
×
94
        if err != nil {
×
95
                return err
×
96
        }
×
97

98
        return nil
×
99
}
100

101
func readDocs(file string) (*document.Docs, error) {
×
102
        data, err := read(file)
×
103
        if err != nil {
×
104
                return nil, err
×
105
        }
×
106

107
        if strings.HasSuffix(file, ".yaml") || strings.HasSuffix(file, ".yml") {
×
108
                return document.FromYaml(data)
×
109
        }
×
110

111
        return document.FromJson(data)
×
112
}
113

114
func read(file string) ([]byte, error) {
×
115
        if file == "" {
×
116
                return io.ReadAll(os.Stdin)
×
117
        } else {
×
118
                return os.ReadFile(file)
×
119
        }
×
120
}
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