mirror of
https://github.com/NotAShelf/mdlinkt.git
synced 2024-11-22 21:31:09 +00:00
use my own logging implementation
This commit is contained in:
parent
6654afc8f3
commit
e5e16be391
1 changed files with 25 additions and 9 deletions
34
main.go
34
main.go
|
@ -3,13 +3,14 @@ package main
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"flag"
|
"flag"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
"log/slog"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type LinkCheckResult struct {
|
type LinkCheckResult struct {
|
||||||
|
@ -18,6 +19,20 @@ type LinkCheckResult struct {
|
||||||
StatusCode int
|
StatusCode int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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...))
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
filename := flag.String("file", "", "Markdown file to test")
|
filename := flag.String("file", "", "Markdown file to test")
|
||||||
verbose := flag.Bool("verbose", false, "Enable verbose mode")
|
verbose := flag.Bool("verbose", false, "Enable verbose mode")
|
||||||
|
@ -25,13 +40,13 @@ func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if *filename == "" {
|
if *filename == "" {
|
||||||
slog.Error("Please provide a markdown file.")
|
logWithColor("INFO", "Please provide a markdown file.")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
file, err := os.Open(*filename)
|
file, err := os.Open(*filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Failed to open file: ", err)
|
logWithColor("ERROR", "Failed to open file: %v", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
@ -47,8 +62,9 @@ func main() {
|
||||||
for _, match := range matches {
|
for _, match := range matches {
|
||||||
link := strings.TrimSpace(match[2])
|
link := strings.TrimSpace(match[2])
|
||||||
resp, err := http.Head(link)
|
resp, err := http.Head(link)
|
||||||
if err != nil {
|
isInvalid := err != nil || resp.StatusCode == http.StatusNotFound || strings.Contains(err.Error(), "no such host")
|
||||||
slog.Error("Error checking link: ", err)
|
if isInvalid {
|
||||||
|
logWithColor("ERROR", "Invalid link: %s", link)
|
||||||
} else {
|
} else {
|
||||||
result := LinkCheckResult{
|
result := LinkCheckResult{
|
||||||
Link: link,
|
Link: link,
|
||||||
|
@ -61,14 +77,14 @@ func main() {
|
||||||
if !result.IsValid {
|
if !result.IsValid {
|
||||||
statusColor = color.RedString
|
statusColor = color.RedString
|
||||||
}
|
}
|
||||||
slog.Info(link + ": " + statusColor("%d", result.StatusCode))
|
logWithColor("INFO", "%s: %s", link, statusColor("%d", result.StatusCode))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := scanner.Err(); err != nil {
|
if err := scanner.Err(); err != nil {
|
||||||
slog.Info("Error scanning file: ", err)
|
logWithColor("ERROR", "Error scanning file: %v", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,7 +101,7 @@ func main() {
|
||||||
if *failedOnly {
|
if *failedOnly {
|
||||||
for _, result := range results {
|
for _, result := range results {
|
||||||
if !result.IsValid {
|
if !result.IsValid {
|
||||||
slog.Info("Failed link: " + result.Link)
|
logWithColor("ERROR", "Failed link: %s", result.Link)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -93,7 +109,7 @@ func main() {
|
||||||
if invalidCount > 0 {
|
if invalidCount > 0 {
|
||||||
summaryColor = color.RedString
|
summaryColor = color.RedString
|
||||||
}
|
}
|
||||||
slog.Info(summaryColor("Summary: %d valid links, %d invalid links", validCount, invalidCount))
|
logWithColor("INFO", summaryColor("Summary: %d valid links, %d invalid links"), validCount, invalidCount)
|
||||||
}
|
}
|
||||||
|
|
||||||
if invalidCount > 0 {
|
if invalidCount > 0 {
|
||||||
|
|
Loading…
Reference in a new issue