Skip to content
This repository has been archived by the owner on May 11, 2023. It is now read-only.

patch: Add create and list operation #267

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 36 additions & 11 deletions patch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,27 @@ impl Default for Update {
}
}

#[derive(Default, Debug)]
#[derive(Debug, PartialEq, Eq)]
pub enum OperationName {
Create,
List,
}

impl Default for OperationName {
fn default() -> Self {
Self::List
}
}

#[derive(Debug)]
pub enum Operation {
Create,
List,
}

#[derive(Debug)]
pub struct Options {
pub list: bool,
pub op: Operation,
pub verbose: bool,
pub sync: bool,
pub push: bool,
Expand All @@ -94,7 +112,7 @@ impl Args for Options {
use lexopt::prelude::*;

let mut parser = lexopt::Parser::from_args(args);
let mut list = false;
let mut op: Option<OperationName> = None;
let mut verbose = false;
let mut sync = true;
let mut message = Comment::default();
Expand All @@ -103,9 +121,6 @@ impl Args for Options {

while let Some(arg) = parser.next()? {
match arg {
Long("list") | Short('l') => {
list = true;
}
Long("verbose") | Short('v') => {
verbose = true;
}
Expand Down Expand Up @@ -144,13 +159,24 @@ impl Args for Options {
Long("help") => {
return Err(Error::Help.into());
}
Value(val) if op.is_none() => match val.to_string_lossy().as_ref() {
"n" | "new" => op = Some(OperationName::Create),
"l" | "list" => op = Some(OperationName::List),

unknown => anyhow::bail!("unknown operation '{}'", unknown),
},
_ => return Err(anyhow::anyhow!(arg.unexpected())),
}
}

let op = match op.unwrap_or_default() {
OperationName::Create => Operation::Create,
OperationName::List => Operation::List,
};

Ok((
Options {
list,
op,
sync,
message,
push,
Expand All @@ -172,10 +198,9 @@ pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
let project = project::get(&storage, &urn)?
.ok_or_else(|| anyhow!("couldn't load project {} from local state", urn))?;

if options.list {
list(&storage, Some(repo), &profile, &project, options)?;
} else {
create(&storage, &profile, &project, &repo, options)?;
match options.op {
Operation::Create => create(&storage, &profile, &project, &repo, options)?,
Operation::List => list(&storage, Some(repo), &profile, &project, options)?,
}

Ok(())
Expand Down