nushell/crates/nu-command/src/experimental/git_checkout.rs

73 lines
2.1 KiB
Rust
Raw Normal View History

2021-09-29 20:25:05 +02:00
use nu_engine::eval_expression;
use nu_protocol::ast::Call;
2021-10-25 18:58:58 +02:00
use nu_protocol::engine::{Command, EngineState, Stack};
2021-10-25 06:01:02 +02:00
use nu_protocol::{IntoPipelineData, PipelineData, Signature, SyntaxShape, Value};
2021-09-29 20:25:05 +02:00
2021-10-25 06:01:02 +02:00
#[derive(Clone)]
2021-09-29 20:25:05 +02:00
pub struct GitCheckout;
impl Command for GitCheckout {
fn name(&self) -> &str {
"git checkout"
}
fn usage(&self) -> &str {
"Checkout a git revision"
}
fn signature(&self) -> nu_protocol::Signature {
Signature::build("git checkout").required(
"branch",
SyntaxShape::Custom(Box::new(SyntaxShape::String), "list-git-branches".into()),
"the branch to checkout",
)
}
fn run(
&self,
2021-10-25 08:31:39 +02:00
engine_state: &EngineState,
stack: &mut Stack,
2021-09-29 20:25:05 +02:00
call: &Call,
2021-10-25 06:01:02 +02:00
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
2021-09-29 20:25:05 +02:00
use std::process::Command as ProcessCommand;
use std::process::Stdio;
let block = &call.positional[0];
2021-10-25 08:31:39 +02:00
let out = eval_expression(engine_state, stack, block)?;
2021-09-29 20:25:05 +02:00
let out = out.as_string()?;
let proc = ProcessCommand::new("git")
.arg("checkout")
.arg(out)
.stdout(Stdio::piped())
.spawn();
match proc {
Ok(child) => {
match child.wait_with_output() {
Ok(val) => {
let result = val.stdout;
2021-10-20 07:58:25 +02:00
Ok(Value::String {
val: String::from_utf8_lossy(&result).to_string(),
span: call.head,
2021-10-25 06:01:02 +02:00
}
.into_pipeline_data())
2021-09-29 20:25:05 +02:00
}
Err(_err) => {
2021-10-12 19:44:23 +02:00
// FIXME: Move this to an external signature and add better error handling
2021-10-25 06:01:02 +02:00
Ok(PipelineData::new())
2021-09-29 20:25:05 +02:00
}
}
}
Err(_err) => {
2021-10-12 19:44:23 +02:00
// FIXME: Move this to an external signature and add better error handling
2021-10-25 06:01:02 +02:00
Ok(PipelineData::new())
2021-09-29 20:25:05 +02:00
}
}
}
}