Skip to content

Commit

Permalink
Fixes #23240: Windows technique generated function name is incoherent…
Browse files Browse the repository at this point in the history
… with the generated directives
  • Loading branch information
amousset committed Aug 9, 2023
1 parent 6f66ad9 commit e9b9483
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 66 deletions.
33 changes: 4 additions & 29 deletions policies/rudderc/src/backends/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct TechniqueTemplate<'a> {
}

/// Filters for the technique template
mod filters {
pub mod filters {
use std::fmt::Display;

use anyhow::Error;
Expand Down Expand Up @@ -84,7 +84,7 @@ mod filters {
}

/// `my_method` -> `MyMethod`
pub fn _camel_case<T: Display>(s: T) -> askama::Result<String> {
pub fn camel_case<T: Display>(s: T) -> askama::Result<String> {
Ok(s.to_string()
.split('_')
.map(uppercase_first_letter)
Expand Down Expand Up @@ -176,7 +176,7 @@ fn method_call(m: Method, condition: Condition) -> Result<WindowsMethod> {
Some(condition.to_string())
},
args,
name: Windows::pascal_case(&m.info.as_ref().unwrap().bundle_name),
name: filters::dsc_case(&m.info.as_ref().unwrap().bundle_name).unwrap(),
})
}

Expand All @@ -185,33 +185,8 @@ impl Windows {
Self
}

fn pascal_case(s: &str) -> String {
let chars = s.chars();

let mut pascal = String::new();
let mut is_next_uppercase = true;
for c in chars {
let next = match c {
' ' | '_' | '-' => {
is_next_uppercase = true;
String::new()
}
c => {
if is_next_uppercase {
is_next_uppercase = false;
c.to_uppercase().to_string()
} else {
c.to_string()
}
}
};
pascal.push_str(&next);
}
pascal
}

pub fn technique_name(s: &str) -> String {
Self::pascal_case(s)
filters::dsc_case(s).unwrap()
}

fn technique(src: Technique, resources: &Path) -> Result<String> {
Expand Down
6 changes: 3 additions & 3 deletions policies/rudderc/templates/technique.ps1.askama
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function {{ id|dsc_case }} {
ComponentName = "{{ m.component_name|escape_double_quotes }}"
PolicyMode = $policyMode
ReportId = $reportId
DisableReporting = {{ m.disable_reporting }}
DisableReporting = ${{ m.disable_reporting }}
TechniqueName = $techniqueName
}
{% match m.condition %}
Expand All @@ -46,7 +46,7 @@ function {{ id|dsc_case }} {
{{- arg.0|dsc_case }} = {{ arg|parameter_fmt }}
{% endfor %}
}
$call = {{ m.name|dsc_case }} $methodParams -PolicyMode $policyMode
$call = {{ m.name|dsc_case }} @methodParams -PolicyMode $policyMode
$methodContext = Compute-Method-Call @reportParams -MethodCall $call
$localContext.merge($methodContext)
} else {
Expand All @@ -58,7 +58,7 @@ function {{ id|dsc_case }} {
{{- arg.0|dsc_case }} = "{{ arg.1 }}"
{% endfor %}
}
$call = {{ m.name }} $methodParams -PolicyMode $policyMode
$call = {{ m.name }} @methodParams -PolicyMode $policyMode
$methodContext = Compute-Method-Call @reportParams -MethodCall $call
$localContext.merge($methodContext)
{% endmatch %}
Expand Down
4 changes: 2 additions & 2 deletions policies/rudderc/tests/cases/general/escaping/technique.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
ComponentName = "${sys.host} . | / ${sys.${host}} ' '' ''' $ $$ `" `"`" \ \\😋aà3"
PolicyMode = $policyMode
ReportId = $reportId
DisableReporting = false
DisableReporting = $false
TechniqueName = $techniqueName
}
Expand All @@ -44,7 +44,7 @@
'@
}
$call = PackagePresent $methodParams -PolicyMode $policyMode
$call = Package-Present @methodParams -PolicyMode $policyMode
$methodContext = Compute-Method-Call @reportParams -MethodCall $call
$localContext.merge($methodContext)
} else {
Expand Down
4 changes: 2 additions & 2 deletions policies/rudderc/tests/cases/general/form/technique.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
ComponentName = "Ensure correct ntp configuration"
PolicyMode = $policyMode
ReportId = $reportId
DisableReporting = false
DisableReporting = $false
TechniqueName = $techniqueName
}

