Skip to content

Commit bc4f25e

Browse files
committed
Refactor and fix linter
1 parent 2dc1180 commit bc4f25e

12 files changed

+152
-233
lines changed

Diff for: .golangci.yml

-33
This file was deleted.

Diff for: .mega-linter.yml

+2
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ ENABLE_LINTERS: # If you use ENABLE_LINTERS variable, all other linters will be
1111
SHOW_ELAPSED_TIME: true
1212
FILEIO_REPORTER: false
1313
# DISABLE_ERRORS: true # Uncomment if you want MegaLinter to detect errors but not block CI to pass
14+
ADDITIONAL_EXCLUDED_DIRECTORIES:
15+
- "vendor"

Diff for: models/email_handler.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ var (
1313
type emailHandler struct {
1414
}
1515

16-
func GetEmailHandlerInstance() *emailHandler {
16+
// GetEmailHandlerInstance Get email handler instance
17+
func GetEmailHandlerInstance() LinkHandlerInterface {
1718
if mailHandler == nil {
1819
mailHandler = &emailHandler{}
1920
}

Diff for: models/file_data.go

-54
This file was deleted.

Diff for: models/file_handler.go

-46
This file was deleted.

Diff for: models/file_link_handler.go

+19-24
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,29 @@ import (
55
"net/url"
66
"os"
77
"path"
8+
"path/filepath"
89
"regexp"
910
"strings"
1011
)
1112

1213
var (
13-
linkFileHandler *fileLinkHandler
14+
internalLink *internalLinkHandler
1415
)
1516

16-
type fileLinkHandler struct {
17-
folderPath string
18-
fileName string
17+
type internalLinkHandler struct {
18+
filePath string
1919
}
2020

21-
func GetFileLinkHandler(filePath string) *fileLinkHandler {
22-
fileData, err := NewFileData(filePath)
23-
if err != nil {
24-
return nil
25-
}
26-
linkFileHandler = &fileLinkHandler{
27-
folderPath: fileData.folderPath,
28-
fileName: fileData.fileName,
21+
func GetInternalLinkHandler(filePath string) LinkHandlerInterface {
22+
internalLink = &internalLinkHandler{
23+
filePath: filePath,
2924
}
30-
return linkFileHandler
25+
return internalLink
3126
}
3227

33-
func (handler *fileLinkHandler) Handle(linkPath string) int {
34-
linkedFileEscapedFullPath := handler.escapedFullPath(linkPath)
28+
func (handler *internalLinkHandler) Handle(linkPath string) int {
29+
folderPath, fileName := filepath.Split(handler.filePath)
30+
linkedFileEscapedFullPath := handler.escapedFullPath(folderPath, fileName, linkPath)
3531
if strings.Contains(linkedFileEscapedFullPath, "?") {
3632
linkedFileEscapedFullPath = strings.Split(linkedFileEscapedFullPath, "?")[0]
3733
}
@@ -53,21 +49,20 @@ func (handler *fileLinkHandler) Handle(linkPath string) int {
5349
return 200
5450
}
5551

56-
func (handler *fileLinkHandler) escapedFullPath(extension string) string {
57-
folderPath := handler.folderPath
58-
if strings.HasPrefix(extension, "#") {
59-
folderPath = path.Join(handler.folderPath, handler.fileName)
60-
} else if strings.Contains(extension, "#") {
61-
fileName := strings.Split(extension, "#")[0]
62-
folderPath = path.Join(handler.folderPath, fileName)
52+
func (handler *internalLinkHandler) escapedFullPath(folderPath, fileName, linkPath string) string {
53+
if strings.HasPrefix(linkPath, "#") {
54+
folderPath = path.Join(folderPath, fileName)
55+
} else if strings.Contains(linkPath, "#") {
56+
fileName := strings.Split(linkPath, "#")[0]
57+
folderPath = path.Join(folderPath, fileName)
6358
} else {
64-
folderPath = path.Join(handler.folderPath, extension)
59+
folderPath = path.Join(folderPath, linkPath)
6560
}
6661
folderPath, _ = url.PathUnescape(folderPath)
6762
return folderPath
6863
}
6964

70-
func (handler *fileLinkHandler) fileContainsLink(titleLink string, fileText string) bool {
65+
func (handler *internalLinkHandler) fileContainsLink(titleLink string, fileText string) bool {
7166
titleLink = strings.Split(titleLink, "#")[1]
7267
title := strings.ReplaceAll(titleLink, "#", "")
7368
title = strings.ReplaceAll(title, "(", "\\(")

Diff for: models/file_processor.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package models
2+
3+
import (
4+
log "github.com/sirupsen/logrus"
5+
"os"
6+
)
7+
8+
type FileProcessor interface {
9+
ProcessFile()
10+
}
11+
12+
type fileProcessor struct {
13+
resultChan chan *LinkResult
14+
filePath string
15+
fileLinesData map[int]string
16+
}
17+
18+
func GetNewFileProcessor(filePath string, resultChan chan *LinkResult) FileProcessor {
19+
return &fileProcessor{
20+
filePath: filePath,
21+
resultChan: resultChan,
22+
}
23+
}
24+
25+
func (fp *fileProcessor) ProcessFile() {
26+
defer wg.Done()
27+
linkHandler := GetLinkProcessorInstance()
28+
fileBytes, err := os.ReadFile(fp.filePath)
29+
if err != nil {
30+
log.WithFields(log.Fields{"error": err}).Error("failed to read file " + fp.filePath)
31+
linkData := &LinkResult{
32+
lineNumber: 0,
33+
status: 404,
34+
filePath: fp.filePath,
35+
linkType: InternalLink,
36+
}
37+
fp.resultChan <- linkData
38+
return
39+
}
40+
fileData := string(fileBytes)
41+
linksPaths := linkHandler.ExtractLinks(fileData)
42+
for _, linkData := range linksPaths {
43+
fp.resultChan <- linkHandler.CheckLink(fp.filePath, linkData.Link, linkData.LinkLineNumber)
44+
}
45+
}

Diff for: models/files_handler.go renamed to models/files_processor.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,27 @@ func GetFilesProcessorInstance() FilesProcessor {
3030

3131
func (fh *filesProcessor) Process(files []string) error {
3232
for _, filePath := range files {
33-
fileLinkData := FileLink{
33+
fileLinkData := FileResultData{
3434
FilePath: filePath,
35-
Links: []*Link{},
35+
Links: []*LinkResult{},
3636
Error: false,
3737
}
3838
fh.AddNewFile(&fileLinkData)
39-
fileHandler := GetNewFileHandler(filePath, fh.Channel)
40-
if fileHandler != nil {
39+
fileProcessor := GetNewFileProcessor(filePath, fh.Channel)
40+
if fileProcessor != nil {
4141
wg.Add(1)
42-
fh.invoke(fileHandler)
42+
fh.invoke(fileProcessor)
4343
}
4444
}
4545
wg.Wait()
4646
fh.Close()
4747
return fh.Print()
4848
}
4949

50-
func (fh *filesProcessor) invoke(fileHandler FileHandler) {
50+
func (fh *filesProcessor) invoke(fileProcessor FileProcessor) {
5151
if fh.serial {
52-
fileHandler.HandleFile()
52+
fileProcessor.ProcessFile()
5353
} else {
54-
go fileHandler.HandleFile()
54+
go fileProcessor.ProcessFile()
5555
}
5656
}

Diff for: models/handler_interface.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package models
2+
3+
type LinkHandlerInterface interface {
4+
Handle(linkPath string) int
5+
}

0 commit comments

Comments
 (0)