wip: add --walk flag to gfrun to accept directory

This commit is contained in:
Hayden 2022-08-14 16:49:20 -08:00
parent dac1c40ee5
commit a41f215785
2 changed files with 65 additions and 19 deletions

View File

@ -82,7 +82,7 @@ jobs:
chmod +x "${GITHUB_WORKSPACE}/.local/bin/gfmrun" chmod +x "${GITHUB_WORKSPACE}/.local/bin/gfmrun"
- name: 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 - name: diff check
run: | run: |

View File

@ -60,7 +60,14 @@ func main() {
Action: TestActionFunc, 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, Action: GfmrunActionFunc,
}, },
{ {
@ -220,31 +227,70 @@ func GfmrunActionFunc(cCtx *cli.Context) error {
filename = "README.md" filename = "README.md"
} }
file, err := os.Open(filename) walk := cCtx.Bool("walk")
if err != nil { sources := []string{}
return err
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 var counter int
scanner := bufio.NewScanner(file)
for scanner.Scan() { for _, src := range sources {
if strings.Contains(scanner.Text(), "package main") { file, err := os.Open(src)
counter++ 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() gfmArgs := []string{
if err != nil { "--count",
return err fmt.Sprint(counter),
}
for _, src := range sources {
gfmArgs = append(gfmArgs, "--sources", src)
} }
err = scanner.Err() if err := runCmd("gfmrun", gfmArgs...); err != nil {
if err != nil {
return err
}
if err := runCmd("gfmrun", "-c", fmt.Sprint(counter), "-s", filename); err != nil {
return err return err
} }