Skip to content

Commit

Permalink
Merge pull request #80 from WeareJH/output-fixes
Browse files Browse the repository at this point in the history
feat: `--clean` flag
  • Loading branch information
shakyShane authored Jan 7, 2020
2 parents dd49a82 + 6c73363 commit 10442ec
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 29 deletions.
1 change: 1 addition & 0 deletions pre-push.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
set -eo pipefail
cargo fix --allow-dirty --allow-staged && cargo fmt
cargo check
cargo test
cargo clippy -- -D warnings
Expand Down
1 change: 1 addition & 0 deletions wf2_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub mod php;
pub mod recipes;
pub mod scripts;
pub mod task;
pub mod tasks;
pub mod util;
pub mod vars;
pub mod zip_utils;
Expand Down
6 changes: 1 addition & 5 deletions wf2_core/src/recipes/m2/subcommands/m2_playground_help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@ pub fn help(pg: &M2Playground) -> String {
format!(
r#"Next steps:
Stop existing docker containers:
docker stop $(docker ps -qa) && docker rm $(docker ps -qa)
Now start wf2 in the new directory:
cd {}
wf2 up
wf2 up --clean
{}"#,
pg.dir.file_name().unwrap().to_string_lossy(),
Expand Down
23 changes: 16 additions & 7 deletions wf2_core/src/recipes/m2/subcommands/up.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::recipes::m2::subcommands::m2_playground_help;
use crate::recipes::m2::subcommands::up_help::up_help;
use crate::recipes::m2::M2Recipe;
use crate::recipes::Recipe;
use crate::tasks::docker_clean::docker_clean;
use crate::{context::Context, task::Task};
use ansi_term::Colour::Green;
use clap::{App, ArgMatches};
Expand All @@ -22,6 +23,7 @@ impl M2Up {
#[derive(StructOpt)]
struct Opts {
attached: bool,
clean: bool,
}

impl<'a, 'b> CliCommand<'a, 'b> for M2Up {
Expand All @@ -30,19 +32,20 @@ impl<'a, 'b> CliCommand<'a, 'b> for M2Up {
}
fn exec(&self, matches: Option<&ArgMatches>, ctx: &Context) -> Option<Vec<Task>> {
let opts: Opts = matches.map(Opts::from_clap).expect("guarded by Clap");
Some(up(&ctx, opts.attached).unwrap_or_else(Task::task_err_vec))
Some(up(&ctx, opts.clean, opts.attached).unwrap_or_else(Task::task_err_vec))
}
fn subcommands(&self, _ctx: &Context) -> Vec<App<'a, 'b>> {
vec![App::new(M2Up::NAME)
.about(M2Up::ABOUT)
.arg_from_usage("-a --attached 'Run in attached mode (streaming logs)'")]
.arg_from_usage("-a --attached 'Run in attached mode (streaming logs)'")
.arg_from_usage("-c --clean 'stop & remove other containers before starting new ones'")]
}
}

///
/// Bring the project up using given templates
///
pub fn up(ctx: &Context, attached: bool) -> Result<Vec<Task>, failure::Error> {
pub fn up(ctx: &Context, clean: bool, attached: bool) -> Result<Vec<Task>, failure::Error> {
//
// Display which config file (if any) is being used.
//
Expand Down Expand Up @@ -76,6 +79,11 @@ pub fn up(ctx: &Context, attached: bool) -> Result<Vec<Task>, failure::Error> {
//
let dc_tasks = M2Recipe::dc_tasks(&ctx)?;

//
// Stop & remove docker containers before starting new ones
//
let clean_task = if clean { docker_clean() } else { vec![] };

//
// The final DC task, either in detached mode (default)
// or 'attached' if '-a' given.
Expand All @@ -92,11 +100,11 @@ pub fn up(ctx: &Context, attached: bool) -> Result<Vec<Task>, failure::Error> {
let up_help_task = if !attached {
if let Some(origin) = ctx.origin.as_ref() {
match origin.as_str() {
"m2-playground" => Task::notify_info(m2_playground_help::up_help()),
_ => Task::notify_info(up_help(&ctx)),
"m2-playground" => Task::notify(m2_playground_help::up_help()),
_ => Task::notify(up_help(&ctx)),
}
} else {
Task::notify_info(up_help(&ctx))
Task::notify(up_help(&ctx))
}
} else {
// if we're attached to the output stream, we cannot show any terminal output
Expand All @@ -109,6 +117,7 @@ pub fn up(ctx: &Context, attached: bool) -> Result<Vec<Task>, failure::Error> {
.chain(notify.into_iter())
.chain(missing_env.into_iter())
.chain(templates.into_iter())
.chain(clean_task.into_iter())
.chain(vec![up].into_iter())
.chain(vec![up_help_task].into_iter())
.collect())
Expand All @@ -125,7 +134,7 @@ mod tests {
cwd: PathBuf::from("/users/shane"),
..Context::default()
};
let output = up(&ctx, false).expect("test");
let output = up(&ctx, false, false).expect("test");
let file_ops = Task::file_op_paths(output);
assert_eq!(
vec![
Expand Down
1 change: 1 addition & 0 deletions wf2_core/src/recipes/m2/subcommands/up_help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub fn up_help(ctx: &Context) -> String {
};

let items = vec![
String::from(""),
site(&ctx),
extra_services(),
hosts(),
Expand Down
8 changes: 2 additions & 6 deletions wf2_core/src/recipes/wp/subcommands/wp_playground_help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@ pub fn help(wp: &WpPlayground) -> String {
format!(
r#"Next steps:
Stop existing docker containers:
docker stop $(docker ps -qa) && docker rm $(docker ps -qa)
Now start wf2 in the new directory:
cd {}
wf2 up
wf2 up --clean
Then, once it's up an running, in a new tab, run the following:
Then run the following:
wf2 composer install
Expand Down
44 changes: 33 additions & 11 deletions wf2_core/src/recipes/wp/subcommands/wp_up.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use crate::context::Context;
use crate::recipes::wp::services::WpVolumeMounts;
use crate::recipes::wp::WpRecipe;
use crate::task::Task;
use crate::tasks::docker_clean::docker_clean;
use clap::{App, ArgMatches};
use structopt::StructOpt;

pub struct WpUp;

Expand All @@ -13,22 +15,32 @@ impl WpUp {
const ABOUT: &'static str = "[wp] Bring up WP containers";
}

#[derive(StructOpt)]
struct Opts {
attached: bool,
clean: bool,
}

impl<'a, 'b> CliCommand<'a, 'b> for WpUp {
fn name(&self) -> String {
String::from(WpUp::NAME)
}
fn exec(&self, _matches: Option<&ArgMatches>, ctx: &Context) -> Option<Vec<Task>> {
Some(up(&ctx, false))
fn exec(&self, matches: Option<&ArgMatches>, ctx: &Context) -> Option<Vec<Task>> {
let opts: Opts = matches.map(Opts::from_clap).expect("guarded by Clap");
Some(up(&ctx, opts.clean, opts.attached))
}
fn subcommands(&self, _ctx: &Context) -> Vec<App<'a, 'b>> {
vec![App::new(WpUp::NAME).about(WpUp::ABOUT)]
vec![App::new(WpUp::NAME)
.about(WpUp::ABOUT)
.arg_from_usage("-a --attached 'Run in attached mode (streaming logs)'")
.arg_from_usage("-c --clean 'stop & remove other containers before starting new ones'")]
}
}

fn up(ctx: &Context, detached: bool) -> Vec<Task> {
fn up(ctx: &Context, clean: bool, attached: bool) -> Vec<Task> {
WpRecipe::dc_tasks(&ctx)
.map(|dc_tasks| {
vec![
let base_tasks = vec![
Task::file_write(
ctx.file_path(WpVolumeMounts::NGINX_CONF),
"Writes the nginx conf file",
Expand All @@ -39,12 +51,22 @@ fn up(ctx: &Context, detached: bool) -> Vec<Task> {
"Writes the nginx conf file",
include_bytes!("../templates/host.conf").to_vec(),
),
if detached {
dc_tasks.cmd_task(vec!["up -d".to_string()])
} else {
dc_tasks.cmd_task(vec!["up".to_string()])
},
]
];

let clean = if clean { docker_clean() } else { vec![] };

let up_task = if attached {
dc_tasks.cmd_task(vec!["up".to_string()])
} else {
dc_tasks.cmd_task(vec!["up -d".to_string()])
};

vec![]
.into_iter()
.chain(base_tasks.into_iter())
.chain(clean.into_iter())
.chain(vec![up_task].into_iter())
.collect()
})
.unwrap_or_else(Task::task_err_vec)
}
8 changes: 8 additions & 0 deletions wf2_core/src/tasks/docker_clean.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use crate::task::Task;

pub fn docker_clean() -> Vec<Task> {
vec![
Task::simple_command("docker stop $(docker ps -aq)"),
Task::simple_command("docker rm $(docker ps -aq)"),
]
}
1 change: 1 addition & 0 deletions wf2_core/src/tasks/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod docker_clean;

0 comments on commit 10442ec

Please sign in to comment.