Skip to content

Commit

Permalink
feat: added support for custom output directory(& disabling file output)
Browse files Browse the repository at this point in the history
  • Loading branch information
Velka-DEV committed Jan 4, 2024
1 parent 03bdc3d commit 59372df
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
27 changes: 21 additions & 6 deletions pkg/core/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ package core

import (
"fmt"
"os"
"sync"
"sync/atomic"

"github.com/panjf2000/ants/v2"
)

type CheckerConfig struct {
Threads int
OutputFree bool
OutputInvalid bool
OutputLocked bool
OutputUnknown bool
Threads int
OutputToFile bool
OutputDirectory string
OutputFree bool
OutputInvalid bool
OutputLocked bool
OutputUnknown bool
}

type CheckProcessArgs struct {
Expand Down Expand Up @@ -43,6 +46,10 @@ func (c *Checker) internalOutputProcess(args *CheckProcessArgs, result *CheckRes
c.outputProcess(args, result)
}

if !c.Config.OutputToFile {
return
}

shouldOutput := false

switch result.Status {
Expand All @@ -60,8 +67,16 @@ func (c *Checker) internalOutputProcess(args *CheckProcessArgs, result *CheckRes
shouldOutput = true
}

var outputBasePath string

if c.Config.OutputDirectory == "" {
outputBasePath, _ = os.Getwd()
} else {
outputBasePath = c.Config.OutputDirectory
}

if shouldOutput {
err := WriteResultToFile(result, c.Infos)
err := WriteResultToFile(result, c.Infos, outputBasePath)
if err != nil {
// Display error
fmt.Println(err)
Expand Down
9 changes: 4 additions & 5 deletions pkg/core/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ import (
// GetOutputPath returns the path to the output file for a given status and time
// If directories do not exist, they will be created
// Example: {currentDirectory}/output/free/2021-01-01_16-34-24.txt
func createOutputPath(status CheckStatus, time time.Time) string {
currentDirectory, _ := os.Getwd()
outputDirectory := fmt.Sprintf("%s/output", currentDirectory)
func createOutputPath(status CheckStatus, time time.Time, basePath string) string {
outputDirectory := fmt.Sprintf("%s/output", basePath)
statusDirectory := fmt.Sprintf("%s/%s", outputDirectory, status.String())
filePath := fmt.Sprintf("%s/%s.txt", statusDirectory, time.Format("2006-01-02-15-04-05"))

Expand All @@ -28,7 +27,7 @@ func createOutputPath(status CheckStatus, time time.Time) string {
return filePath
}

func WriteResultToFile(result *CheckResult, info *CheckerInfo) error {
func WriteResultToFile(result *CheckResult, info *CheckerInfo, basePath string) error {
sb := strings.Builder{}

sb.WriteString(result.Combo.String())
Expand Down Expand Up @@ -56,7 +55,7 @@ func WriteResultToFile(result *CheckResult, info *CheckerInfo) error {

sb.WriteString(fmt.Sprintf("|%s", info.StartTime.Format("2006-01-02 15:04:05")))

return writeLineToFile(createOutputPath(result.Status, info.StartTime), []byte(sb.String()))
return writeLineToFile(createOutputPath(result.Status, info.StartTime, basePath), []byte(sb.String()))
}

func writeLineToFile(path string, data []byte) error {
Expand Down

0 comments on commit 59372df

Please sign in to comment.