Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

formatter:chore - replace strings.Replace to fmt.Sprintf #914

Merged
merged 1 commit into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/helpers/messages/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ const (
MsgDebugDockerAPIContainerRead = "{HORUSEC_CLI} Docker read container output: "
MsgDebugDockerAPIFinishedSuccess = "{HORUSEC_CLI} Docker Finished analysis with SUCCESS: "
MsgDebugDockerAPIFinishedError = "{HORUSEC_CLI} Docker Finished analysis with ERROR: "
MsgDebugToolStartAnalysis = "{HORUSEC_CLI} Running {{0}} - {{1}} in analysisID: "
MsgDebugToolFinishAnalysis = "{HORUSEC_CLI} {{0}} - {{1}} is finished in analysisID: "
MsgDebugToolStartAnalysis = "{HORUSEC_CLI} Running %s - %s in analysisID: %s"
MsgDebugToolFinishAnalysis = "{HORUSEC_CLI} %s - %s is finished in analysisID: %s"
MsgDebugOutputEmpty = "{HORUSEC_CLI} When format Output it's Empty!"
MsgDebugConfigFileRunningOnPath = "{HORUSEC_CLI} Config file running on path: "
MsgDebugFolderOrFileIgnored = "{HORUSEC_CLI} The file or folder was ignored to send analysis:"
Expand Down
9 changes: 5 additions & 4 deletions internal/helpers/messages/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@
package messages

// Block of messages usage into error response
//
// nolint:lll
const (
MsgErrorPathNotValid = "invalid path:"
MsgErrorJSONOutputFilePathNotValidExtension = "Output File path not valid file of type:"
MsgErrorJSONOutputFilePathNotValidUnknown = "Output File path is required or is invalid:"
MsgErrorSeverityNotValid = "Type of severity not valid. See severities enable:"
MsgErrorAskForUserCancelled = "{HORUSEC_CLI} Operation was canceled by user"
MsgVulnerabilityTypeToShowInvalid = "{HORUSEC_CLI} Error on validate vulnerability type is wrong type: "
MsgErrorRunToolInDocker = "{HORUSEC_CLI} Something error went wrong in {{0}} tool " +
"| analysisID -> {{1}} | output -> {{2}}"
MsgErrorInvalidWorkDir = "{HORUSEC_CLI} Workdir is nil! Check the configuration and try again"
MsgErrorParseStringToToolsConfig = "{HORUSEC_CLI} Error when try parse tools config string to entity. " +
MsgErrorRunToolInDocker = "{HORUSEC_CLI} Error to execute tool %s | analysisID -> %s | output -> %s"
MsgErrorInvalidWorkDir = "{HORUSEC_CLI} Workdir is nil! Check the configuration and try again"
MsgErrorParseStringToToolsConfig = "{HORUSEC_CLI} Error when try parse tools config string to entity. " +
"Returning default values"
MsgErrorNotFoundRequirementsTxt = "{HORUSEC_CLI} Error The file requirements.txt not found in python project to " +
"start analysis. It would be a good idea to commit it so horusec can check for vulnerabilities"
Expand Down
9 changes: 2 additions & 7 deletions internal/services/formatters/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,7 @@ func (s *Service) ExecuteContainer(data *dockerentity.AnalysisData) (output stri
}

func (s *Service) GetAnalysisIDErrorMessage(tool tools.Tool, output string) string {
msg := strings.ReplaceAll(messages.MsgErrorRunToolInDocker, "{{0}}", tool.ToString())
msg = strings.ReplaceAll(msg, "{{1}}", s.GetAnalysisID())
msg = strings.ReplaceAll(msg, "{{2}}", output)
return msg
return fmt.Sprintf(messages.MsgErrorRunToolInDocker, tool, s.GetAnalysisID(), output)
}

func (s *Service) GetConfigProjectPath() string {
Expand All @@ -103,9 +100,7 @@ func (s *Service) AddWorkDirInCmd(cmd, projectSubPath string, tool tools.Tool) s
}

func (s *Service) LogDebugWithReplace(msg string, tool tools.Tool, lang languages.Language) {
newMsg := strings.ReplaceAll(msg, "{{0}}", tool.ToString())
newMsg = strings.ReplaceAll(newMsg, "{{1}}", lang.ToString())
logger.LogDebugWithLevel(newMsg, s.analysis.GetIDString())
logger.LogDebugWithLevel(fmt.Sprintf(msg, tool, lang, s.analysis.GetIDString()))
}

func (s *Service) GetAnalysisID() string {
Expand Down
9 changes: 4 additions & 5 deletions internal/services/formatters/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ func TestGetAnalysisIDErrorMessage(t *testing.T) {
result := monitorController.GetAnalysisIDErrorMessage(tools.Bandit, "test")

assert.NotEmpty(t, result)
assert.Equal(t, "{HORUSEC_CLI} Something error went wrong in Bandit tool"+
" | analysisID -> 00000000-0000-0000-0000-000000000000 | output -> test", result)
assert.Equal(t, "{HORUSEC_CLI} Error to execute tool Bandit | analysisID -> 00000000-0000-0000-0000-000000000000 | output -> test", result)
})
}

Expand Down Expand Up @@ -219,7 +218,7 @@ func TestLogDebugWithReplace(t *testing.T) {
assert.NotPanics(t, func() {
monitorController.LogDebugWithReplace(messages.MsgDebugToolStartAnalysis, tools.NpmAudit, languages.Javascript)
})
assert.Contains(t, stdOutMock.String(), `level=debug msg="{HORUSEC_CLI} Running NpmAudit - JavaScript in analysisID: [00000000-0000-0000-0000-000000000000]`)
assert.Contains(t, stdOutMock.String(), `level=debug msg="{HORUSEC_CLI} Running NpmAudit - JavaScript in analysisID: 00000000-0000-0000-0000-000000000000`)
})
}

Expand All @@ -241,7 +240,7 @@ func TestLogAnalysisError(t *testing.T) {
monitorController.SetAnalysisError(errors.New("test"), tools.GoSec, "container err", "")
monitorController.SetAnalysisError(errors.New("test2"), tools.GitLeaks, "container err", "")
})
assert.Contains(t, stdOutMock.String(), `{HORUSEC_CLI} Something error went wrong in GoSec tool | analysisID -> 00000000-0000-0000-0000-000000000000 | output -> container err`)
assert.Contains(t, stdOutMock.String(), `{HORUSEC_CLI} Error to execute tool GoSec | analysisID -> 00000000-0000-0000-0000-000000000000 | output -> container err`)
})
t.Run("should not panic when logging error and exists projectSubPath", func(t *testing.T) {
monitorController := NewFormatterService(&analysis.Analysis{}, testutil.NewDockerMock(), &config.Config{})
Expand All @@ -252,7 +251,7 @@ func TestLogAnalysisError(t *testing.T) {
monitorController.SetAnalysisError(errors.New("test"), tools.GoSec, "container err", "/tmp")
monitorController.SetAnalysisError(errors.New("test2"), tools.GitLeaks, "container err", "/tmp")
})
assert.Contains(t, stdOutMock.String(), `{HORUSEC_CLI} Something error went wrong in GoSec tool | analysisID -> 00000000-0000-0000-0000-000000000000 | output -> container err | ProjectSubPath -> /tmp - test"`)
assert.Contains(t, stdOutMock.String(), `{HORUSEC_CLI} Error to execute tool GoSec | analysisID -> 00000000-0000-0000-0000-000000000000 | output -> container err | ProjectSubPath -> /tmp - test"`)
})
}

Expand Down