Skip to content

Commit

Permalink
feat(rust): add --config argument to node create command
Browse files Browse the repository at this point in the history
It loads a json file containing a list of commands that will be executed after creating
the node. To generate this json file, the user can use the global `--export`, `--export-as`,
and `--depends-on` arguments on any command, that will copy the command input to the
desired file.
  • Loading branch information
adrianbenavides authored and mergify[bot] committed Sep 22, 2022
1 parent 08281f1 commit fe6e34b
Show file tree
Hide file tree
Showing 4 changed files with 456 additions and 4 deletions.
1 change: 1 addition & 0 deletions implementations/rust/ockam/ockam_command/Cargo.toml
Expand Up @@ -87,3 +87,4 @@ ockam_core = { path = "../ockam_core", version = "^0.70.0" }

[dev-dependencies]
assert_cmd = "2"
tempfile = "3"
37 changes: 35 additions & 2 deletions implementations/rust/ockam/ockam_command/src/lib.rs
Expand Up @@ -26,6 +26,7 @@ mod util;
mod vault;
mod version;

use anyhow::Context;
use authenticated::AuthenticatedCommand;
use completion::CompletionCommand;
use configuration::ConfigurationCommand;
Expand All @@ -37,10 +38,12 @@ use identity::IdentityCommand;
use message::MessageCommand;
use node::NodeCommand;
use project::ProjectCommand;
use rand::prelude::random;
use reset::ResetCommand;
use secure_channel::{listener::SecureChannelListenerCommand, SecureChannelCommand};
use service::ServiceCommand;
use space::SpaceCommand;
use std::path::PathBuf;
use tcp::{
connection::TcpConnectionCommand, inlet::TcpInletCommand, listener::TcpListenerCommand,
outlet::TcpOutletCommand,
Expand Down Expand Up @@ -193,6 +196,9 @@ pub struct GlobalArgs {
// but the command is not executed.
#[clap(global = true, long, hide = true)]
test_argument_parser: bool,

#[clap(flatten)]
export: ExportCommandArgs,
}

#[derive(Debug, Clone, ArgEnum, PartialEq, Eq)]
Expand All @@ -201,6 +207,22 @@ pub enum OutputFormat {
Json,
}

#[derive(Debug, Clone, Args)]
pub struct ExportCommandArgs {
/// Export the command input to a file.
/// Used to run a set of commands after creating a node with `ockam node create --run commands.json`
#[clap(global = true, long = "export")]
export_path: Option<PathBuf>,

/// Unique name for the exported command.
#[clap(global = true, long, hide_default_value = true, default_value_t = hex::encode(&random::<[u8;4]>()))]
export_as: String,

/// Reference to a previously exported command that must be run before the current command.
#[clap(global = true, long)]
depends_on: Option<String>,
}

#[derive(Clone)]
pub struct CommandGlobalOpts {
pub global_args: GlobalArgs,
Expand Down Expand Up @@ -261,8 +283,10 @@ pub enum OckamSubcommand {
}

pub fn run() {
let input = std::env::args().map(replace_hyphen_with_stdin);

let input = std::env::args()
.map(replace_hyphen_with_stdin)
.collect::<Vec<_>>();
let args = input.clone();
let command: OckamCommand = OckamCommand::parse_from(input);
if !command.global_args.test_argument_parser {
check_if_an_upgrade_is_available();
Expand All @@ -276,6 +300,15 @@ pub fn run() {
tracing::debug!("Parsed {:?}", &command);
}

if let Some(path) = command.global_args.export.export_path {
let name = command.global_args.export.export_as.clone();
let depends_on = command.global_args.export.depends_on.clone();
node::util::run::CommandsRunner::export(path, name, depends_on, args)
.context("Failed to export command")
.unwrap();
return;
}

let options = CommandGlobalOpts::new(command.global_args, config);

// If test_argument_parser is true, command arguments are checked
Expand Down
9 changes: 9 additions & 0 deletions implementations/rust/ockam/ockam_command/src/node/create.rs
Expand Up @@ -83,6 +83,9 @@ pub struct CreateCommand {

#[clap(long, hide = true)]
pub project: Option<PathBuf>,

#[clap(long, hide = true)]
pub config: Option<PathBuf>,
}

impl From<&'_ CreateCommand> for ComposableSnippet {
Expand Down Expand Up @@ -110,6 +113,7 @@ impl Default for CreateCommand {
launch_config: None,
no_watchdog: false,
project: None,
config: None,
}
}
}
Expand Down Expand Up @@ -164,6 +168,11 @@ impl CreateCommand {
(cfg.clone(), cmd.node_name, true),
print_query_status,
);
if let Some(config) = self.config {
crate::node::util::run::CommandsRunner::run(&config)
.context("Failed to run commands from config")
.unwrap();
}
}
}

Expand Down

0 comments on commit fe6e34b

Please sign in to comment.