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

mlange-42 / modo / 12821068519

17 Jan 2025 01:47AM CUT coverage: 36.563%. Remained the same
12821068519

Pull #53

github

web-flow
Merge 0bfc5ef9a into bc43036f2
Pull Request #53: Add CLI help to README

468 of 1280 relevant lines covered (36.56%)

2.63 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
        if args.caseInsensitive {
×
82
                document.CaseSensitiveSystem = false
×
83
        }
×
84

85
        err = format.Render(docs, args.outDir, args.templateDirs, rFormat, args.useExports, args.shortLinks)
×
86
        if err != nil {
×
87
                return err
×
88
        }
×
89

90
        return nil
×
91
}
92

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

99
        if strings.HasSuffix(file, ".yaml") || strings.HasSuffix(file, ".yml") {
×
100
                return document.FromYaml(data)
×
101
        }
×
102

103
        return document.FromJson(data)
×
104
}
105

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