Skip to content

Commit

Permalink
fix: recursively create output path
Browse files Browse the repository at this point in the history
  • Loading branch information
Velka-DEV committed May 25, 2024
1 parent 1d3b283 commit 164cf48
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
11 changes: 1 addition & 10 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/panjf2000/ants/v2 v2.8.1 h1:C+n/f++aiW8kHCExKlpX6X+okmxKXP7DWLutxuAPuwQ=
github.com/panjf2000/ants/v2 v2.8.1/go.mod h1:KIBmYG9QQX5U2qzFP/yQJaq/nSb6rahS9iEHkrCMgM8=
github.com/panjf2000/ants/v2 v2.8.2 h1:D1wfANttg8uXhC9149gRt1PDQ+dLVFjNXkCEycMcvQQ=
github.com/panjf2000/ants/v2 v2.8.2/go.mod h1:7ZxyxsqE4vvW0M7LSD8aI3cKwgFhBHbxnlN8mDqHa1I=
github.com/panjf2000/ants/v2 v2.9.0 h1:SztCLkVxBRigbg+vt0S5QvF5vxAbxbKt09/YfAJ0tEo=
github.com/panjf2000/ants/v2 v2.9.0/go.mod h1:7ZxyxsqE4vvW0M7LSD8aI3cKwgFhBHbxnlN8mDqHa1I=
github.com/panjf2000/ants/v2 v2.9.1 h1:Q5vh5xohbsZXGcD6hhszzGqB7jSSc2/CRr3QKIga8Kw=
github.com/panjf2000/ants/v2 v2.9.1/go.mod h1:7ZxyxsqE4vvW0M7LSD8aI3cKwgFhBHbxnlN8mDqHa1I=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -16,12 +10,9 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
29 changes: 17 additions & 12 deletions pkg/core/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"time"
Expand All @@ -11,20 +12,18 @@ 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, 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"))
func createOutputPath(status CheckStatus, t time.Time, basePath string) (string, error) {
outputDirectory := filepath.Join(basePath, "output")
statusDirectory := filepath.Join(outputDirectory, status.String())
filePath := filepath.Join(statusDirectory, fmt.Sprintf("%s.txt", t.Format("2006-01-02-15-04-05")))

if _, err := os.Stat(outputDirectory); os.IsNotExist(err) {
os.Mkdir(outputDirectory, 0755)
}

if _, err := os.Stat(statusDirectory); os.IsNotExist(err) {
os.Mkdir(statusDirectory, 0755)
// Use os.MkdirAll to create all necessary directories
err := os.MkdirAll(statusDirectory, 0755)
if err != nil {
return "", err
}

return filePath
return filePath, nil
}

func WriteResultToFile(result *CheckResult, info *CheckerInfo, basePath string) error {
Expand Down Expand Up @@ -55,7 +54,13 @@ func WriteResultToFile(result *CheckResult, info *CheckerInfo, basePath string)

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

return writeLineToFile(createOutputPath(result.Status, info.StartTime, basePath), []byte(sb.String()))
outputPath, err := createOutputPath(result.Status, info.StartTime, basePath)

if err != nil {
return err
}

return writeLineToFile(outputPath, []byte(sb.String()))
}

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

0 comments on commit 164cf48

Please sign in to comment.