From f817c10bdf072ddfb3e62648800c2875891430e3 Mon Sep 17 00:00:00 2001 From: Barak Nehmad Date: Sun, 17 May 2020 17:24:04 +0300 Subject: [PATCH 1/9] Added complete generation script, modified game-config.toml, and added example template. --- levels/checkers/start-here.sh | 22 ++++----- levels/game-config.toml | 13 +++--- scripts/generate-pre-receive-hook/Cargo.toml | 2 + scripts/generate-pre-receive-hook/src/main.rs | 45 +++++++++++++++++-- scripts/generate-pre-receive-hook/template.sh | 9 ++++ 5 files changed, 69 insertions(+), 22 deletions(-) create mode 100644 scripts/generate-pre-receive-hook/template.sh diff --git a/levels/checkers/start-here.sh b/levels/checkers/start-here.sh index 8ee00ae..0383595 100644 --- a/levels/checkers/start-here.sh +++ b/levels/checkers/start-here.sh @@ -7,26 +7,26 @@ read old new ref < /dev/stdin dump_dir=$(dump-commit-to-directory $new) pushd dump_dir - # Check file existence. - if [ ! -f alice.txt ]; - then reject-solution "Alice is missing! Try again."; - fi - - if [ ! -f bob.txt ]; - then reject-solution "Bob is missing! Try again."; - fi + # Check file existence. + if [ ! -f alice.txt ]; + then reject-solution "Alice is missing! Try again."; + fi + + if [ ! -f bob.txt ]; + then reject-solution "Bob is missing! Try again."; + fi popd git fetch --tags --quiet # get all the tags but don't show them to the user # Check how many commits the user needed - should be two (the user commit + merge commit)! commit_amount=$( git log start-here-tag..$new --oneline | wc -l ) if [ $commit_amount -ne 1 ]; - then reject-solution "The files should have been added in a single commit, but I've found ${commit_amount} commits in the log. To reset and try again, delete your local start-here branch, checkout the original start-here branch again and try again."; + then reject-solution "The files should have been added in a single commit, but I've found ${commit_amount} commits in the log. To reset and try again, delete your local start-here branch, checkout the original start-here branch again and try again."; fi # We know that there's only one commit in the changes - otherwise it would have failed before. -number_of_files_changed=$( git diff --stat $old $new | grep "files changed" | awk ' {print $1} ' ) +number_of_files_changed=$( git diff --stat $old $new | grep "files changed" | awk ' {print $1} ' ) if [[ $number_of_files_changed -ne 2 ]] -then bad "More than 2 files were changed! Only add alice.txt and bob.txt. Check out the original branch and try again."; + then reject-solution "More than 2 files were changed! Only add alice.txt and bob.txt. Check out the original branch and try again."; fi diff --git a/levels/game-config.toml b/levels/game-config.toml index 6ade461..dd3828c 100644 --- a/levels/game-config.toml +++ b/levels/game-config.toml @@ -1,18 +1,17 @@ -[levels] -[levels.clone] +[[levels]] title = "clone" branch = "master" - solutionCheckers = [] + solution_checker = "exit 0" flags = [] # ["start-here"], but it's implicit in the readme -[levels.start-here] +[[levels]] title = "start-here" branch = "start-here" - solutionChecker = "checkers/start-here.sh" + solution_checker = "checkers/start-here.sh" flags = ["merge-1"] -[levels.merge-1] +[[levels]] title = "merge-1" branch = "fizzling-vulture-pedial" - solutionChecker = "checkers/merge-1.sh" + solution_checker = "checkers/merge-1.sh" flags = ["merge-2", "log-1"] diff --git a/scripts/generate-pre-receive-hook/Cargo.toml b/scripts/generate-pre-receive-hook/Cargo.toml index 7619790..aa197c7 100644 --- a/scripts/generate-pre-receive-hook/Cargo.toml +++ b/scripts/generate-pre-receive-hook/Cargo.toml @@ -8,4 +8,6 @@ edition = "2018" [dependencies] structopt = "0.3.13" +serde = { version = "1.0", features = ["derive"] } toml = "0.5" +tinytemplate = "1.0.4" \ No newline at end of file diff --git a/scripts/generate-pre-receive-hook/src/main.rs b/scripts/generate-pre-receive-hook/src/main.rs index 4c87075..a600645 100644 --- a/scripts/generate-pre-receive-hook/src/main.rs +++ b/scripts/generate-pre-receive-hook/src/main.rs @@ -1,31 +1,68 @@ +use serde::{Deserialize, Serialize}; +use std::io::Write; use std::fs; use structopt::StructOpt; +use tinytemplate::TinyTemplate; +use toml; // Search for a pattern in a file and display the lines that contain it. #[derive(Debug, StructOpt)] +#[structopt(about = "A script to generate the master pre-receive hook file.")] struct Cli { // The path to the file to read #[structopt(parse(from_os_str))] game_config_path: std::path::PathBuf, + template_path: std::path::PathBuf, + output_path: std::path::PathBuf, + + #[structopt(short = "v", long = "verbose")] + verbose: bool, } +#[derive(Debug, Deserialize, Serialize)] struct Level { title: String, branch: String, - solution_checke: String, + solution_checker: String, flags: Vec, } +#[derive(Debug, Deserialize, Serialize)] struct GameConfig { levels: Vec, } fn main() { let args = Cli::from_args(); - println!("Loading script from {:?}", args); + println!("Reading script from {:?}", args.game_config_path); let game_config_file_contents = - fs::read_to_string(args.game_config_path).expect("Couldn't read the config file!"); + fs::read_to_string(args.game_config_path).unwrap(); + + let game_config: GameConfig = + toml::from_str(&game_config_file_contents).unwrap(); + + if args.verbose { + println!("########## GAME CONFIG STRUCT ##########"); + println!("{:?}\n", game_config); + } + + println!("Reading template from {:?}", args.template_path); + let template_file_contents = + fs::read_to_string(args.template_path).unwrap(); + + let mut tt = TinyTemplate::new(); + let template_name = "switch_case"; + tt.add_template(template_name, &template_file_contents).unwrap(); + let rendered = tt.render(template_name, &game_config).unwrap(); + + if args.verbose { + println!("########## RENDERED TEMPLATE ##########"); + println!("{}\n", rendered); + } + + let mut output_file = fs::File::create(&args.output_path).unwrap(); + output_file.write_all(&rendered.as_bytes()).unwrap(); - println!("{}", game_config_file_contents); + println!("Wrote rendered file to {:?}", args.output_path); } diff --git a/scripts/generate-pre-receive-hook/template.sh b/scripts/generate-pre-receive-hook/template.sh new file mode 100644 index 0000000..494c2bd --- /dev/null +++ b/scripts/generate-pre-receive-hook/template.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +current_branch = `git rev-parse --abbrev-ref HEAD` + +case $current_branch in +{{ for level in levels }}{level.branch}) + {level.solution_checker} + ;; +{{ endfor }}esac \ No newline at end of file From fff15bd25519d162ca6ce59e5f9e97445bfc8cfd Mon Sep 17 00:00:00 2001 From: Barak Nehmad Date: Sun, 17 May 2020 17:35:00 +0300 Subject: [PATCH 2/9] Added usage information. --- scripts/generate-pre-receive-hook/src/main.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/scripts/generate-pre-receive-hook/src/main.rs b/scripts/generate-pre-receive-hook/src/main.rs index a600645..714b8b4 100644 --- a/scripts/generate-pre-receive-hook/src/main.rs +++ b/scripts/generate-pre-receive-hook/src/main.rs @@ -9,13 +9,16 @@ use toml; #[derive(Debug, StructOpt)] #[structopt(about = "A script to generate the master pre-receive hook file.")] struct Cli { - // The path to the file to read - #[structopt(parse(from_os_str))] + #[structopt(parse(from_os_str), help = "Path to game config file to read")] game_config_path: std::path::PathBuf, + + #[structopt(parse(from_os_str), help = "Path to template file to read")] template_path: std::path::PathBuf, + + #[structopt(parse(from_os_str), help = "Path to output file (creates if doesn't exist)")] output_path: std::path::PathBuf, - #[structopt(short = "v", long = "verbose")] + #[structopt(short = "v", long = "verbose", help = "Show more information about the actions taken")] verbose: bool, } From 46053a19b4e5b42e0dbe82be2e22790867d7c599 Mon Sep 17 00:00:00 2001 From: Barak Nehmad Date: Sun, 17 May 2020 18:33:42 +0300 Subject: [PATCH 3/9] Fixed template file. --- levels/game-config.toml | 2 +- scripts/generate-pre-receive-hook/template.sh | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/levels/game-config.toml b/levels/game-config.toml index dd3828c..2d9285c 100644 --- a/levels/game-config.toml +++ b/levels/game-config.toml @@ -1,7 +1,7 @@ [[levels]] title = "clone" branch = "master" - solution_checker = "exit 0" + solution_checker = "echo I am on the master branch" flags = [] # ["start-here"], but it's implicit in the readme [[levels]] diff --git a/scripts/generate-pre-receive-hook/template.sh b/scripts/generate-pre-receive-hook/template.sh index 494c2bd..e097070 100644 --- a/scripts/generate-pre-receive-hook/template.sh +++ b/scripts/generate-pre-receive-hook/template.sh @@ -1,9 +1,9 @@ #!/bin/bash -current_branch = `git rev-parse --abbrev-ref HEAD` +current_branch=`git rev-parse --abbrev-ref HEAD` case $current_branch in {{ for level in levels }}{level.branch}) {level.solution_checker} ;; -{{ endfor }}esac \ No newline at end of file +{{ endfor }}esac From 5848fc05b16e0a340ae35eaebbd15f0118baad55 Mon Sep 17 00:00:00 2001 From: Shay Nehmad Date: Sun, 17 May 2020 20:13:34 +0300 Subject: [PATCH 4/9] Added logging, default output argument, and minor fixes --- scripts/generate-pre-receive-hook/.gitignore | 3 + scripts/generate-pre-receive-hook/Cargo.toml | 5 +- scripts/generate-pre-receive-hook/src/main.rs | 59 +++++++++++-------- 3 files changed, 43 insertions(+), 24 deletions(-) create mode 100644 scripts/generate-pre-receive-hook/.gitignore diff --git a/scripts/generate-pre-receive-hook/.gitignore b/scripts/generate-pre-receive-hook/.gitignore new file mode 100644 index 0000000..1426e40 --- /dev/null +++ b/scripts/generate-pre-receive-hook/.gitignore @@ -0,0 +1,3 @@ +# Script puts files here by default. Let's avoid commiting them if possible +output/ + diff --git a/scripts/generate-pre-receive-hook/Cargo.toml b/scripts/generate-pre-receive-hook/Cargo.toml index aa197c7..7e1c9db 100644 --- a/scripts/generate-pre-receive-hook/Cargo.toml +++ b/scripts/generate-pre-receive-hook/Cargo.toml @@ -10,4 +10,7 @@ edition = "2018" structopt = "0.3.13" serde = { version = "1.0", features = ["derive"] } toml = "0.5" -tinytemplate = "1.0.4" \ No newline at end of file +tinytemplate = "1.0.4" +simple_logger = "1.6.0" +log = "0.4" + diff --git a/scripts/generate-pre-receive-hook/src/main.rs b/scripts/generate-pre-receive-hook/src/main.rs index 714b8b4..97cd88e 100644 --- a/scripts/generate-pre-receive-hook/src/main.rs +++ b/scripts/generate-pre-receive-hook/src/main.rs @@ -1,11 +1,13 @@ +use log; +use log::{debug, info}; use serde::{Deserialize, Serialize}; -use std::io::Write; +use simple_logger; use std::fs; +use std::io::Write; use structopt::StructOpt; use tinytemplate::TinyTemplate; use toml; -// Search for a pattern in a file and display the lines that contain it. #[derive(Debug, StructOpt)] #[structopt(about = "A script to generate the master pre-receive hook file.")] struct Cli { @@ -15,10 +17,18 @@ struct Cli { #[structopt(parse(from_os_str), help = "Path to template file to read")] template_path: std::path::PathBuf, - #[structopt(parse(from_os_str), help = "Path to output file (creates if doesn't exist)")] + #[structopt( + parse(from_os_str), + default_value = "output/pre-receive", + help = "Path to output file (creates if doesn't exist)" + )] output_path: std::path::PathBuf, - #[structopt(short = "v", long = "verbose", help = "Show more information about the actions taken")] + #[structopt( + short = "v", + long = "verbose", + help = "Show more information about the actions taken" + )] verbose: bool, } @@ -38,34 +48,37 @@ struct GameConfig { fn main() { let args = Cli::from_args(); - println!("Reading script from {:?}", args.game_config_path); - let game_config_file_contents = - fs::read_to_string(args.game_config_path).unwrap(); + if args.verbose { + simple_logger::init_with_level(log::Level::Debug).unwrap(); + } else { + simple_logger::init_with_level(log::Level::Info).unwrap(); + }; + + info!("Reading script from {:?}", args.game_config_path); + let game_config_file_contents = fs::read_to_string(args.game_config_path).unwrap(); - let game_config: GameConfig = - toml::from_str(&game_config_file_contents).unwrap(); + let game_config: GameConfig = toml::from_str(&game_config_file_contents).unwrap(); - if args.verbose { - println!("########## GAME CONFIG STRUCT ##########"); - println!("{:?}\n", game_config); - } + debug!("########## GAME CONFIG STRUCT ##########"); + debug!("{:?}\n", game_config); - println!("Reading template from {:?}", args.template_path); - let template_file_contents = - fs::read_to_string(args.template_path).unwrap(); + info!("Reading template from {:?}", args.template_path); + let template_file_contents = fs::read_to_string(args.template_path).unwrap(); let mut tt = TinyTemplate::new(); let template_name = "switch_case"; - tt.add_template(template_name, &template_file_contents).unwrap(); + tt.add_template(template_name, &template_file_contents) + .unwrap(); let rendered = tt.render(template_name, &game_config).unwrap(); - if args.verbose { - println!("########## RENDERED TEMPLATE ##########"); - println!("{}\n", rendered); - } + debug!("########## RENDERED TEMPLATE ##########"); + debug!("{}\n", rendered); - let mut output_file = fs::File::create(&args.output_path).unwrap(); + let mut output_dir = args.output_path.clone(); + output_dir.pop(); + fs::create_dir_all(&output_dir).expect("Failed to create parent dir"); + let mut output_file = fs::File::create(&args.output_path).expect("Couldn't create file!"); output_file.write_all(&rendered.as_bytes()).unwrap(); - println!("Wrote rendered file to {:?}", args.output_path); + info!("Wrote rendered file to {:?}", args.output_path); } From c83055076a03895104113f4276f5d2bff3cdd457 Mon Sep 17 00:00:00 2001 From: Shay Nehmad Date: Sun, 17 May 2020 20:38:24 +0300 Subject: [PATCH 5/9] debugging the scripts and added the flags --- levels/game-config.toml | 6 +++--- scripts/generate-pre-receive-hook/template.sh | 12 +++++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/levels/game-config.toml b/levels/game-config.toml index 2d9285c..22f37ab 100644 --- a/levels/game-config.toml +++ b/levels/game-config.toml @@ -1,17 +1,17 @@ [[levels]] title = "clone" branch = "master" - solution_checker = "echo I am on the master branch" + solution_checker = "echo \"No pushing to master Read the README file\"; exit 1" flags = [] # ["start-here"], but it's implicit in the readme [[levels]] title = "start-here" branch = "start-here" - solution_checker = "checkers/start-here.sh" + solution_checker = "hooks/checkers/start-here.sh" flags = ["merge-1"] [[levels]] title = "merge-1" branch = "fizzling-vulture-pedial" - solution_checker = "checkers/merge-1.sh" + solution_checker = "hooks/checkers/merge-1.sh" flags = ["merge-2", "log-1"] diff --git a/scripts/generate-pre-receive-hook/template.sh b/scripts/generate-pre-receive-hook/template.sh index e097070..9a598c9 100644 --- a/scripts/generate-pre-receive-hook/template.sh +++ b/scripts/generate-pre-receive-hook/template.sh @@ -1,9 +1,15 @@ #!/bin/bash -current_branch=`git rev-parse --abbrev-ref HEAD` +read old new ref < /dev/stdin -case $current_branch in +branch_name=$(echo $ref | awk 'BEGIN \{ FS = "/" } ; \{ print $NF }') + +case $branch_name in {{ for level in levels }}{level.branch}) - {level.solution_checker} + echo $old $new $ref | {level.solution_checker} + echo "solved! the flags are..." + {{ for levelflag in level.flags }} + echo {levelflag} + {{ endfor }} ;; {{ endfor }}esac From 394edd2342162f62df8c1b8dd6c605427415c197 Mon Sep 17 00:00:00 2001 From: Barak Nehmad Date: Sun, 17 May 2020 21:58:28 +0300 Subject: [PATCH 6/9] Fixed template quoting issue and renamed template. --- levels/game-config.toml | 2 +- .../generate-pre-receive-hook/{template.sh => template.tmpl} | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) rename scripts/generate-pre-receive-hook/{template.sh => template.tmpl} (83%) diff --git a/levels/game-config.toml b/levels/game-config.toml index 22f37ab..14cc758 100644 --- a/levels/game-config.toml +++ b/levels/game-config.toml @@ -1,7 +1,7 @@ [[levels]] title = "clone" branch = "master" - solution_checker = "echo \"No pushing to master Read the README file\"; exit 1" + solution_checker = "echo No pushing to master. Read the README file; exit 1" flags = [] # ["start-here"], but it's implicit in the readme [[levels]] diff --git a/scripts/generate-pre-receive-hook/template.sh b/scripts/generate-pre-receive-hook/template.tmpl similarity index 83% rename from scripts/generate-pre-receive-hook/template.sh rename to scripts/generate-pre-receive-hook/template.tmpl index 9a598c9..04861b3 100644 --- a/scripts/generate-pre-receive-hook/template.sh +++ b/scripts/generate-pre-receive-hook/template.tmpl @@ -8,8 +8,7 @@ case $branch_name in {{ for level in levels }}{level.branch}) echo $old $new $ref | {level.solution_checker} echo "solved! the flags are..." - {{ for levelflag in level.flags }} - echo {levelflag} + {{ for levelflag in level.flags }}echo {levelflag} {{ endfor }} ;; {{ endfor }}esac From 00cccdd863198a9fc4363376cb8420a11ccb1893 Mon Sep 17 00:00:00 2001 From: Shay Nehmad Date: Sun, 17 May 2020 22:28:32 +0300 Subject: [PATCH 7/9] fixed start-here import path and template error code checking --- levels/checkers/start-here.sh | 4 ++-- scripts/generate-pre-receive-hook/template.tmpl | 13 +++++++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/levels/checkers/start-here.sh b/levels/checkers/start-here.sh index 0383595..7b53f17 100644 --- a/levels/checkers/start-here.sh +++ b/levels/checkers/start-here.sh @@ -1,12 +1,12 @@ #!/bin/bash -source ./common.sh +source $(dirname $0)/common.sh read old new ref < /dev/stdin dump_dir=$(dump-commit-to-directory $new) -pushd dump_dir +pushd $dump_dir # Check file existence. if [ ! -f alice.txt ]; then reject-solution "Alice is missing! Try again."; diff --git a/scripts/generate-pre-receive-hook/template.tmpl b/scripts/generate-pre-receive-hook/template.tmpl index 04861b3..64396f3 100644 --- a/scripts/generate-pre-receive-hook/template.tmpl +++ b/scripts/generate-pre-receive-hook/template.tmpl @@ -4,11 +4,16 @@ read old new ref < /dev/stdin branch_name=$(echo $ref | awk 'BEGIN \{ FS = "/" } ; \{ print $NF }') +print_flags () \{ + if [[ $# -eq 1 ]] + then echo "You won! The flag is "$1 + else + echo "You won! The flags are "$@ + fi +} + case $branch_name in {{ for level in levels }}{level.branch}) - echo $old $new $ref | {level.solution_checker} - echo "solved! the flags are..." - {{ for levelflag in level.flags }}echo {levelflag} - {{ endfor }} + echo $old $new $ref | {level.solution_checker} && print_flags{{ for levelflag in level.flags }} {levelflag}{{ endfor }} ;; {{ endfor }}esac From eb64b83f99cf9be2c980c96c3ce6c815ce147c97 Mon Sep 17 00:00:00 2001 From: Shay Nehmad Date: Sun, 17 May 2020 22:45:32 +0300 Subject: [PATCH 8/9] wip on branch name replacement --- levels/game-config.toml | 1 + scripts/generate-pre-receive-hook/src/main.rs | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/levels/game-config.toml b/levels/game-config.toml index 14cc758..d8d6fdc 100644 --- a/levels/game-config.toml +++ b/levels/game-config.toml @@ -15,3 +15,4 @@ branch = "fizzling-vulture-pedial" solution_checker = "hooks/checkers/merge-1.sh" flags = ["merge-2", "log-1"] + diff --git a/scripts/generate-pre-receive-hook/src/main.rs b/scripts/generate-pre-receive-hook/src/main.rs index 97cd88e..bfc2e62 100644 --- a/scripts/generate-pre-receive-hook/src/main.rs +++ b/scripts/generate-pre-receive-hook/src/main.rs @@ -57,11 +57,26 @@ fn main() { info!("Reading script from {:?}", args.game_config_path); let game_config_file_contents = fs::read_to_string(args.game_config_path).unwrap(); - let game_config: GameConfig = toml::from_str(&game_config_file_contents).unwrap(); + let mut game_config: GameConfig = toml::from_str(&game_config_file_contents).unwrap(); debug!("########## GAME CONFIG STRUCT ##########"); debug!("{:?}\n", game_config); + for mut level in &game_config.levels { + for mut flag in &level.flags { + println!("in {} flag {}", level.title, flag); + let mut levels_iterator = game_config.levels.iter(); + let found = levels_iterator.find(|&x| &x.title == flag); + match found { + Some(x) => { + debug!("replacing {} with {}", flag, x.branch); + flag = &x.branch; + } + None => debug!("flag {} is final", flag), + } + } + } + info!("Reading template from {:?}", args.template_path); let template_file_contents = fs::read_to_string(args.template_path).unwrap(); From 082450fc2900d7668a8a06e261300d6e69133ce1 Mon Sep 17 00:00:00 2001 From: Shay Nehmad Date: Sun, 17 May 2020 23:42:43 +0300 Subject: [PATCH 9/9] Script works woot --- levels/game-config.toml | 18 ++++++++ scripts/generate-pre-receive-hook/src/main.rs | 45 ++++++++++++------- 2 files changed, 46 insertions(+), 17 deletions(-) diff --git a/levels/game-config.toml b/levels/game-config.toml index d8d6fdc..0e92a65 100644 --- a/levels/game-config.toml +++ b/levels/game-config.toml @@ -16,3 +16,21 @@ solution_checker = "hooks/checkers/merge-1.sh" flags = ["merge-2", "log-1"] +[[levels]] + title = "merge-2" + branch = "ironish-quartzic-brahmas" + solution_checker = "hooks/checkers/merge-2.sh" + flags = ["merge-3"] + +[[levels]] + title = "log-1" + branch = "akmite-radicle-garce" + solution_checker = "echo No pushing in this stage. ; exit 1" + flags = ["log-2"] + +[[levels]] + title = "log-2" + branch = "alibies-listwork-homotaxy" + solution_checker = "hooks/checkers/log-2.sh" + flags = ["log-3"] + diff --git a/scripts/generate-pre-receive-hook/src/main.rs b/scripts/generate-pre-receive-hook/src/main.rs index bfc2e62..f2034e1 100644 --- a/scripts/generate-pre-receive-hook/src/main.rs +++ b/scripts/generate-pre-receive-hook/src/main.rs @@ -32,7 +32,7 @@ struct Cli { verbose: bool, } -#[derive(Debug, Deserialize, Serialize)] +#[derive(Debug, Clone, Deserialize, Serialize)] struct Level { title: String, branch: String, @@ -45,6 +45,30 @@ struct GameConfig { levels: Vec, } +fn replace_flags_with_branch_names(game_config: &mut GameConfig) { + let levels_info = game_config.levels.clone(); + + for mut level in &mut game_config.levels { + let mut new_flags = Vec::new(); + for flag in &level.flags { + debug!("level {} flag {}", level.title, flag); + let mut levels_iterator = levels_info.iter(); + let found = levels_iterator.find(|&x| &x.title == flag); + match found { + Some(x) => { + debug!("replacing {} with {}", flag, x.branch); + new_flags.push(String::from(&x.branch)); + } + None => { + debug!("flag {} is final", flag); + new_flags.push(flag.to_string()); + } + } + } + level.flags = new_flags; + } +} + fn main() { let args = Cli::from_args(); @@ -58,24 +82,11 @@ fn main() { let game_config_file_contents = fs::read_to_string(args.game_config_path).unwrap(); let mut game_config: GameConfig = toml::from_str(&game_config_file_contents).unwrap(); + debug!("Game config before editing: {:?}\n", game_config); - debug!("########## GAME CONFIG STRUCT ##########"); - debug!("{:?}\n", game_config); + replace_flags_with_branch_names(&mut game_config); - for mut level in &game_config.levels { - for mut flag in &level.flags { - println!("in {} flag {}", level.title, flag); - let mut levels_iterator = game_config.levels.iter(); - let found = levels_iterator.find(|&x| &x.title == flag); - match found { - Some(x) => { - debug!("replacing {} with {}", flag, x.branch); - flag = &x.branch; - } - None => debug!("flag {} is final", flag), - } - } - } + debug!("Game config after editing: {:?}\n", game_config); info!("Reading template from {:?}", args.template_path); let template_file_contents = fs::read_to_string(args.template_path).unwrap();