Skip to content

Commit

Permalink
wip: add --walk flag to gfrun to accept directory
Browse files Browse the repository at this point in the history
  • Loading branch information
hay-kot committed Aug 15, 2022
1 parent dac1c40 commit a41f215
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cli.yml
Expand Up @@ -82,7 +82,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: |
Expand Down
80 changes: 63 additions & 17 deletions internal/build/build.go
Expand Up @@ -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,
},
{
Expand Down Expand Up @@ -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()

err = file.Close()
if err != nil {
return err
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 = scanner.Err()
if err != nil {
return err
gfmArgs := []string{
"--count",
fmt.Sprint(counter),
}
for _, src := range sources {
gfmArgs = append(gfmArgs, "--sources", src)
}

if err := runCmd("gfmrun", "-c", fmt.Sprint(counter), "-s", filename); err != nil {
if err := runCmd("gfmrun", gfmArgs...); err != nil {
return err
}

Expand Down

0 comments on commit a41f215

Please sign in to comment.