wip: add --walk flag to gfrun to accept directory
This commit is contained in:
parent
8036eac8dc
commit
d217a5ac6c
2
.github/workflows/cli.yml
vendored
2
.github/workflows/cli.yml
vendored
@ -92,7 +92,7 @@ jobs:
|
||||
chmod +x "${GITHUB_WORKSPACE}/.local/bin/gfmrun"
|
||||
|
||||
- name: gfmrun
|
||||
run: go run internal/build/build.go gfmrun docs/v2/manual.md
|
||||
run: go run internal/build/build.go gfmrun --walk docs/v2/
|
||||
|
||||
- name: diff check
|
||||
run: |
|
||||
|
@ -60,7 +60,14 @@ func main() {
|
||||
Action: TestActionFunc,
|
||||
},
|
||||
{
|
||||
Name: "gfmrun",
|
||||
Name: "gfmrun",
|
||||
Flags: []cli.Flag{
|
||||
&cli.BoolFlag{
|
||||
Name: "walk",
|
||||
Value: false,
|
||||
Usage: "Walk the specified directory and perform validation on all markdown files",
|
||||
},
|
||||
},
|
||||
Action: GfmrunActionFunc,
|
||||
},
|
||||
{
|
||||
@ -220,31 +227,70 @@ func GfmrunActionFunc(cCtx *cli.Context) error {
|
||||
filename = "README.md"
|
||||
}
|
||||
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
walk := cCtx.Bool("walk")
|
||||
sources := []string{}
|
||||
|
||||
if walk {
|
||||
// Walk the directory and find all markdown files.
|
||||
err := filepath.Walk(top, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
if filepath.Ext(path) != ".md" {
|
||||
return nil
|
||||
}
|
||||
|
||||
sources = append(sources, path)
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
sources = append(sources, filename)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
var counter int
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
if strings.Contains(scanner.Text(), "package main") {
|
||||
counter++
|
||||
|
||||
for _, src := range sources {
|
||||
file, err := os.Open(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
if strings.Contains(scanner.Text(), "package main") {
|
||||
counter++
|
||||
}
|
||||
}
|
||||
|
||||
err = file.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = scanner.Err()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
err = file.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
gfmArgs := []string{
|
||||
"--count",
|
||||
fmt.Sprint(counter),
|
||||
}
|
||||
for _, src := range sources {
|
||||
gfmArgs = append(gfmArgs, "--sources", src)
|
||||
}
|
||||
|
||||
err = scanner.Err()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := runCmd("gfmrun", "-c", fmt.Sprint(counter), "-s", filename); err != nil {
|
||||
if err := runCmd("gfmrun", gfmArgs...); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user