Expand All @@ -83,7 +83,7 @@
'@

}
$call = PackagePresent $methodParams -PolicyMode $policyMode
$call = Package-Present @methodParams -PolicyMode $policyMode
$methodContext = Compute-Method-Call @reportParams -MethodCall $call
$localContext.merge($methodContext)
} else {
Expand Down
4 changes: 2 additions & 2 deletions policies/rudderc/tests/cases/general/min/technique.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
ComponentName = "Package present"
PolicyMode = $policyMode
ReportId = $reportId
DisableReporting = false
DisableReporting = $false
TechniqueName = $techniqueName
}

Expand All @@ -37,7 +37,7 @@
Version = "2.3.4"

}
$call = PackagePresent $methodParams -PolicyMode $policyMode
$call = Package-Present @methodParams -PolicyMode $policyMode
$methodContext = Compute-Method-Call @reportParams -MethodCall $call
$localContext.merge($methodContext)

Expand Down
8 changes: 4 additions & 4 deletions policies/rudderc/tests/cases/general/ntp/metadata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@
<POLICYGENERATION>separated-with-parameters</POLICYGENERATION>
<AGENT type="cfengine-community">
<BUNDLES>
<NAME>ntp</NAME>
<NAME>ntp_technique</NAME>
</BUNDLES>
<FILES>
<FILE name="technique.cf">
<INCLUDED>true</INCLUDED>
</FILE>
<FILE name="resources/file.cfg">
<INCLUDED>false</INCLUDED>
<OUTPATH>ntp/0.1/resources/file.cfg</OUTPATH>
<OUTPATH>ntp_technique/0.1/resources/file.cfg</OUTPATH>
</FILE>
</FILES>
</AGENT>
<AGENT type="dsc">
<BUNDLES>
<NAME>Ntp</NAME>
<NAME>Ntp-Technique</NAME>
</BUNDLES>
<FILES>
<FILE name="technique.ps1">
<INCLUDED>true</INCLUDED>
</FILE>
<FILE name="resources/file.cfg">
<INCLUDED>false</INCLUDED>
<OUTPATH>ntp/0.1/resources/file.cfg</OUTPATH>
<OUTPATH>ntp_technique/0.1/resources/file.cfg</OUTPATH>
</FILE>
</FILES>
</AGENT>
Expand Down
4 changes: 2 additions & 2 deletions policies/rudderc/tests/cases/general/ntp/technique.cf
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# @name NTP
# @version 0.1

bundle agent ntp(server) {
bundle agent ntp_technique(server) {

vars:
"resources_dir" string => "${this.promise_dirname}/resources";

"args" slist => {"${server}"};
"report_param" string => join("_", args);
"full_class_prefix" string => canonify("ntp_${report_param}");
"full_class_prefix" string => canonify("ntp_technique_${report_param}");
"class_prefix" string => string_head("${full_class_prefix}", "1000");

methods:
Expand Down
10 changes: 5 additions & 5 deletions policies/rudderc/tests/cases/general/ntp/technique.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Ntp {
function Ntp-Technique {
[CmdletBinding()]
param (
[parameter(Mandatory = $true)]
Expand Down Expand Up @@ -31,7 +31,7 @@
ComponentName = "Ensure correct ntp configuration"
PolicyMode = $policyMode
ReportId = $reportId
DisableReporting = false
DisableReporting = $false
TechniqueName = $techniqueName
}

Expand All @@ -46,7 +46,7 @@
'@

}
$call = PackagePresent $methodParams -PolicyMode $policyMode
$call = Package-Present @methodParams -PolicyMode $policyMode
$methodContext = Compute-Method-Call @reportParams -MethodCall $call
$localContext.merge($methodContext)
} else {
Expand All @@ -61,7 +61,7 @@
ComponentName = "NTP service"
PolicyMode = $policyMode
ReportId = $reportId
DisableReporting = false
DisableReporting = $false
TechniqueName = $techniqueName
}

Expand All @@ -71,7 +71,7 @@
Name = "/bin/true `"# $($node.inventory[os][fullName])`""

}
$call = PackageInstall $methodParams -PolicyMode $policyMode
$call = Package-Install @methodParams -PolicyMode $policyMode
$methodContext = Compute-Method-Call @reportParams -MethodCall $call
$localContext.merge($methodContext)
} else {
Expand Down
2 changes: 1 addition & 1 deletion policies/rudderc/tests/cases/general/ntp/technique.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
id: ntp
id: ntp_technique
name: NTP
version: "0.1"
params:
Expand Down
32 changes: 16 additions & 16 deletions policies/rudderc/tests/cases/general/reporting/technique.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
ComponentName = "No block without condition"
PolicyMode = $policyMode
ReportId = $reportId
DisableReporting = false
DisableReporting = $false
TechniqueName = $techniqueName
}

