diff --git a/cmd/version.go b/cmd/version.go index 8eaa29a..47d2018 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "github.com/spf13/cobra" ) diff --git a/file/file.go b/file/file.go index 6d71126..257993f 100644 --- a/file/file.go +++ b/file/file.go @@ -27,7 +27,6 @@ func FindTextBasedFiles(sourceDir string, includePatterns []string) []string { return err }) - if err != nil { panic(err) } @@ -48,7 +47,6 @@ func appendMatches(extension string, filename string, path string, matches []str // ReadFile reads the contents of a given file. func ReadFile(path string) string { read, err := ioutil.ReadFile(path) - if err != nil { panic(err) } diff --git a/file/file_test.go b/file/file_test.go index d44f12c..c428a1c 100644 --- a/file/file_test.go +++ b/file/file_test.go @@ -1,19 +1,19 @@ package file_test import ( - "fmt" - . "github.com/bmuschko/link-verifier/file" - . "github.com/stretchr/testify/assert" "os" "path/filepath" "testing" + + . "github.com/bmuschko/link-verifier/file" + . "github.com/stretchr/testify/assert" ) func TestFindAsciiDocFilesInRootDir(t *testing.T) { tempDirPath := filepath.Join(os.TempDir(), "a") createDir(tempDirPath) - path1 := filepath.Join(tempDirPath, fmt.Sprintf("1.adoc")) - path2 := filepath.Join(tempDirPath, fmt.Sprintf("abc.adoc")) + path1 := filepath.Join(tempDirPath, "1.adoc") + path2 := filepath.Join(tempDirPath, "abc.adoc") jpgPath := filepath.Join(tempDirPath, "my.jpg") binPath := filepath.Join(tempDirPath, "other.bin") docPath := filepath.Join(tempDirPath, "some.doc") @@ -42,8 +42,8 @@ func TestFindAsciiDocFilesInSubDirs(t *testing.T) { createDir(subDirPath) subSubDirPath := filepath.Join(subDirPath, "subsub") createDir(subSubDirPath) - path1 := filepath.Join(subDirPath, fmt.Sprintf("1.adoc")) - path2 := filepath.Join(subSubDirPath, fmt.Sprintf("2.adoc")) + path1 := filepath.Join(subDirPath, "1.adoc") + path2 := filepath.Join(subSubDirPath, "2.adoc") createFile(path1) createFile(path2) @@ -59,9 +59,9 @@ func TestFindAsciiDocFilesInSubDirs(t *testing.T) { func TestFindAsciiDocFilesDifferentExtensions(t *testing.T) { tempDirPath := filepath.Join(os.TempDir(), "c") createDir(tempDirPath) - path1 := filepath.Join(tempDirPath, fmt.Sprintf("1.adoc")) - path2 := filepath.Join(tempDirPath, fmt.Sprintf("2.asciidoc")) - path3 := filepath.Join(tempDirPath, fmt.Sprintf("3.asc")) + path1 := filepath.Join(tempDirPath, "1.adoc") + path2 := filepath.Join(tempDirPath, "2.asciidoc") + path3 := filepath.Join(tempDirPath, "3.asc") createFile(path1) createFile(path2) createFile(path3) @@ -80,8 +80,8 @@ func TestFindAsciiDocFilesDifferentExtensions(t *testing.T) { func TestFilesForCustomIncludePatterns(t *testing.T) { tempDirPath := filepath.Join(os.TempDir(), "e") createDir(tempDirPath) - path1 := filepath.Join(tempDirPath, fmt.Sprintf("1.html")) - path2 := filepath.Join(tempDirPath, fmt.Sprintf("2.yml")) + path1 := filepath.Join(tempDirPath, "1.html") + path2 := filepath.Join(tempDirPath, "2.yml") createFile(path1) createFile(path2) @@ -98,7 +98,7 @@ func TestReadFile(t *testing.T) { expectedContent := "some text" tempDirPath := filepath.Join(os.TempDir(), "content") createDir(tempDirPath) - path1 := filepath.Join(tempDirPath, fmt.Sprintf("1.adoc")) + path1 := filepath.Join(tempDirPath, "1.adoc") createFile(path1) writeFile(path1, expectedContent) @@ -116,8 +116,7 @@ func TestPanicWhenReadingNonExistentFile(t *testing.T) { } func createDir(path string) { - err := os.MkdirAll(path, 0755) - + err := os.MkdirAll(path, 0o755) if err != nil { panic(err) } @@ -125,7 +124,6 @@ func createDir(path string) { func createFile(path string) { w, err := os.Create(path) - if err != nil { panic(err) } @@ -134,7 +132,7 @@ func createFile(path string) { } func writeFile(path string, content string) { - var file, err = os.OpenFile(path, os.O_RDWR, 0644) + file, err := os.OpenFile(path, os.O_RDWR, 0o644) if err != nil { panic(err) @@ -143,20 +141,18 @@ func writeFile(path string, content string) { defer file.Close() _, err = file.WriteString(content) - if err != nil { panic(err) } err = file.Sync() - if err != nil { panic(err) } } func deleteFile(path string) { - var err = os.Remove(path) + err := os.Remove(path) if err != nil { panic(err) diff --git a/http/http.go b/http/http.go index 50352d6..67c2e75 100644 --- a/http/http.go +++ b/http/http.go @@ -39,14 +39,12 @@ func (h *HTTP) Get(link string) HttpResponse { func sendRequest(link string, req func(*url.URL) (resp *http.Response, err error)) HttpResponse { result := HttpResponse{Url: link} url, err := url.ParseRequestURI(link) - if err != nil { result.Error = err return result } resp, err := req(url) - if err != nil { result.Error = err return result diff --git a/http/http_test.go b/http/http_test.go index 8d85fd5..2e456bf 100644 --- a/http/http_test.go +++ b/http/http_test.go @@ -1,12 +1,13 @@ package http_test import ( - . "github.com/bmuschko/link-verifier/http" - . "github.com/stretchr/testify/assert" "net/http" "net/http/httptest" "testing" "time" + + . "github.com/bmuschko/link-verifier/http" + . "github.com/stretchr/testify/assert" ) const ( diff --git a/main.go b/main.go index f0dc837..34e7448 100644 --- a/main.go +++ b/main.go @@ -4,9 +4,7 @@ import ( "github.com/bmuschko/link-verifier/cmd" ) -var ( - version = "undefined" -) +var version = "undefined" func main() { cmd.SetVersion(version) diff --git a/stat/stat_test.go b/stat/stat_test.go index 565119b..ec5a2b2 100644 --- a/stat/stat_test.go +++ b/stat/stat_test.go @@ -1,9 +1,10 @@ package stat_test import ( + "testing" + . "github.com/bmuschko/link-verifier/stat" . "github.com/stretchr/testify/assert" - "testing" ) func TestSumSuccessesForEmptySlice(t *testing.T) { diff --git a/text/parse.go b/text/parse.go index 0c14745..6a99cac 100644 --- a/text/parse.go +++ b/text/parse.go @@ -4,17 +4,20 @@ package text import ( - "mvdan.cc/xurls/v2" "regexp" "strings" + + "mvdan.cc/xurls/v2" ) // ParseLinks parses a given text and extracts all links. None of the links is further modified except for the rules listed below. // Sanitizes founds like based on the following logic: // - Removes link description e.g. [...] if extraction logic couldn't remove it. // - Remove fragments in URLs e.g. #sec:news - they are a browser-only concept. +// // Does not included links based on the following logic: // - URLs that contains String interpolation with ${...}. +// // Returns a slice of links. func ParseLinks(content string) []string { uniqueLinks := make(map[string]bool) diff --git a/text/parse_test.go b/text/parse_test.go index 1fbd82c..44e0bb9 100644 --- a/text/parse_test.go +++ b/text/parse_test.go @@ -2,9 +2,10 @@ package text_test import ( "fmt" + "testing" + . "github.com/bmuschko/link-verifier/text" . "github.com/stretchr/testify/assert" - "testing" ) const expectedLink = "http://www.oracle.com/technetwork/articles/java/index-jsp-135444.html" diff --git a/verify/verify.go b/verify/verify.go index 7867a82..b34d7f9 100644 --- a/verify/verify.go +++ b/verify/verify.go @@ -2,13 +2,14 @@ package verify import ( "fmt" + "os" + "strconv" + "strings" + "github.com/bmuschko/link-verifier/file" "github.com/bmuschko/link-verifier/http" "github.com/bmuschko/link-verifier/stat" "github.com/bmuschko/link-verifier/text" - "os" - "strconv" - "strings" ) // Resolve resolves text-based files for a given directories. @@ -111,7 +112,7 @@ func validateLink(link string, timeout int, ignoreStatusCodes []int, summary *st } func calculateSeparator(stats string) string { - var separator = "" + separator := "" for i := 0; i < len(stats); i++ { separator += "-"