-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
148 lines (127 loc) · 3.03 KB
/
main.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/urfave/cli/v2"
)
func main() {
app := &cli.App{
Name: "wmdfmt",
Usage: "Format Markdown files",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "in-place",
Aliases: []string{"i"},
Usage: "Format file(s) in-place",
},
&cli.BoolFlag{
Name: "recursive",
Aliases: []string{"r"},
Usage: "Recursively format files in subdirectories",
},
&cli.StringFlag{
Name: "ignore",
Usage: "Ignore files matching this pattern",
},
},
Action: run,
}
err := app.Run(os.Args)
if err != nil {
if showErr := cli.ShowAppHelp(cli.NewContext(app, nil, nil)); showErr != nil {
os.Exit(1)
}
_, _ = fmt.Fprintf(os.Stderr, "\nError: %v\n", err)
os.Exit(1)
}
}
func run(c *cli.Context) error {
inPlace := c.Bool("in-place")
recursive := c.Bool("recursive")
ignorePattern := c.String("ignore")
if c.NArg() == 0 {
// No arguments, read from stdin
return processStdin()
}
for _, path := range c.Args().Slice() {
err := processPath(path, inPlace, recursive, ignorePattern)
if err != nil {
return err
}
}
return nil
}
func processStdin() error {
input, err := io.ReadAll(os.Stdin)
if err != nil {
return fmt.Errorf("error reading from stdin: %v", err)
}
formatted := formatContent(input)
_, err = os.Stdout.Write(formatted)
if err != nil {
return fmt.Errorf("error writing to stdout: %v", err)
}
return nil
}
func processPath(path string, inPlace, recursive bool, ignorePattern string) error {
fileInfo, err := os.Stat(path)
if err != nil {
return fmt.Errorf("error accessing path %s: %v", path, err)
}
if fileInfo.IsDir() {
return processDirectory(path, inPlace, recursive, ignorePattern)
}
return processFile(path, inPlace, ignorePattern)
}
func processDirectory(dirPath string, inPlace, recursive bool, ignorePattern string) error {
walkFunc := func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
if !recursive && path != dirPath {
return filepath.SkipDir
}
return nil
}
if filepath.Ext(path) != ".md" {
return nil
}
return processFile(path, inPlace, ignorePattern)
}
return filepath.WalkDir(dirPath, walkFunc)
}
func processFile(filePath string, inPlace bool, ignorePattern string) error {
if shouldIgnore(filePath, ignorePattern) {
return nil
}
input, err := os.ReadFile(filePath)
if err != nil {
return fmt.Errorf("error reading file %s: %v", filePath, err)
}
formatted := formatContent(input)
if inPlace {
err = os.WriteFile(filePath, formatted, 0o600)
if err != nil {
return fmt.Errorf("error writing to file %s: %v", filePath, err)
}
} else {
fmt.Printf("--- %s ---\n", filePath)
_, err = os.Stdout.Write(formatted)
if err != nil {
return fmt.Errorf("error writing to stdout: %v", err)
}
fmt.Println()
}
return nil
}
func shouldIgnore(path, ignorePattern string) bool {
if ignorePattern == "" {
return false
}
return strings.Contains(filepath.Base(path), ignorePattern)
}