Skip to content

Commit

Permalink
Pass command args owned to allow for direct use
Browse files Browse the repository at this point in the history
  • Loading branch information
bruceg committed Jan 5, 2023
1 parent 5ee9df7 commit 8e6cde0
Show file tree
Hide file tree
Showing 18 changed files with 38 additions and 37 deletions.
2 changes: 1 addition & 1 deletion vdev/src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct Cli {
}

impl Cli {
pub fn exec(&self) -> Result<()> {
pub fn exec(self) -> Result<()> {
let mut command = Command::with_path("cargo");
command.args(["build", "--no-default-features"]);

Expand Down
2 changes: 1 addition & 1 deletion vdev/src/commands/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct Cli {
}

impl Cli {
pub fn exec(&self) -> Result<()> {
pub fn exec(self) -> Result<()> {
let mut cmd = RootCli::command();
let bin_name = cmd.get_name().to_string();
generate(self.shell, &mut cmd, bin_name, &mut io::stdout());
Expand Down
2 changes: 1 addition & 1 deletion vdev/src/commands/config/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::config;
pub struct Cli {}

impl Cli {
pub fn exec(&self) -> Result<()> {
pub fn exec(self) -> Result<()> {
display!("{}", config::path()?.display());

Ok(())
Expand Down
4 changes: 2 additions & 2 deletions vdev/src/commands/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ enum Commands {
}

impl Cli {
pub fn exec(&self) -> Result<()> {
match &self.command {
pub fn exec(self) -> Result<()> {
match self.command {
Commands::Find(cli) => cli.exec(),
Commands::Set(cli) => cli.exec(),
}
Expand Down
4 changes: 2 additions & 2 deletions vdev/src/commands/config/set/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ enum Commands {
}

impl Cli {
pub fn exec(&self) -> Result<()> {
match &self.command {
pub fn exec(self) -> Result<()> {
match self.command {
Commands::Repo(cli) => cli.exec(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion vdev/src/commands/config/set/org.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct Cli {
}

impl Cli {
pub fn exec(&self) -> Result<()> {
pub fn exec(self) -> Result<()> {
let mut config = app::config().clone();
config.org = self.name.to_string();
config::save(config)?;
Expand Down
2 changes: 1 addition & 1 deletion vdev/src/commands/config/set/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct Cli {
}

impl Cli {
pub fn exec(&self) -> Result<()> {
pub fn exec(self) -> Result<()> {
let path = platform::canonicalize_path(&self.path);

let mut config = app::config().clone();
Expand Down
2 changes: 1 addition & 1 deletion vdev/src/commands/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct Cli {
}

impl Cli {
pub fn exec(&self) -> Result<()> {
pub fn exec(self) -> Result<()> {
let mut command = Command::with_path(&self.args[0]);
if self.args.len() > 1 {
command.args(&self.args[1..]);
Expand Down
4 changes: 2 additions & 2 deletions vdev/src/commands/integration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ enum Commands {
}

impl Cli {
pub fn exec(&self) -> Result<()> {
match &self.command {
pub fn exec(self) -> Result<()> {
match self.command {
Commands::Show(cli) => cli.exec(),
Commands::Start(cli) => cli.exec(),
Commands::Stop(cli) => cli.exec(),
Expand Down
2 changes: 1 addition & 1 deletion vdev/src/commands/integration/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct Cli {
}

impl Cli {
pub fn exec(&self) -> Result<()> {
pub fn exec(self) -> Result<()> {
if self.integration.is_none() {
let mut entries = vec![];
let root_dir: PathBuf = [app::path(), "scripts", "integration"].iter().collect();
Expand Down
2 changes: 1 addition & 1 deletion vdev/src/commands/integration/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct Cli {
}

impl Cli {
pub fn exec(&self) -> Result<()> {
pub fn exec(self) -> Result<()> {
let test_dir = IntegrationTestConfig::locate_source(app::path(), &self.integration)?;

let envs_dir = state::envs_dir(&platform::data_dir(), &self.integration);
Expand Down
2 changes: 1 addition & 1 deletion vdev/src/commands/integration/stop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct Cli {
}

impl Cli {
pub fn exec(&self) -> Result<()> {
pub fn exec(self) -> Result<()> {
let test_dir = IntegrationTestConfig::locate_source(app::path(), &self.integration)?;
let envs_dir = state::envs_dir(&platform::data_dir(), &self.integration);
let config = IntegrationTestConfig::from_source(&test_dir)?;
Expand Down
31 changes: 16 additions & 15 deletions vdev/src/commands/integration/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,23 @@ pub struct Cli {
}

impl Cli {
pub fn exec(&self) -> Result<()> {
pub fn exec(self) -> Result<()> {
let test_dir = IntegrationTestConfig::locate_source(app::path(), &self.integration)?;
let toolchain_config = RustToolchainConfig::parse(app::path())?;
let runner =
IntegrationTestRunner::new(self.integration.to_string(), toolchain_config.channel);
let envs_dir = state::envs_dir(&platform::data_dir(), &self.integration);
let config = IntegrationTestConfig::from_source(&test_dir)?;
let envs = config.environments();

let mut env_vars = BTreeMap::new();
if let Some(configured_env_vars) = &config.env {
env_vars.extend(configured_env_vars.clone());
}
let env_vars: BTreeMap<_, _> = config
.env
.clone()
.map_or(BTreeMap::default(), |map| map.into_iter().collect());

let mut args = Vec::from_iter(config.args.clone());
if let Some(configured_args) = &self.args {
args.extend(configured_args.clone());
let mut args: Vec<_> = config.args.into_iter().collect();
if let Some(configured_args) = self.args {
args.extend(configured_args);
}

if let Some(environment) = &self.environment {
Expand All @@ -55,19 +56,19 @@ impl Cli {
runner.ensure_network()?;

let active_envs = state::active_envs(&envs_dir)?;
for (env_name, env_config) in config.environments().iter() {
if !(active_envs.is_empty() || active_envs.contains(env_name)) {
for (env_name, env_config) in envs {
if !(active_envs.is_empty() || active_envs.contains(&env_name)) {
continue;
}

let env_active = state::env_exists(&envs_dir, env_name);
let env_active = state::env_exists(&envs_dir, &env_name);
if !env_active {
let mut command = Command::new("cargo");
command.current_dir(&test_dir);
command.env(NETWORK_ENV_VAR, runner.network_name());
command.args(["run", "--quiet", "--", "start"]);

let json = serde_json::to_string(env_config)?;
let json = serde_json::to_string(&env_config)?;
command.arg(&json);

if let Some(env_vars) = &config.env {
Expand All @@ -77,7 +78,7 @@ impl Cli {
waiting!("Starting environment {}", env_name);
command.run()?;

state::save_env(&envs_dir, env_name, &json)?;
state::save_env(&envs_dir, &env_name, &json)?;
}

runner.test(&env_vars, &args)?;
Expand All @@ -91,7 +92,7 @@ impl Cli {
"--quiet",
"--",
"stop",
&state::read_env_config(&envs_dir, env_name)?,
&state::read_env_config(&envs_dir, &env_name)?,
]);

if let Some(env_vars) = &config.env {
Expand All @@ -101,7 +102,7 @@ impl Cli {
waiting!("Stopping environment {}", env_name);
command.run()?;

state::remove_env(&envs_dir, env_name)?;
state::remove_env(&envs_dir, &env_name)?;
}
}

Expand Down
4 changes: 2 additions & 2 deletions vdev/src/commands/meta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ enum Commands {
}

impl Cli {
pub fn exec(&self) -> Result<()> {
match &self.command {
pub fn exec(self) -> Result<()> {
match self.command {
Commands::Starship(cli) => cli.exec(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion vdev/src/commands/meta/starship.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const VERSION_START: &str = "version = ";
pub struct Cli {}

impl Cli {
pub fn exec(&self) -> Result<()> {
pub fn exec(self) -> Result<()> {
let mut contexts = vec![];

let path: PathBuf = [app::path(), "Cargo.toml"].iter().collect();
Expand Down
4 changes: 2 additions & 2 deletions vdev/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ enum Commands {
}

impl Cli {
pub fn exec(&self) -> Result<()> {
match &self.command {
pub fn exec(self) -> Result<()> {
match self.command {
Commands::Build(cli) => cli.exec(),
Commands::Complete(cli) => cli.exec(),
Commands::Config(cli) => cli.exec(),
Expand Down
2 changes: 1 addition & 1 deletion vdev/src/commands/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::git;
pub struct Cli {}

impl Cli {
pub fn exec(&self) -> Result<()> {
pub fn exec(self) -> Result<()> {
display!("Branch: {}", git::current_branch()?);
display!("Changed files: {}", git::changed_files()?.len());

Expand Down
2 changes: 1 addition & 1 deletion vdev/src/commands/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct Cli {
}

impl Cli {
pub fn exec(&self) -> Result<()> {
pub fn exec(self) -> Result<()> {
let toolchain_config = RustToolchainConfig::parse(app::path())?;
let runner = get_agent_test_runner(self.container, toolchain_config.channel);

Expand Down

0 comments on commit 8e6cde0

Please sign in to comment.