From 1934fc73e8608a846afcf4e6e06aa4d0b61cac56 Mon Sep 17 00:00:00 2001 From: Mike Danese Date: Tue, 5 Jan 2016 11:26:53 -0800 Subject: [PATCH] mungedocs: fix ignored errors in link checker Before this change the link checker would ignore errors in a file if the last link in a file was correct. The last link would wipe out the error variable and set it to nil. Furthermore, it replaced errored links with the empty string. If we find an error that we can't correct, append the error message to an an errs slice and leave the string as is. Signed-off-by: Mike Danese --- cmd/mungedocs/links.go | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/cmd/mungedocs/links.go b/cmd/mungedocs/links.go index e7c95705a5a..9fb82ac16a7 100644 --- a/cmd/mungedocs/links.go +++ b/cmd/mungedocs/links.go @@ -17,6 +17,7 @@ limitations under the License. package main import ( + "errors" "fmt" "net/url" "os" @@ -33,11 +34,12 @@ var ( ) func processLink(in string, filePath string) (string, error) { - var err error + var errs []string out := linkRE.ReplaceAllStringFunc(in, func(in string) string { + var err error match := linkRE.FindStringSubmatch(in) if match == nil { - err = fmt.Errorf("Detected this line had a link, but unable to parse, %v", in) + errs = append(errs, fmt.Sprintf("Detected this line had a link, but unable to parse, %v", in)) return "" } // match[0] is the entire expression; @@ -56,8 +58,8 @@ func processLink(in string, filePath string) (string, error) { u, terr := url.Parse(linkText) if terr != nil { - err = fmt.Errorf("link %q is unparsable: %v", linkText, terr) - return "" + errs = append(errs, fmt.Sprintf("link %q is unparsable: %v", linkText, terr)) + return in } if u.Host != "" && u.Host != "github.com" { @@ -69,8 +71,8 @@ func processLink(in string, filePath string) (string, error) { if u.Path != "" && !strings.HasPrefix(linkText, "TODO:") { newPath, targetExists := checkPath(filePath, path.Clean(u.Path)) if !targetExists { - err = fmt.Errorf("%q: target not found", linkText) - return "" + errs = append(errs, fmt.Sprintf("%q: target not found", linkText)) + return in } u.Path = newPath if strings.HasPrefix(u.Path, "/") { @@ -87,7 +89,8 @@ func processLink(in string, filePath string) (string, error) { dir := path.Dir(filePath) suggestedVisibleText, err = makeRepoRelative(path.Join(dir, u.Path), filePath) if err != nil { - return "" + errs = append(errs, fmt.Sprintf("%q: unable to make path relative", filePath)) + return in } } else { suggestedVisibleText = u.Path @@ -109,8 +112,8 @@ func processLink(in string, filePath string) (string, error) { return fmt.Sprintf("[%s](%s)", visibleText, linkText+altText) }) - if out == "" { - return in, err + if len(errs) != 0 { + return "", errors.New(strings.Join(errs, ",")) } return out, nil }