Expand All @@ -37,7 +37,7 @@
Version = ""

}
$call = PackagePresent $methodParams -PolicyMode $policyMode
$call = Package-Present @methodParams -PolicyMode $policyMode
$methodContext = Compute-Method-Call @reportParams -MethodCall $call
$localContext.merge($methodContext)

Expand All @@ -50,7 +50,7 @@
ComponentName = "No block with condition"
PolicyMode = $policyMode
ReportId = $reportId
DisableReporting = false
DisableReporting = $false
TechniqueName = $techniqueName
}

Expand All @@ -65,7 +65,7 @@
'@

}
$call = PackagePresent $methodParams -PolicyMode $policyMode
$call = Package-Present @methodParams -PolicyMode $policyMode
$methodContext = Compute-Method-Call @reportParams -MethodCall $call
$localContext.merge($methodContext)
} else {
Expand All @@ -80,7 +80,7 @@
ComponentName = "NTP service"
PolicyMode = $policyMode
ReportId = $reportId
DisableReporting = false
DisableReporting = $false
TechniqueName = $techniqueName
}

Expand All @@ -91,7 +91,7 @@
Version = ""

}
$call = PackagePresent $methodParams -PolicyMode $policyMode
$call = Package-Present @methodParams -PolicyMode $policyMode
$methodContext = Compute-Method-Call @reportParams -MethodCall $call
$localContext.merge($methodContext)

Expand All @@ -104,7 +104,7 @@
ComponentName = "NTP service"
PolicyMode = $policyMode
ReportId = $reportId
DisableReporting = false
DisableReporting = $false
TechniqueName = $techniqueName
}

Expand All @@ -115,7 +115,7 @@
Version = ""

}
$call = PackagePresent $methodParams -PolicyMode $policyMode
$call = Package-Present @methodParams -PolicyMode $policyMode
$methodContext = Compute-Method-Call @reportParams -MethodCall $call
$localContext.merge($methodContext)

Expand All @@ -128,7 +128,7 @@
ComponentName = "NTP service"
PolicyMode = $policyMode
ReportId = $reportId
DisableReporting = false
DisableReporting = $false
TechniqueName = $techniqueName
}

Expand All @@ -139,7 +139,7 @@
Version = ""

}
$call = PackagePresent $methodParams -PolicyMode $policyMode
$call = Package-Present @methodParams -PolicyMode $policyMode
$methodContext = Compute-Method-Call @reportParams -MethodCall $call
$localContext.merge($methodContext)

Expand All @@ -152,7 +152,7 @@
ComponentName = "NTP service"
PolicyMode = $policyMode
ReportId = $reportId
DisableReporting = false
DisableReporting = $false
TechniqueName = $techniqueName
}

Expand All @@ -163,7 +163,7 @@
Version = ""

}
$call = PackagePresent $methodParams -PolicyMode $policyMode
$call = Package-Present @methodParams -PolicyMode $policyMode
$methodContext = Compute-Method-Call @reportParams -MethodCall $call
$localContext.merge($methodContext)

Expand All @@ -176,7 +176,7 @@
ComponentName = "Enabled reporting"
PolicyMode = $policyMode
ReportId = $reportId
DisableReporting = false
DisableReporting = $false
TechniqueName = $techniqueName
}

Expand All @@ -187,7 +187,7 @@
Version = ""

}
$call = PackagePresent $methodParams -PolicyMode $policyMode
$call = Package-Present @methodParams -PolicyMode $policyMode
$methodContext = Compute-Method-Call @reportParams -MethodCall $call
$localContext.merge($methodContext)

Expand All @@ -200,7 +200,7 @@
ComponentName = "Disabled reporting"
PolicyMode = $policyMode
ReportId = $reportId
DisableReporting = true
DisableReporting = $true
TechniqueName = $techniqueName
}

Expand All @@ -211,7 +211,7 @@
Version = ""

}
$call = PackagePresent $methodParams -PolicyMode $policyMode
$call = Package-Present @methodParams -PolicyMode $policyMode
$methodContext = Compute-Method-Call @reportParams -MethodCall $call
$localContext.merge($methodContext)

Expand Down

0 comments on commit e9b9483

Please sign in to comment.