mirror of
https://codeberg.org/icewind/haze.git
synced 2026-06-03 17:14:08 +02:00
move checkout to new git subcommand
This commit is contained in:
parent
666b58a773
commit
d86ee53f66
3 changed files with 320 additions and 176 deletions
207
src/args.rs
207
src/args.rs
|
|
@ -2,11 +2,28 @@ use crate::cloud::CloudOptions;
|
|||
use crate::config::{HazeConfig, Preset};
|
||||
use crate::service::{Service, ServiceTrait};
|
||||
use miette::{IntoDiagnostic, Report, Result};
|
||||
use std::fmt::Display;
|
||||
use std::fmt::{Debug, Display};
|
||||
use std::rc::Rc;
|
||||
use std::str::FromStr;
|
||||
use strum::{Display, EnumIter, EnumMessage, EnumProperty, EnumString, IntoStaticStr};
|
||||
use strum::{
|
||||
Display, EnumIter, EnumMessage, EnumProperty, EnumString, IntoEnumIterator, IntoStaticStr,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||
pub trait SubCommand: Display + EnumMessage + EnumProperty + Debug {
|
||||
fn parent(&self) -> &'static str {
|
||||
""
|
||||
}
|
||||
|
||||
fn allows_filter(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn sub_commands(&self) -> Option<Box<dyn Iterator<Item = Box<dyn SubCommand>>>> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum HazeArgs {
|
||||
List {
|
||||
filter: Option<String>,
|
||||
|
|
@ -65,8 +82,8 @@ pub enum HazeArgs {
|
|||
filter: Option<String>,
|
||||
},
|
||||
Proxy,
|
||||
Checkout {
|
||||
branch: String,
|
||||
Git {
|
||||
operation: GitOperation,
|
||||
},
|
||||
Env {
|
||||
filter: Option<String>,
|
||||
|
|
@ -75,7 +92,7 @@ pub enum HazeArgs {
|
|||
},
|
||||
Update,
|
||||
Help {
|
||||
command: Option<HazeCommand>,
|
||||
command: Option<Rc<dyn SubCommand>>,
|
||||
},
|
||||
Version,
|
||||
Edit {
|
||||
|
|
@ -84,6 +101,33 @@ pub enum HazeArgs {
|
|||
},
|
||||
}
|
||||
|
||||
#[derive(
|
||||
Debug,
|
||||
Clone,
|
||||
Eq,
|
||||
PartialEq,
|
||||
Display,
|
||||
EnumProperty,
|
||||
EnumString,
|
||||
IntoStaticStr,
|
||||
EnumMessage,
|
||||
EnumIter,
|
||||
)]
|
||||
#[strum(serialize_all = "lowercase")]
|
||||
pub enum GitOperation {
|
||||
/// Checkout a branch in all apps
|
||||
///
|
||||
/// "main" and "master" can be used interchangeably.
|
||||
#[strum(props(Args = "[branch] branch to checkout"))]
|
||||
Checkout { branch: String },
|
||||
}
|
||||
|
||||
impl SubCommand for GitOperation {
|
||||
fn parent(&self) -> &'static str {
|
||||
" git"
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||
pub enum LogService {
|
||||
Service(Service),
|
||||
|
|
@ -271,7 +315,26 @@ impl HazeArgs {
|
|||
.next()
|
||||
.map(S::into)
|
||||
.ok_or_else(|| Report::msg("No branch provided"))?;
|
||||
Ok(HazeArgs::Checkout { branch })
|
||||
Ok(HazeArgs::Git {
|
||||
operation: GitOperation::Checkout { branch },
|
||||
})
|
||||
}
|
||||
HazeCommand::Git => {
|
||||
let operation = args
|
||||
.next()
|
||||
.ok_or_else(|| Report::msg("No git operation provided"))?;
|
||||
match operation.as_ref() {
|
||||
"checkout" => {
|
||||
let branch = args
|
||||
.next()
|
||||
.map(S::into)
|
||||
.ok_or_else(|| Report::msg("No branch provided"))?;
|
||||
Ok(HazeArgs::Git {
|
||||
operation: GitOperation::Checkout { branch },
|
||||
})
|
||||
}
|
||||
operation => Err(Report::msg(format!("Unknown git operation {operation}"))),
|
||||
}
|
||||
}
|
||||
HazeCommand::Env => {
|
||||
let mut args = args.map(S::into);
|
||||
|
|
@ -286,13 +349,21 @@ impl HazeArgs {
|
|||
}
|
||||
HazeCommand::Update => Ok(HazeArgs::Update),
|
||||
HazeCommand::Help => {
|
||||
let command = args.next();
|
||||
let command = command
|
||||
.as_ref()
|
||||
.map(|s| s.as_ref())
|
||||
.map(HazeCommand::from_str)
|
||||
.transpose()
|
||||
.map_err(|_| Report::msg("Unknown command"))?;
|
||||
let command = args
|
||||
.next()
|
||||
.map(|command| match command.as_ref() {
|
||||
"git" => match args.next() {
|
||||
Some(op) => GitOperation::from_str(op.as_ref())
|
||||
.map(|op| Rc::new(op) as Rc<dyn SubCommand>)
|
||||
.map_err(|_| Report::msg("Unknown command")),
|
||||
None => Ok(Rc::new(HazeCommand::Git) as Rc<dyn SubCommand>),
|
||||
},
|
||||
command => HazeCommand::from_str(command)
|
||||
.map(|command| Rc::new(command) as Rc<dyn SubCommand>)
|
||||
.map_err(|_| Report::msg("Unknown command")),
|
||||
})
|
||||
.transpose()?;
|
||||
|
||||
Ok(HazeArgs::Help { command })
|
||||
}
|
||||
HazeCommand::Version => Ok(HazeArgs::Version),
|
||||
|
|
@ -372,10 +443,9 @@ pub enum HazeCommand {
|
|||
Unpin,
|
||||
/// Start the proxy
|
||||
Proxy,
|
||||
/// Checkout a branch in all apps
|
||||
///
|
||||
/// Only switches branches if the target branch exists locally.
|
||||
/// "main" and "master" can be used interchangeably.
|
||||
/// Perform git operations on all apps
|
||||
Git,
|
||||
/// Deprecated, use `haze git checkout` instead
|
||||
#[strum(props(Args = "[branch] branch to checkout"))]
|
||||
Checkout,
|
||||
/// Run command with notify_push environment variables
|
||||
|
|
@ -394,8 +464,8 @@ pub enum HazeCommand {
|
|||
Edit,
|
||||
}
|
||||
|
||||
impl HazeCommand {
|
||||
pub fn allows_filter(&self) -> bool {
|
||||
impl SubCommand for HazeCommand {
|
||||
fn allows_filter(&self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
HazeCommand::List
|
||||
|
|
@ -411,6 +481,13 @@ impl HazeCommand {
|
|||
| HazeCommand::Edit
|
||||
)
|
||||
}
|
||||
|
||||
fn sub_commands(&self) -> Option<Box<dyn Iterator<Item = Box<dyn SubCommand>>>> {
|
||||
match self {
|
||||
HazeCommand::Git => Some(Box::new(GitOperation::iter().map(|op| Box::new(op) as _))),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -425,45 +502,49 @@ fn test_arg_parse() {
|
|||
preset: vec![],
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
fn assert_eq<A: Debug, B: Debug>(a: A, b: B) {
|
||||
assert_eq!(format!("{a:?}"), format!("{b:?}"));
|
||||
}
|
||||
|
||||
assert_eq(
|
||||
HazeArgs::parse(&config, vec!["haze"].into_iter()).unwrap(),
|
||||
HazeArgs::List { filter: None }
|
||||
HazeArgs::List { filter: None },
|
||||
);
|
||||
assert_eq!(
|
||||
assert_eq(
|
||||
HazeArgs::parse(&config, vec!["haze", "test"].into_iter()).unwrap(),
|
||||
HazeArgs::Test {
|
||||
options: Default::default(),
|
||||
args: vec![]
|
||||
}
|
||||
args: vec![],
|
||||
},
|
||||
);
|
||||
assert_eq!(
|
||||
assert_eq(
|
||||
HazeArgs::parse(&config, vec!["haze", "asdasd"].into_iter()).unwrap(),
|
||||
HazeArgs::List {
|
||||
filter: Some("asdasd".to_string())
|
||||
}
|
||||
filter: Some("asdasd".to_string()),
|
||||
},
|
||||
);
|
||||
assert_eq!(
|
||||
assert_eq(
|
||||
HazeArgs::parse(&config, vec!["haze", "asdasd", "db"].into_iter()).unwrap(),
|
||||
HazeArgs::Db {
|
||||
filter: Some("asdasd".to_string()),
|
||||
root: false,
|
||||
command: Vec::new(),
|
||||
index: None,
|
||||
}
|
||||
},
|
||||
);
|
||||
assert_eq!(
|
||||
assert_eq(
|
||||
HazeArgs::parse(&config, vec!["haze", "asdasd", "db", "root"].into_iter()).unwrap(),
|
||||
HazeArgs::Db {
|
||||
filter: Some("asdasd".to_string()),
|
||||
root: true,
|
||||
command: Vec::new(),
|
||||
index: None,
|
||||
}
|
||||
},
|
||||
);
|
||||
assert_eq!(
|
||||
assert_eq(
|
||||
HazeArgs::parse(
|
||||
&config,
|
||||
vec!["haze", "asdasd", "db", "select", "1"].into_iter()
|
||||
vec!["haze", "asdasd", "db", "select", "1"].into_iter(),
|
||||
)
|
||||
.unwrap(),
|
||||
HazeArgs::Db {
|
||||
|
|
@ -471,12 +552,12 @@ fn test_arg_parse() {
|
|||
root: false,
|
||||
command: vec!["select".to_string(), "1".to_string()],
|
||||
index: None,
|
||||
}
|
||||
},
|
||||
);
|
||||
assert_eq!(
|
||||
assert_eq(
|
||||
HazeArgs::parse(
|
||||
&config,
|
||||
vec!["haze", "asdasd", "db", "root", "select 1"].into_iter()
|
||||
vec!["haze", "asdasd", "db", "root", "select 1"].into_iter(),
|
||||
)
|
||||
.unwrap(),
|
||||
HazeArgs::Db {
|
||||
|
|
@ -484,12 +565,12 @@ fn test_arg_parse() {
|
|||
root: true,
|
||||
command: vec!["select 1".to_string()],
|
||||
index: None,
|
||||
}
|
||||
},
|
||||
);
|
||||
assert_eq!(
|
||||
assert_eq(
|
||||
HazeArgs::parse(
|
||||
&config,
|
||||
vec!["haze", "asdasd", "db", "root", "1", "select 1"].into_iter()
|
||||
vec!["haze", "asdasd", "db", "root", "1", "select 1"].into_iter(),
|
||||
)
|
||||
.unwrap(),
|
||||
HazeArgs::Db {
|
||||
|
|
@ -497,12 +578,12 @@ fn test_arg_parse() {
|
|||
root: true,
|
||||
command: vec!["select 1".to_string()],
|
||||
index: Some("1".into()),
|
||||
}
|
||||
},
|
||||
);
|
||||
assert_eq!(
|
||||
assert_eq(
|
||||
HazeArgs::parse(
|
||||
&config,
|
||||
vec!["haze", "asdasd", "db", "all", "select 1"].into_iter()
|
||||
vec!["haze", "asdasd", "db", "all", "select 1"].into_iter(),
|
||||
)
|
||||
.unwrap(),
|
||||
HazeArgs::Db {
|
||||
|
|
@ -510,21 +591,21 @@ fn test_arg_parse() {
|
|||
root: false,
|
||||
command: vec!["select 1".to_string()],
|
||||
index: Some("all".into()),
|
||||
}
|
||||
},
|
||||
);
|
||||
assert_eq!(
|
||||
assert_eq(
|
||||
HazeArgs::parse(&config, vec!["haze", "exec", "foo", "bar"].into_iter()).unwrap(),
|
||||
HazeArgs::Exec {
|
||||
filter: None,
|
||||
service: None,
|
||||
command: vec!["foo".to_string(), "bar".to_string()],
|
||||
root: false,
|
||||
}
|
||||
},
|
||||
);
|
||||
assert_eq!(
|
||||
assert_eq(
|
||||
HazeArgs::parse(
|
||||
&config,
|
||||
vec!["haze", "exec", "root", "foo", "bar"].into_iter()
|
||||
vec!["haze", "exec", "root", "foo", "bar"].into_iter(),
|
||||
)
|
||||
.unwrap(),
|
||||
HazeArgs::Exec {
|
||||
|
|
@ -532,12 +613,12 @@ fn test_arg_parse() {
|
|||
service: None,
|
||||
command: vec!["foo".to_string(), "bar".to_string()],
|
||||
root: true,
|
||||
}
|
||||
},
|
||||
);
|
||||
assert_eq!(
|
||||
assert_eq(
|
||||
HazeArgs::parse(
|
||||
&config,
|
||||
vec!["haze", "asdasd", "exec", "foo", "bar"].into_iter()
|
||||
vec!["haze", "asdasd", "exec", "foo", "bar"].into_iter(),
|
||||
)
|
||||
.unwrap(),
|
||||
HazeArgs::Exec {
|
||||
|
|
@ -545,12 +626,12 @@ fn test_arg_parse() {
|
|||
service: None,
|
||||
command: vec!["foo".to_string(), "bar".to_string()],
|
||||
root: false,
|
||||
}
|
||||
},
|
||||
);
|
||||
assert_eq!(
|
||||
assert_eq(
|
||||
HazeArgs::parse(
|
||||
&config,
|
||||
vec!["haze", "asdasd", "exec", "db", "foo", "bar"].into_iter()
|
||||
vec!["haze", "asdasd", "exec", "db", "foo", "bar"].into_iter(),
|
||||
)
|
||||
.unwrap(),
|
||||
HazeArgs::Exec {
|
||||
|
|
@ -558,28 +639,28 @@ fn test_arg_parse() {
|
|||
service: Some(ExecService::Db),
|
||||
command: vec!["foo".to_string(), "bar".to_string()],
|
||||
root: false,
|
||||
}
|
||||
},
|
||||
);
|
||||
assert_eq!(
|
||||
assert_eq(
|
||||
HazeArgs::parse(&config, vec!["haze", "test", "foo", "bar"].into_iter()).unwrap(),
|
||||
HazeArgs::Test {
|
||||
options: Default::default(),
|
||||
args: vec!["foo".into(), "bar".into()]
|
||||
}
|
||||
args: vec!["foo".into(), "bar".into()],
|
||||
},
|
||||
);
|
||||
assert_eq!(
|
||||
assert_eq(
|
||||
HazeArgs::parse(&config, vec!["haze", "logs", "-f", "smb"].into_iter()).unwrap(),
|
||||
HazeArgs::Logs {
|
||||
filter: None,
|
||||
follow: true,
|
||||
service: Some(LogService::from_type(&[], "smb").unwrap()),
|
||||
count: None,
|
||||
}
|
||||
},
|
||||
);
|
||||
assert_eq!(
|
||||
assert_eq(
|
||||
HazeArgs::parse(
|
||||
&config,
|
||||
vec!["haze", "asdasd", "logs", "smb", "123"].into_iter()
|
||||
vec!["haze", "asdasd", "logs", "smb", "123"].into_iter(),
|
||||
)
|
||||
.unwrap(),
|
||||
HazeArgs::Logs {
|
||||
|
|
@ -587,6 +668,6 @@ fn test_arg_parse() {
|
|||
follow: false,
|
||||
service: Some(LogService::from_type(&[], "smb").unwrap()),
|
||||
count: Some(123),
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue