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

mlange-42 / modo / 12831974413

17 Jan 2025 03:26PM CUT coverage: 57.927%. Remained the same
12831974413

push

github

web-flow
Update CHANGELOG for v0.6.0 release (#57)

749 of 1293 relevant lines covered (57.93%)

6.98 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
        }
×
81
        formatter := format.GetFormatter(rFormat)
×
82

×
83
        err = document.Render(docs, &document.Config{
×
84
                OutputDir:     args.outDir,
×
85
                TemplateDirs:  args.templateDirs,
×
86
                RenderFormat:  formatter,
×
87
                UseExports:    args.useExports,
×
88
                ShortLinks:    args.shortLinks,
×
89
                CaseSensitive: !args.caseInsensitive,
×
90
        })
×
91
        if err != nil {
×
92
                return err
×
93
        }
×
94

95
        return nil
×
96
}
97

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

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

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

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