Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
gpoblon committed Sep 14, 2020
1 parent 9e3ac77 commit 0d23aba
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 16 deletions.
3 changes: 1 addition & 2 deletions rudder-lang/src/bin/rudderc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fn main() {
let command = Opt::from_args();
let (output, log_level, has_backtrace) = command.extract_logging_infos();
let action = command.to_action();
// Initialize logger
// Initialize logger and output
output.init(log_level, action, has_backtrace);

let ctx = command.extract_parameters().unwrap_or_else(|e| {
Expand All @@ -94,7 +94,6 @@ fn main() {
Action::Compile => compile_file(&ctx, true),
// TODO Migrate: call cf_to_json perl script then call json->rl == Technique generate()
Action::Migrate => migrate(&ctx),
// TODO: rl -> json + add a json wrapper : { data: {}, errors: {}}
Action::ReadTechnique => read_technique(&ctx),
// TODO Generate: call technique generate then compile into all formats + json wrapper: { rl: "", dsc: "", cf: "", errors:{} }
Action::GenerateTechnique => generate_technique(&ctx),
Expand Down
2 changes: 1 addition & 1 deletion rudder-lang/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: 2019-2020 Normation SAS

use crate::logger::Backtrace;
use crate::output::Backtrace;
/// We write our own error type to have a consistent error type through all our code.
/// We translate other types to this one when necessary.
/// All case contain 4 elements:
Expand Down
2 changes: 1 addition & 1 deletion rudder-lang/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub mod migrate;
pub mod opt;
pub use generator::Format;
pub mod cfstrings;
pub mod logger;
pub mod output;
mod parser;
pub mod rudderlang_lib;
pub mod technique;
Expand Down
8 changes: 8 additions & 0 deletions rudder-lang/src/migrate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use crate::{error::Result, io::IOContext, ActionResult};

// TODO Migrate: call cf_to_json perl script then call json->rl == Technique generate()
/// Generates RudderLang from CFEngine
pub fn migrate(ctx: &IOContext) -> Result<Vec<ActionResult>> {
// Ok(vec![ActionResult::default()])
unimplemented!()
}
23 changes: 14 additions & 9 deletions rudder-lang/src/opt.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: 2019-2020 Normation SAS

use crate::{error::Result, generator::Format, io::IOContext, logger::Output, Action};
use crate::{error::Result, generator::Format, io::IOContext, output::Output, Action};
use log::LevelFilter;
use std::path::PathBuf;
use structopt::StructOpt;
Expand All @@ -24,7 +24,7 @@ pub enum Opt {

/// Use json logs instead of human readable output
#[structopt(long, short)]
json_log: bool,
json_output: bool,
},
/// Generates either a DSC / CFEngine technique (`--format` option) from a RudderLang technique
Compile {
Expand All @@ -33,7 +33,7 @@ pub enum Opt {

/// Use json logs instead of human readable output
#[structopt(long, short)]
json_log: bool,
json_output: bool,

/// Enforce a compiler output format (overrides configuration format)
/// Handled formats: [ "cf" (alias "cfengine"), "dsc", "json" ]
Expand All @@ -45,20 +45,25 @@ impl Opt {
pub fn extract_logging_infos(&self) -> (Output, LevelFilter, bool) {
match self {
Self::Compile {
options, json_log, ..
options,
json_output,
..
} => {
let logger = match json_log {
let output = match json_output {
true => Output::JSON,
false => Output::Terminal,
};
(logger, options.log_level, options.backtrace)
(output, options.log_level, options.backtrace)
}
Self::Migrate { options, json_log } => {
let logger = match json_log {
Self::Migrate {
options,
json_output,
} => {
let output = match json_output {
true => Output::JSON,
false => Output::Terminal,
};
(logger, options.log_level, options.backtrace)
(output, options.log_level, options.backtrace)
}
Self::Technique(t) => t.extract_logging_infos(),
}
Expand Down
6 changes: 3 additions & 3 deletions rudder-lang/src/logger.rs → rudder-lang/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ impl Backtrace {
(Some(start), None) => start.as_str().to_owned().into(),
})
.and_then(|fmt_name| {
// do not put logger in the backtrace since it always ultimately calls panic_hook and print_backtrace
if fmt_name.starts_with("rudderc::logger")
// do not put output related calls in the backtrace since it always ultimately calls panic_hook and print_backtrace
if fmt_name.starts_with("rudderc::output")
|| fmt_name.starts_with("rudderc::error::Error::new")
{
return None;
Expand Down Expand Up @@ -142,7 +142,7 @@ pub enum Output {
JSON,
}
impl Output {
/// Initialize the logger
/// Initialize the output generator
pub fn init(self, log_level: LevelFilter, action: Action, backtrace_enabled: bool) {
// Content called when panic! is encountered to close logger brackets and print error
Self::set_panic_hook(self, action, backtrace_enabled);
Expand Down
6 changes: 6 additions & 0 deletions rudder-lang/src/technique/generate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use crate::{error::Result, io::IOContext, ActionResult};

pub fn generate_technique(ctx: &IOContext) -> Result<Vec<ActionResult>> {
// Ok(vec![ActionResult::default()])
unimplemented!()
}
16 changes: 16 additions & 0 deletions rudder-lang/src/technique/read.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::{
compile::technique_to_ir, error::Result, generator::Format, io::IOContext,
technique::Technique, ActionResult,
};
use typed_arena::Arena;

/// takes a RudderLang technique, and generate a JSON technique that wraps it up
// TODO report errors directly into the output (+data)
pub fn read_technique(ctx: &IOContext) -> Result<Vec<ActionResult>> {
let sources = Arena::new();
let technique = Technique::from(&technique_to_ir(ctx, &sources)?).to_json()?;

// TODO Option -> output -> Some else None
// TODO deal with errors (implement)
Ok(vec![ActionResult::new(Format::JSON, Some(technique), None)])
}
22 changes: 22 additions & 0 deletions rudder-lang/tests/techniques/6.1.rc5/technique.rl.cf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# generated by rudderc
# @name normal
# @version unknown

bundle agent normal_technique(parameter_wdd, paramtest) {

vars:
"resources_dir" string => "${this.promise_dirname}/resources";
"args" slist => {"parameter_wdd", "paramtest"};
"report_param" string => join("_", args);
"full_class_prefix" string => canonify("normal_technique_${report_param}");
"class_prefix" string => string_head("${full_class_prefix}", "1000");

methods:
# Condition once:
#
# condition("mycond").once() as condition_once_mycond
#
"${report_data.directive_id}_0" usebundle => _method_reporting_context("Condition once", "mycond");
"${report_data.directive_id}_0" usebundle => condition_once("mycond");

}
38 changes: 38 additions & 0 deletions rudder-lang/tests/techniques/6.1.rc5/technique.rl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"type": "ncf_technique",
"version": 2,
"data": {
"bundle_name": "normal",
"description": "ewf",
"name": "normal",
"version": 0,
"parameter": [
{
"id": "c6e6cc3a-9ce8-4889-bccc-6bfc1b091d0d",
"name": "parameter wdd",
"description": ""
},
{
"id": "d74a03dd-5b0b-4b06-8dcf-b4e0cb387c60",
"name": "paramtest",
"description": ""
}
],
"category": "",
"method_calls": [
{
"parameters": [
{
"name": "condition_prefix",
"value": "mycond",
"$errors": []
}
],
"class_context": "any",
"method_name": "condition_once",
"component": "Condition once"
}
],
"resources": []
}
}

0 comments on commit 0d23aba

Please sign in to comment.