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

mlange-42 / modo / 12877300192

20 Jan 2025 11:41PM CUT coverage: 60.111%. Remained the same
12877300192

push

github

web-flow
Extend the user guide (#84)

972 of 1617 relevant lines covered (60.11%)

28.0 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
func rootCommand() *cobra.Command {
×
25
        var cliArgs document.Config
×
26
        var renderFormat string
×
27

×
28
        root := &cobra.Command{
×
29
                Use:   "modo OUT-PATH",
×
30
                Short: "Modo -- DocGen for Mojo.",
×
31
                Long: `Modo -- DocGen for Mojo.
×
32

×
33
Modo generates Markdown for static site generators (SSGs) from 'mojo doc' JSON output.`,
×
34
                Example: `  modo docs -i docs.json        # from a file
×
35
  mojo doc ./src | modo docs    # from 'mojo doc'`,
×
36
                Args:         cobra.ExactArgs(1),
×
37
                SilenceUsage: true,
×
38
                RunE: func(cmd *cobra.Command, args []string) error {
×
39
                        cliArgs.OutputDir = args[0]
×
40
                        return run(&cliArgs, renderFormat)
×
41
                },
×
42
        }
43

44
        root.Flags().StringVarP(&cliArgs.InputFile, "input", "i", "", "'mojo doc' JSON file to process. Reads from STDIN if not specified.")
×
45
        root.Flags().StringVarP(&cliArgs.DocTests, "doctest", "d", "", "Target folder to extract doctests for 'mojo test'. (default no doctests)")
×
46
        root.Flags().StringVarP(&renderFormat, "format", "f", "plain", "Output format. One of (plain|mdbook|hugo).")
×
47
        root.Flags().BoolVarP(&cliArgs.UseExports, "exports", "e", false, "Process according to 'Exports:' sections in packages.")
×
48
        root.Flags().BoolVar(&cliArgs.ShortLinks, "short-links", false, "Render shortened link labels, stripping packages and modules.")
×
49
        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.")
×
50
        root.Flags().BoolVar(&cliArgs.Strict, "strict", false, "Strict mode. Errors instead of warnings.")
×
51
        root.Flags().BoolVar(&cliArgs.DryRun, "dry-run", false, "Dry-run without any file output.")
×
52
        root.Flags().StringSliceVarP(&cliArgs.TemplateDirs, "templates", "t", []string{}, "Optional directories with templates for (partial) overwrite.\nSee folder assets/templates in the repository.")
×
53

×
54
        root.Flags().SortFlags = false
×
55
        root.MarkFlagFilename("input", "json")
×
56
        root.MarkFlagDirname("templates")
×
57

×
58
        return root
×
59
}
60

61
func run(args *document.Config, renderFormat string) error {
×
62
        if args.OutputDir == "" {
×
63
                return fmt.Errorf("no output path given")
×
64
        }
×
65

66
        docs, err := readDocs(args.InputFile)
×
67
        if err != nil {
×
68
                return err
×
69
        }
×
70

71
        rFormat, err := format.GetFormat(renderFormat)
×
72
        if err != nil {
×
73
                return err
×
74
        }
×
75
        formatter := format.GetFormatter(rFormat)
×
76
        err = formatter.Render(docs, args)
×
77
        if err != nil {
×
78
                return err
×
79
        }
×
80

81
        return nil
×
82
}
83

84
func readDocs(file string) (*document.Docs, error) {
×
85
        data, err := read(file)
×
86
        if err != nil {
×
87
                return nil, err
×
88
        }
×
89

90
        if strings.HasSuffix(file, ".yaml") || strings.HasSuffix(file, ".yml") {
×
91
                return document.FromYaml(data)
×
92
        }
×
93

94
        return document.FromJson(data)
×
95
}
96

97
func read(file string) ([]byte, error) {
×
98
        if file == "" {
×
99
                return io.ReadAll(os.Stdin)
×
100
        } else {
×
101
                return os.ReadFile(file)
×
102
        }
×
103
}
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