mdlinkt/main.go

119 lines
2.7 KiB
Go
Raw Normal View History

2023-12-24 17:02:13 +00:00
package main
import (
"bufio"
"flag"
2023-12-24 17:37:07 +00:00
"fmt"
2023-12-24 17:02:13 +00:00
"net/http"
"os"
"regexp"
"strings"
2023-12-24 17:37:07 +00:00
"time"
2023-12-24 17:02:13 +00:00
"github.com/fatih/color"
)
type LinkCheckResult struct {
Link string
IsValid bool
StatusCode int
}
2023-12-24 17:37:07 +00:00
func logWithColor(level string, msg string, args ...interface{}) {
timestamp := time.Now().Format("2006/01/02 15:04:05")
colorFunc := color.New(color.FgWhite).SprintFunc()
switch level {
case "ERROR":
colorFunc = color.New(color.FgRed).SprintFunc()
case "WARN":
colorFunc = color.New(color.FgYellow).SprintFunc()
case "INFO":
colorFunc = color.New(color.FgCyan).SprintFunc()
}
fmt.Printf("%s %s %s\n", timestamp, colorFunc(level), fmt.Sprintf(msg, args...))
}
2023-12-24 17:02:13 +00:00
func main() {
filename := flag.String("file", "", "Markdown file to test")
verbose := flag.Bool("verbose", false, "Enable verbose mode")
failedOnly := flag.Bool("failed-only", false, "Return only failed links")
flag.Parse()
if *filename == "" {
2023-12-24 17:37:07 +00:00
logWithColor("INFO", "Please provide a markdown file.")
2023-12-24 17:02:13 +00:00
os.Exit(1)
}
file, err := os.Open(*filename)
if err != nil {
2023-12-24 17:37:07 +00:00
logWithColor("ERROR", "Failed to open file: %v", err)
2023-12-24 17:02:13 +00:00
os.Exit(1)
}
defer file.Close()
scanner := bufio.NewScanner(file)
re := regexp.MustCompile(`\[(.*?)\]\((.*?)\)`)
results := []LinkCheckResult{}
for scanner.Scan() {
line := scanner.Text()
matches := re.FindAllStringSubmatch(line, -1)
for _, match := range matches {
link := strings.TrimSpace(match[2])
resp, err := http.Head(link)
2023-12-24 17:37:07 +00:00
isInvalid := err != nil || resp.StatusCode == http.StatusNotFound || strings.Contains(err.Error(), "no such host")
if isInvalid {
logWithColor("ERROR", "Invalid link: %s", link)
2023-12-24 17:02:13 +00:00
} else {
result := LinkCheckResult{
Link: link,
IsValid: resp.StatusCode == http.StatusOK,
StatusCode: resp.StatusCode,
}
results = append(results, result)
if *verbose || (!*failedOnly && !result.IsValid) {
statusColor := color.GreenString
if !result.IsValid {
statusColor = color.RedString
}
2023-12-24 17:37:07 +00:00
logWithColor("INFO", "%s: %s", link, statusColor("%d", result.StatusCode))
2023-12-24 17:02:13 +00:00
}
}
}
}
if err := scanner.Err(); err != nil {
2023-12-24 17:37:07 +00:00
logWithColor("ERROR", "Error scanning file: %v", err)
2023-12-24 17:02:13 +00:00
os.Exit(1)
}
// Print summary
validCount := 0
invalidCount := 0
for _, result := range results {
if result.IsValid {
validCount++
} else {
invalidCount++
}
}
if *failedOnly {
for _, result := range results {
if !result.IsValid {
2023-12-24 17:37:07 +00:00
logWithColor("ERROR", "Failed link: %s", result.Link)
2023-12-24 17:02:13 +00:00
}
}
} else {
summaryColor := color.GreenString
if invalidCount > 0 {
summaryColor = color.RedString
}
2023-12-24 17:37:07 +00:00
logWithColor("INFO", summaryColor("Summary: %d valid links, %d invalid links"), validCount, invalidCount)
2023-12-24 17:02:13 +00:00
}
if invalidCount > 0 {
os.Exit(1)
}
}