Skip to content

Commit 6d85dd2

Browse files
committed
Merge branch 'branches/rudder/8.0'
2 parents d709e31 + a01d290 commit 6d85dd2

File tree

7 files changed

+74
-46
lines changed

7 files changed

+74
-46
lines changed

policies/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
/rudderc/repos
77
/rudderc/docs/examples/*.cf
88
/rudderc/docs/examples/*.ps1
9-
rudderc/src/methods.json
9+
rudderc/src/methods.json
10+
/rudderc/tests/cases/general/*/technique.ids.yml

policies/rudderc/docs/src/usage.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ To open the documentation in your browser after build, pass the `--open` option.
115115
You can export your current technique with:
116116

117117
```shell
118-
$ rudderc export
118+
$ rudderc build --export
119119
Writing ./target/ntp_technique-0.1.zip
120120
```
121121

policies/rudderc/src/cli.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ pub enum Command {
6161
#[arg(long)]
6262
standalone: bool,
6363

64+
/// Export as an archive for import into a Rudder server
65+
#[arg(long)]
66+
export: bool,
67+
6468
/// Add ids to the source technique. This will also reformat the file.
6569
#[arg(long)]
6670
store_ids: bool,
@@ -88,15 +92,6 @@ pub enum Command {
8892
agent_verbose: bool,
8993
},
9094

91-
/// Export as an archive for import into a Rudder server
92-
Export {
93-
/// Output file
94-
///
95-
/// Defaults to the target directory.
96-
#[arg(short, long)]
97-
output: Option<PathBuf>,
98-
},
99-
10095
/// Build the method documentation
10196
Lib {
10297
/// Load a library from the given path Uses `/var/rudder/ncf` if not paths were provided.

policies/rudderc/src/ir/condition.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,18 @@ use std::{fmt, str::FromStr};
55

66
use anyhow::{bail, Error};
77
use rudder_commons::is_canonified;
8-
use serde::{de, Deserialize, Deserializer, Serialize};
8+
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
99
use serde_yaml::Value;
1010

1111
/// Valid condition
1212
///
1313
/// Simple representation to allow trivial optimizations. At some point we might add
1414
/// a real condition evaluator.
15-
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
16-
#[serde(untagged)]
15+
#[derive(Clone, Debug, PartialEq, Eq)]
1716
pub enum Condition {
1817
/// a.k.a. "true" / "any"
19-
#[serde(rename = "true")]
2018
Defined,
2119
/// a.k.a. "false" / "!any"
22-
#[serde(rename = "false")]
2320
NotDefined,
2421
/// Condition expression that will be evaluated at runtime
2522
Expression(String),
@@ -132,6 +129,20 @@ impl FromStr for Condition {
132129
}
133130
}
134131

132+
impl Serialize for Condition {
133+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
134+
where
135+
S: Serializer,
136+
{
137+
// 3 is the number of fields in the struct.
138+
serializer.serialize_str(match self {
139+
Condition::Defined => "true",
140+
Condition::NotDefined => "false",
141+
Condition::Expression(e) => e,
142+
})
143+
}
144+
}
145+
135146
impl<'de> Deserialize<'de> for Condition {
136147
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
137148
where

policies/rudderc/src/lib.rs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ pub fn run(args: MainArgs) -> Result<()> {
106106
library,
107107
output,
108108
standalone,
109+
export,
109110
store_ids,
110111
} => {
111112
let library = check_libraries(library)?;
@@ -115,8 +116,12 @@ pub fn run(args: MainArgs) -> Result<()> {
115116
input,
116117
actual_output.as_path(),
117118
standalone,
118-
store_ids,
119-
)
119+
store_ids || export,
120+
)?;
121+
if export {
122+
action::export(&cwd, actual_output)?;
123+
}
124+
Ok(())
120125
}
121126
Command::Test {
122127
library,
@@ -136,7 +141,6 @@ pub fn run(args: MainArgs) -> Result<()> {
136141
agent_verbose,
137142
)
138143
}
139-
Command::Export { output } => action::export(&cwd, output),
140144
Command::Lib {
141145
library,
142146
output,
@@ -179,7 +183,7 @@ pub mod action {
179183
frontends::read_methods,
180184
ir::Technique,
181185
test::TestCase,
182-
METADATA_FILE, RESOURCES_DIR, TARGET_DIR, TECHNIQUE, TECHNIQUE_SRC, TESTS_DIR,
186+
METADATA_FILE, RESOURCES_DIR, TECHNIQUE, TECHNIQUE_SRC, TESTS_DIR,
183187
};
184188

185189
/// Create a technique skeleton
@@ -463,26 +467,28 @@ pub mod action {
463467
Ok(())
464468
}
465469

466-
pub fn export(src: &Path, output: Option<PathBuf>) -> Result<()> {
470+
pub fn export(src: &Path, dir: PathBuf) -> Result<()> {
467471
// We don't need to parse everything, let's just extract what we need
468-
let technique_src = src.join(TECHNIQUE_SRC);
469-
let yml: serde_yaml::Value = serde_yaml::from_str(&read_to_string(&technique_src)?)?;
472+
// We use the technique with ids
473+
let technique_src = src.join(TECHNIQUE_SRC).with_extension("ids.yml");
474+
let yml: serde_yaml::Value =
475+
serde_yaml::from_str(&read_to_string(&technique_src).context(format!(
476+
"Could not read source technique {}",
477+
technique_src.display()
478+
))?)?;
470479
let id = yml.get("id").unwrap().as_str().unwrap();
471480
let version = yml.get("version").unwrap().as_str().unwrap();
472481
let category = yml
473482
.get("category")
474483
.map(|c| c.as_str().unwrap())
475484
.unwrap_or("ncf_techniques");
476-
let actual_output = match output {
477-
Some(p) => p,
478-
None => {
479-
let dir = src.join(TARGET_DIR);
480-
create_dir_all(&dir)?;
481-
dir.join(format!("{}-{}.zip", id, version))
482-
}
483-
};
485+
create_dir_all(&dir).context(format!("Creating output directory {}", dir.display()))?;
486+
let actual_output = dir.join(format!("{}-{}.zip", id, version));
484487

485-
let file = File::create(&actual_output)?;
488+
let file = File::create(&actual_output).context(format!(
489+
"Creating export output file {}",
490+
&actual_output.display()
491+
))?;
486492
let options = zip::write::FileOptions::default();
487493
let mut zip = ZipWriter::new(file);
488494

@@ -491,7 +497,10 @@ pub mod action {
491497
// Technique
492498
zip.start_file(format!("{}/{}", zip_dir, TECHNIQUE_SRC), options)?;
493499
let mut buffer = Vec::new();
494-
let mut f = File::open(technique_src)?;
500+
let mut f = File::open(&technique_src).context(format!(
501+
"Opening technique source {}",
502+
technique_src.display()
503+
))?;
495504
f.read_to_end(&mut buffer)?;
496505
zip.write_all(&buffer)?;
497506

policies/rudderc/tests/compile.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub fn init_logs() {
3131
const TEST_METHODS: &str = "tests/lib/common/30_generic_methods";
3232

3333
/// Compiles all files in `cases/general`. Files ending in `.fail.yml` are expected to fail.
34-
#[test_resources("tests/cases/general/*/*.yml")]
34+
#[test_resources("tests/cases/general/*/technique.yml")]
3535
fn compile(filename: &str) {
3636
init_logs();
3737
let input = read_to_string(filename).unwrap();

policies/rudderc/tests/export.rs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
// SPDX-License-Identifier: GPL-3.0-or-later
22
// SPDX-FileCopyrightText: 2019-2020 Normation SAS
33

4-
use std::path::Path;
4+
use std::path::{Path, PathBuf};
5+
use test_generator::test_resources;
56

67
use rudderc::action;
78

8-
#[test]
9-
fn test_export_resources() {
10-
let technique_dir = Path::new("./tests/cases/general/ntp");
11-
let res = action::export(technique_dir, None);
12-
res.unwrap();
13-
}
14-
15-
#[test]
16-
fn test_export_without_resources() {
17-
let technique_dir = Path::new("./tests/cases/general/min");
18-
let res = action::export(technique_dir, None);
9+
#[test_resources("tests/cases/general/*/technique.yml")]
10+
fn compile(filename: &str) {
11+
// One example with resource, one example without
12+
let technique_dir = Path::new(filename).parent().unwrap();
13+
action::build(
14+
&[PathBuf::from("tests/lib/common")],
15+
&technique_dir.join("technique.yml"),
16+
&technique_dir.join("target"),
17+
false,
18+
true,
19+
)
20+
.unwrap();
21+
// Check that the generated technique is correct
22+
action::build(
23+
&[PathBuf::from("tests/lib/common")],
24+
&technique_dir.join("technique.ids.yml"),
25+
&technique_dir.join("target"),
26+
false,
27+
false,
28+
)
29+
.unwrap();
30+
let res = action::export(technique_dir, technique_dir.join("target"));
1931
res.unwrap();
2032
}

0 commit comments

Comments
 (0)