Extend commands help display.

Add match actions for deploy module.
This commit is contained in:
Chris Daßler 2024-08-06 11:10:30 +02:00
parent bb85b2054c
commit 54adfe5095
2 changed files with 119 additions and 22 deletions

View File

@ -1,5 +1,5 @@
use nu_plugin::{EngineInterface, EvaluatedCall, SimplePluginCommand}; use nu_plugin::{EngineInterface, EvaluatedCall, SimplePluginCommand};
use nu_protocol::{record, Category, Example, LabeledError, Signature, Type, Value}; use nu_protocol::{record, Category, Example, LabeledError, Signature, Value};
use crate::OncePlugin; use crate::OncePlugin;
@ -17,13 +17,13 @@ impl SimplePluginCommand for Scenario {
} }
fn extra_usage(&self) -> &str { fn extra_usage(&self) -> &str {
"Extra usage for once scenario" r"This command shows all available scenarios. To manage these scenarios use the sub commands, e.g.
> once scenario.deploy"
} }
fn signature(&self) -> Signature { fn signature(&self) -> Signature {
Signature::build(self.name()) Signature::build(self.name()).category(Category::Experimental)
.category(Category::Experimental)
.input_output_type(Type::Nothing, Type::table())
} }
fn search_terms(&self) -> Vec<&str> { fn search_terms(&self) -> Vec<&str> {

View File

@ -1,5 +1,5 @@
use nu_plugin::{EngineInterface, EvaluatedCall, SimplePluginCommand}; use nu_plugin::{EngineInterface, EvaluatedCall, SimplePluginCommand};
use nu_protocol::{Category, Example, LabeledError, Signature, SyntaxShape, Value}; use nu_protocol::{Category, Example, LabeledError, Signature, Span, SyntaxShape, Value};
use crate::OncePlugin; use crate::OncePlugin;
@ -17,19 +17,31 @@ impl SimplePluginCommand for ScenarioDeploy {
} }
fn extra_usage(&self) -> &str { fn extra_usage(&self) -> &str {
"Extra usage for once scenario.deploy" r" Lifecycle actions:
init - init remote scenario dir
up - Create and start scenario
stop - Stop scenario
start - Start scenario if stopped
down - Stop and shut down scenario
deinit - Cleanup/remove remote and local scenario dir (leave config untouched)
Service actions:
test - Test the running scenario
updateconfig - update local scenario config"
} }
fn signature(&self) -> Signature { fn signature(&self) -> Signature {
// The signature defines the usage of the command inside Nu, and also automatically // The signature defines the usage of the command inside Nu, and also automatically
// generates its help page. // generates its help page.
Signature::build(self.name()) Signature::build(self.name())
.required("a", SyntaxShape::Int, "required integer value") .required("scenario", SyntaxShape::String, "Use available scenario")
.required("b", SyntaxShape::String, "required string value") .required(
.switch("flag", "a flag for the signature", Some('f')) "action",
.optional("opt", SyntaxShape::Int, "Optional number") SyntaxShape::String,
.named("named", SyntaxShape::String, "named string", Some('n')) "Actions [init,up,start,stop,down,deinit,test,updateconfig]",
.rest("rest", SyntaxShape::String, "rest value string") )
.switch("verbose", "Set verbosity for command", Some('v'))
.switch("silent", "Set silent mode for command", Some('s'))
.category(Category::Experimental) .category(Category::Experimental)
} }
@ -38,26 +50,111 @@ impl SimplePluginCommand for ScenarioDeploy {
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
vec![Example { vec![
example: "once scenario.deploy 3 bb", Example {
description: "running scenario.deploy with an int value and string value", example: "once scenario.deploy localhost/certbot init -s",
result: None, description: "Initialize scenario 'localhost/certbot' silently.",
}] result: None,
},
Example {
example: "once scenario.deploy localhost/certbot up -v",
description: "Create and start scenario 'localhost/certbot' in verbose mode.",
result: None,
},
]
} }
fn run( fn run(
&self, &self,
plugin: &OncePlugin, _plugin: &OncePlugin,
_engine: &EngineInterface, _engine: &EngineInterface,
call: &EvaluatedCall, call: &EvaluatedCall,
input: &Value, _input: &Value,
) -> Result<Value, LabeledError> { ) -> Result<Value, LabeledError> {
plugin.print_values(1, call, input)?; let head = call.head;
let scenario: String = call.req(0)?;
let action: String = call.req(1)?;
let verbose = call.has_flag("verbose")?;
let silent = call.has_flag("silent")?;
Ok(Value::nothing(call.head)) println!("Silent: {:?}, Verbose: {:?}", silent, verbose);
match action.as_str() {
"init" => action_init(scenario, head),
"up" => action_up(scenario, head),
"start" => action_start(scenario, head),
"stop" => action_stop(scenario, head),
"down" => action_down(scenario, head),
"deinit" => action_deinit(scenario, head),
"test" => action_test(scenario, head),
"updateconfig" => action_updateconfig(scenario, head),
_ => Ok(Value::String {
val: String::from("The calling action is not implemented."),
internal_span: head,
}),
}
} }
} }
fn action_init(scenario: String, head: Span) -> Result<Value, LabeledError> {
Ok(Value::String {
val: format!("Action 'init' for scenario '{}' was triggered.", scenario),
internal_span: head,
})
}
fn action_up(scenario: String, head: Span) -> Result<Value, LabeledError> {
Ok(Value::String {
val: format!("Action 'up' for scenario '{}' was triggered.", scenario),
internal_span: head,
})
}
fn action_start(scenario: String, head: Span) -> Result<Value, LabeledError> {
Ok(Value::String {
val: format!("Action 'start' for scenario '{}' was triggered.", scenario),
internal_span: head,
})
}
fn action_stop(scenario: String, head: Span) -> Result<Value, LabeledError> {
Ok(Value::String {
val: format!("Action 'stop' for scenario '{}' was triggered.", scenario),
internal_span: head,
})
}
fn action_down(scenario: String, head: Span) -> Result<Value, LabeledError> {
Ok(Value::String {
val: format!("Action 'down' for scenario '{}' was triggered.", scenario),
internal_span: head,
})
}
fn action_deinit(scenario: String, head: Span) -> Result<Value, LabeledError> {
Ok(Value::String {
val: format!("Action 'deinit' for scenario '{}' was triggered.", scenario),
internal_span: head,
})
}
fn action_test(scenario: String, head: Span) -> Result<Value, LabeledError> {
Ok(Value::String {
val: format!("Action 'test' for scenario '{}' was triggered.", scenario),
internal_span: head,
})
}
fn action_updateconfig(scenario: String, head: Span) -> Result<Value, LabeledError> {
Ok(Value::String {
val: format!(
"Action 'updateconfig' for scenario '{}' was triggered.",
scenario
),
internal_span: head,
})
}
#[test] #[test]
fn test_examples() -> Result<(), nu_protocol::ShellError> { fn test_examples() -> Result<(), nu_protocol::ShellError> {
use nu_plugin_test_support::PluginTest; use nu_plugin_test_support::PluginTest;