/
main.go
187 lines (154 loc) · 3.93 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"context"
"errors"
"fmt"
"log"
"log/slog"
"os"
"path/filepath"
"github.com/urfave/cli/v2"
"github.com/vvakame/ptproc"
"golang.org/x/sync/errgroup"
)
func main() {
err := realMain()
if err != nil {
log.Fatal(err)
}
}
func realMain() error {
ctx := context.Background()
setDefaultLoggerWithLevel(slog.LevelInfo)
app := &cli.App{
Name: "ptproc",
Usage: "plain text preprocessor",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "logLevel",
Usage: "--logLevel (debug|info|warn|error)",
Action: func(cCtx *cli.Context, logLevel string) error {
ctx := cCtx.Context
var leveler slog.Leveler
switch logLevel {
case "debug":
leveler = slog.LevelDebug
case "info":
leveler = slog.LevelInfo
case "warn":
leveler = slog.LevelWarn
case "error":
leveler = slog.LevelError
default:
return fmt.Errorf("unknown logLevel: %s", logLevel)
}
setDefaultLoggerWithLevel(leveler)
slog.DebugContext(ctx, "set default log level", slog.String("logLevel", logLevel))
return nil
},
},
&cli.StringFlag{
Name: "config",
Usage: "syntax config file path",
DefaultText: "./ptproc.yaml",
Aliases: []string{"c"},
},
&cli.BoolFlag{
Name: "replace",
Usage: "write back result to source file instead of stdout",
Aliases: []string{"r"},
},
&cli.StringFlag{
Name: "glob",
Usage: "specify target file by glob pattern. see https://pkg.go.dev/path/filepath#Glob",
Aliases: []string{"g"},
},
},
Action: func(cCtx *cli.Context) error {
ctx := cCtx.Context
configFilePath := cCtx.String("config")
configFileSpecified := true
if configFilePath == "" {
configFilePath = "ptproc.yaml"
configFileSpecified = false
}
useReplace := cCtx.Bool("replace")
globPattern := cCtx.String("glob")
var cfg *ptproc.ProcessorConfig
if rawCfg, err := ptproc.LoadConfig(ctx, configFilePath); !configFileSpecified && errors.Is(err, os.ErrNotExist) {
slog.DebugContext(ctx, "ptproc.yaml is not exists. ignored")
cfg = nil
} else if errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("failed to load config file: %s, : %w", configFilePath, err)
} else if err != nil {
return err
} else {
cfg, err = rawCfg.ToProcessorConfig(ctx)
if err != nil {
return err
}
}
slog.DebugContext(ctx, "start processing", slog.Bool("replace", useReplace), slog.String("glob", globPattern))
var filePaths []string
if fs := cCtx.Args().Slice(); len(fs) != 0 {
filePaths = append(filePaths, fs...)
}
if globPattern != "" {
fs, err := filepath.Glob(globPattern)
if err != nil {
return err
}
filePaths = append(filePaths, fs...)
}
if len(filePaths) == 0 {
return errors.New("no files specified")
}
slog.DebugContext(ctx, "target files", "filePaths", filePaths)
proc, err := ptproc.NewProcessor(cfg)
if err != nil {
return err
}
var eg errgroup.Group
for _, s := range filePaths {
s := s
if useReplace {
eg.Go(func() error {
slog.DebugContext(ctx, "replace file", slog.String("file", s))
result, err := proc.ProcessFile(ctx, s)
if err != nil {
return err
}
err = os.WriteFile(s, []byte(result), 0o644)
if err != nil {
return err
}
slog.InfoContext(ctx, "file has been replaced", slog.String("file", s))
return nil
})
} else {
result, err := proc.ProcessFile(ctx, s)
if err != nil {
return err
}
fmt.Println(result)
}
}
err = eg.Wait()
if err != nil {
return err
}
return nil
},
}
if err := app.RunContext(ctx, os.Args); err != nil {
return err
}
return nil
}
func setDefaultLoggerWithLevel(level slog.Leveler) {
h := slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
Level: level,
})
logger := slog.New(h)
slog.SetDefault(logger)
}