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

mlange-42 / modo / 12849298326

19 Jan 2025 01:48AM CUT coverage: 58.946% (-0.09%) from 59.033%
12849298326

push

github

web-flow
Print completed message with processing duration (#67)

0 of 2 new or added lines in 1 file covered. (0.0%)

794 of 1347 relevant lines covered (58.95%)

7.44 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() {
×
NEW
16
        start := time.Now()
×
17
        if err := rootCommand().Execute(); err != nil {
×
18
                fmt.Println("Use 'modo --help' for help.")
×
19
                os.Exit(1)
×
20
        }
×
NEW
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
        outDir          string
31
        templateDirs    []string
32
}
33

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

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

×
42
Modo generates Markdown for static site generators (SSGs) from 'mojo doc' JSON output.`,
×
43
                Example: `  modo docs -i docs.json        # from a file
×
44
  mojo doc ./src | modo docs    # from 'mojo doc'`,
×
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
        }
×
81
        formatter := format.GetFormatter(rFormat)
×
82
        err = formatter.Render(docs, &document.Config{
×
83
                OutputDir:     args.outDir,
×
84
                TemplateDirs:  args.templateDirs,
×
85
                UseExports:    args.useExports,
×
86
                ShortLinks:    args.shortLinks,
×
87
                CaseSensitive: !args.caseInsensitive,
×
88
        })
×
89
        if err != nil {
×
90
                return err
×
91
        }
×
92

93
        return nil
×
94
}
95

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

102
        if strings.HasSuffix(file, ".yaml") || strings.HasSuffix(file, ".yml") {
×
103
                return document.FromYaml(data)
×
104
        }
×
105

106
        return document.FromJson(data)
×
107
}
108

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