Skip to content

Commit

Permalink
Updates to .conf file checks
Browse files Browse the repository at this point in the history
Some .conf file were not checking corretly with prefex stanzas. splunk#22, splunk#24, splunk#28
  • Loading branch information
JasonConger committed Jul 24, 2020
1 parent 7652693 commit 30a9328
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
35 changes: 24 additions & 11 deletions out/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ const BLANK_LINE_REGEX = /^\s*\n/gm
const DEFAULT_STANZA_REGEX = /^# Use the \[default\] stanza/

const STANZA_REGEX = /^\[(?<stanza>[^\]].*?)\]/
const STANZA_PREFIX_REGEX = /^\[(?<prefix>[^\]].*?(:|::|::...))[\<|\w|\/]/ // matches things like [tcp:<port>], [tcp:123], [source::...a...], [tcp://<remote server>:<port>], or [tcp://123]
const STANZA_PREFIX_REGEX = /^\[(?<prefix>[^\]].*?(=|:|::|::...|_))[\<|\w|\/]/ // matches things like [author=<name>], [tcp:<port>], [tcp:123], [source::...a...], [tcp://<remote server>:<port>], or [tcp://123]
const STANZA_FREEFORM_REGEX = /^\[\<(?<stanza>.*?)\>\]/ // matches things like [<spec>] or [<custom_alert_action>]
const STANZA_ABSOLUTE_REGEX = /^\[(?<stanza>[^\<\>\:\/]+)\]/ // matches things like [tcp] or [SSL] (does not allow <, >, :, or /)

const SETTING_REGEX = /^(?<setting>\w.*?)\s*=\s*(?<value>[^\r\n]+)/
const SETTING_PREFIX_REGEX = /^(?<prefix>[^-\.].*?[-|\.])\<.*?\>/
const SETTING_PREFIX_REGEX = /^(?<prefix>[^-\.].*?)\<.*?\>/

const lineTypes = {
DEFAULT_STANZA: 'defaultStanza',
Expand Down Expand Up @@ -85,8 +85,8 @@ function parse (str, name) {
let stanza = createStanza(section)

// Some spec files can create empty default stanzas if the spec file explicitlly defines [default].
// limits.conf.spec does this for example
// In these cases, a default stanza can be produced with no settings
// limits.conf.spec does this for example.
// In these cases, a default stanza can be produced with no settings.

if(stanza["stanzaName"] != "default") {
specConfig["stanzas"].push(stanza)
Expand All @@ -103,6 +103,7 @@ function parse (str, name) {
str = str.replace(BLANK_LINE_REGEX, "")

}

return specConfig
}

Expand Down Expand Up @@ -133,6 +134,7 @@ function createStanza (str) {
let lines = str.split(/[\r\n]+/g)
let defaultStanzaCreated = false


lines.forEach(function (line) {

let lineType = lineTypes.UNKNOWN
Expand Down Expand Up @@ -255,10 +257,10 @@ function getStanzaType(stanza) {

let stanzaType = stanzaTypes.UNKNOWN

if (STANZA_PREFIX_REGEX.test(stanza)) {
stanzaType = stanzaTypes.PREFIX
} else if (STANZA_FREEFORM_REGEX.test(stanza)) {
if (STANZA_FREEFORM_REGEX.test(stanza)) {
stanzaType = stanzaTypes.FREEFORM
} else if (STANZA_PREFIX_REGEX.test(stanza)) {
stanzaType = stanzaTypes.PREFIX
} else if (STANZA_ABSOLUTE_REGEX.test(stanza)) {
stanzaType = stanzaTypes.ABSOLUTE
}
Expand All @@ -274,22 +276,33 @@ function getStanzaSettings(specConfig, stanzaName) {
// * freeform - examples: [my:sourcetype], [my_alert_action], [foo] (if the spec allows freeform)

let settings = []

// Special case for indexes.conf - see https://github.com/splunk/vscode-extension-splunk/issues/23
// All the settings in indexes.conf are valid, so return them all.
if(specConfig.specName == "indexes.conf.spec") {
let settings = []
for (var i=0; i < specConfig["stanzas"].length; i++) {
settings.push(...specConfig["stanzas"][i].settings)
}
return settings
}

let defaultSettings = getStanzaSettingsByStanzaName(specConfig, "[default]")
let stanzaType = getStanzaType(stanzaName)
if((stanzaName == "[default]") && (!specConfig.allowsFreeformStanzas)) { return defaultSettings }

switch(stanzaType) {

case stanzaTypes.PREFIX: {
// There are 2 types of prefix stanzas:
// 1) [tcp:port]
// 2) [tcp://port]
let colonMatch = /^\[(?<prefix>[^:].*?:)[\w|\<]/ // Example: [tcp:test] OR [tcp:<...
let uriMatch = /^\[(?<prefix>[^:].*?:\/\/)[\w|\<]/ // Example: [tcp://test] OR [tcp://<...
let uriMatch = /^\[(?<prefix>[^:].*?:\/\/)[\w|\<|\/]/ // Example: [tcp://test] OR [tcp://<... OR [script:///]
let sepMatch = /^\[(?<prefix>.*?[=|_|:])[\w|\<|\/]/ // Example [author=test] OR [role_name]
if (uriMatch.test(stanzaName)) {
settings = getStanzaSettingsByPrefix(specConfig, stanzaName, uriMatch)
} else if (colonMatch.test(stanzaName)) {
settings = getStanzaSettingsByPrefix(specConfig, stanzaName, colonMatch)
} else if (sepMatch.test(stanzaName)) {
settings = getStanzaSettingsByPrefix(specConfig, stanzaName, sepMatch)
} else {
settings = null
}
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
{
"name": "splunk",
"version": "0.2.3",
"version": "0.2.4",
"publisher": "Splunk",
"engines": {
"vscode": "^1.40.0"
},
"license": "MIT",
"displayName": "Splunk Extension",
"description": "Splunk Python Debugging (local, remote), Linting, IntelliSense, code formatting, and more.",
"description": "Splunk Python Debugging (local, remote), Linting, IntelliSense, Snippets, Templates.",
"categories": [
"Programming Languages",
"Linters",
"Snippets",
"Debuggers",
"Formatters",
"Other"
],
"keywords": [
Expand Down
Empty file removed snippets/snippets.json
Empty file.

0 comments on commit 30a9328

Please sign in to comment.