Skip to content

Commit

Permalink
fix: Handle // comments
Browse files Browse the repository at this point in the history
  • Loading branch information
daffinito committed Oct 17, 2023
1 parent 1e73169 commit a31d39f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
2 changes: 1 addition & 1 deletion suites/suiteDefinitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var suiteDefinitions = []Suite{
{
Identifier: "infra:debug",
DisplayName: "Infrastructure Agent (Debug)",
Description: "Infrastructure Agent installation with 3 minutes of debug log collection",
Description: "Infrastructure Agent installation with 5 minutes of debug log collection",
Tasks: []string{
"Base/*",
"Infra/*",
Expand Down
2 changes: 1 addition & 1 deletion tasks/base/config/integrationTests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
docker_cmd: ./nrdiag && cat nrdiag-output.json
- test_name: ProxyDetectNode
dockerfile_lines:
- COPY tasks/fixtures/integration/proxyDetect/newrelicnode /app/newrelic.js
- COPY tasks/fixtures/integration/proxyDetect/newrelicnode.js /app/newrelic.js
log_entry_expected:
- Success.*Base/Config/ProxyDetect
- http://user:pass@10.0.0.1:8000
Expand Down
29 changes: 25 additions & 4 deletions tasks/base/config/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,33 @@ func ParseJSONarray(reader io.Reader) (result []tasks.ValidateBlob, err error) {
// formatJs - Outputs the newrelic.js file into a more parsable format with comments and other non-essential items removed
func formatJs(jsString string) ([]string, error) {
// remove comments that start with //
slashCommentRe, err := regexp.Compile("(?m)[/][/].*$")
// have to be careful not to remove proxy configs
slashCommentRe, err := regexp.Compile(`(?m)(^.*)([/][/].*)$`)
if err != nil {
return nil, err
}
removeSlashComments := slashCommentRe.ReplaceAllString(jsString, "")

singleQuoteRe, err := regexp.Compile(`[']`)
if err != nil {
return nil, err
}
removeSlashComments := slashCommentRe.ReplaceAllFunc([]byte(jsString), func(b []byte) []byte {
s := string(b)
checkForComment := strings.Split(s, "//")
// nothing on the left of the //, whole line is a comment, just remove it
if checkForComment[0] == "" {
return nil
}
// check to see if the // is within quotes like 'http://...'
quoteCount := len(singleQuoteRe.FindAllStringIndex(checkForComment[0], -1))
if quoteCount == 0 || quoteCount%2 == 0 {
// not in quotes
return []byte(checkForComment[0])
}
// keep the //, it was in quotes
return b
})
// remove \n and \r
removeLineBreaks := strings.ReplaceAll(removeSlashComments, "\n", "")
removeLineBreaks := strings.ReplaceAll(string(removeSlashComments), "\n", "")
removeCarriageReturn := strings.ReplaceAll(removeLineBreaks, "\r", "")

// remove everything before exports.config =
Expand Down Expand Up @@ -346,6 +365,7 @@ func parseJs(rawFile io.Reader) (result tasks.ValidateBlob, err error) {
if err != nil {
return
}
log.Debug("Formatted js: ", strings.Join(jsonString, "\n"))
tempMap := make(map[string]interface{})

location := ""
Expand All @@ -354,6 +374,7 @@ func parseJs(rawFile io.Reader) (result tasks.ValidateBlob, err error) {

loop:
for lineNum, keyValue := range jsonString {
log.Debugf("keyValue: `%s`", keyValue)
switch keyValue {
case "{": //do nothing here
case "}", "},":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
* See lib/config.defaults.js in the agent distribution for a more complete
* description of configuration variables and their potential values.
*/
exports.config = {
// slash comment
exports.config = { // slash comment
/**
* Array of application names.
*/
app_name: ['My Node App'],
/**
* Your New Relic license key.
*/
license_key: 'license-key-val-node',
license_key: 'license-key-val-node',//comment
proxy: 'http://user:pass@10.0.0.1:8000',
logging: {
/**
Expand Down

0 comments on commit a31d39f

Please sign in to comment.