~samwhited/checkmd

610e955e5400003cf8a6d8b9394bd7fe6de5589c — Sam Whited 6 months ago 8a40014
checkmd: don't short circuit if file has errors

Signed-off-by: Sam Whited <sam@samwhited.com>
1 files changed, 20 insertions(+), 11 deletions(-)

M main.go
M main.go => main.go +20 -11
@@ 23,13 23,16 @@ func main() {
		fmt.Fprintf(os.Stderr, "usage %s filenames\n", os.Args[0])
		os.Exit(1)
	}

	var hasErrs bool
	for _, root := range os.Args[1:] {
		if isMD(root) {
			err := checkFile(root)
			ok, err := checkFile(root)
			if err != nil {
				fmt.Fprintln(os.Stderr, err)
				os.Exit(1)
			}
			hasErrs = hasErrs || ok
			continue
		}



@@ 40,27 43,36 @@ func main() {
				return nil
			}

			return checkFile(fpath)
			ok, err := checkFile(fpath)
			if err != nil {
				return err
			}
			hasErrs = hasErrs || ok
			return nil
		})
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			os.Exit(1)
		}
	}

	if hasErrs {
		os.Exit(1)
	}
}

func isMD(fpath string) bool {
	return strings.HasSuffix(fpath, ".md") || strings.HasSuffix(fpath, ".markdown")
}

func checkFile(fpath string) error {
func checkFile(fpath string) (bool, error) {
	/* #nosec */
	md, err := ioutil.ReadFile(fpath)
	if err != nil {
		return err
		return false, err
	}
	node := blackfriday.New().Parse(md)
	var hasErrs int
	var hasErrs bool
	node.Walk(func(node *blackfriday.Node, entering bool) blackfriday.WalkStatus {
		if node.Type != blackfriday.Link || !entering {
			return blackfriday.GoToNext


@@ 68,18 80,15 @@ func checkFile(fpath string) error {

		u, err := url.Parse(string(node.LinkData.Destination))
		if err != nil {
			hasErrs++
			hasErrs = true
			fmt.Fprintf(os.Stderr, "link %q is not a valid URL: %v\n", node.LinkData.Destination, err)
			return blackfriday.GoToNext
		}
		if u.Scheme == "" {
			hasErrs++
			hasErrs = true
			fmt.Fprintf(os.Stderr, "%s: link is missing the URL scheme: %s\n", fpath, u)
		}
		return blackfriday.GoToNext
	})
	if hasErrs > 0 {
		return fmt.Errorf("found %d errors", hasErrs)
	}
	return nil
	return hasErrs, nil
}