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

mlange-42 / modo / 12856315602

19 Jan 2025 06:58PM CUT coverage: 59.246% (+0.3%) from 58.921%
12856315602

push

github

web-flow
Allow cross-refs to aliases in modules and structs (#69)

14 of 16 new or added lines in 3 files covered. (87.5%)

833 of 1406 relevant lines covered (59.25%)

7.94 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
        "time"
9

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

15
func main() {
×
16
        start := time.Now()
×
17
        if err := rootCommand().Execute(); err != nil {
×
18
                fmt.Println("Use 'modo --help' for help.")
×
19
                os.Exit(1)
×
20
        }
×
21
        fmt.Printf("Completed in %.1fms 🧯\n", float64(time.Since(start).Microseconds())/1000.0)
×
22
}
23

24
type args struct {
25
        file            string
26
        renderFormat    string
27
        caseInsensitive bool
28
        useExports      bool
29
        shortLinks      bool
30
        strict          bool
31
        outDir          string
32
        templateDirs    []string
33
}
34

35
func rootCommand() *cobra.Command {
×
36
        var cliArgs args
×
37

×
38
        root := &cobra.Command{
×
39
                Use:   "modo OUT-PATH",
×
40
                Short: "Modo -- DocGen for Mojo.",
×
41
                Long: `Modo -- DocGen for Mojo.
×
42

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

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

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

×
66
        return root
×
67
}
68

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

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

79
        rFormat, err := format.GetFormat(args.renderFormat)
×
80
        if err != nil {
×
81
                return err
×
82
        }
×
83
        formatter := format.GetFormatter(rFormat)
×
84
        err = formatter.Render(docs, &document.Config{
×
85
                OutputDir:     args.outDir,
×
86
                TemplateDirs:  args.templateDirs,
×
87
                UseExports:    args.useExports,
×
88
                ShortLinks:    args.shortLinks,
×
89
                CaseSensitive: !args.caseInsensitive,
×
90
                Strict:        args.strict,
×
91
        })
×
92
        if err != nil {
×
93
                return err
×
94
        }
×
95

96
        return nil
×
97
}
98

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

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

109
        return document.FromJson(data)
×
110
}
111

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