-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.go
71 lines (60 loc) · 1.67 KB
/
index.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package sub
import (
"fmt"
"github.com/go-spectest/markdown"
"github.com/spf13/cobra"
)
// newIndexCmd return index command.
func newIndexCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "index",
Short: "Generate an index for a directory full of markdown files",
RunE: index,
Example: " spectest index TARGET_DIR",
}
cmd.Flags().StringP("title", "t", "", "title of index markdown file")
cmd.Flags().StringSliceP("desc", "d", []string{}, "description of index markdown file")
return cmd
}
// indexer is a struct for index command.
// It has a target directory and a title of index.
type indexer struct {
// target is a target directory.
target string
// title is a title of index.
title string
// description is a description of index.
description []string
}
// newIndexer return indexer.
func newIndexer(cmd *cobra.Command, args []string) (*indexer, error) {
title, err := cmd.Flags().GetString("title")
if err != nil {
return nil, err
}
description, err := cmd.Flags().GetStringSlice("desc")
if err != nil {
return nil, err
}
target := "."
if len(args) > 0 {
target = args[0]
}
return &indexer{
target: target,
title: title,
description: description,
}, nil
}
// run generate an index for a directory full of markdown files.
func (i *indexer) run() error {
return markdown.GenerateIndex(i.target, markdown.WithTitle(i.title), markdown.WithDescription(i.description))
}
// index generate an index for a directory full of markdown files.
func index(cmd *cobra.Command, args []string) error {
i, err := newIndexer(cmd, args)
if err != nil {
return fmt.Errorf("failed to initialize index command: %w", err)
}
return i.run()
}