forked from kpcyrd/sn0int
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkspace_cmd.rs
108 lines (93 loc) · 2.62 KB
/
workspace_cmd.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use clap::Parser;
use crate::blobs::BlobStorage;
use crate::cmd::{Cmd, LiteCmd};
use crate::config::Config;
use crate::db::Database;
use crate::errors::*;
use crate::shell::Shell;
use crate::term;
use crate::utils;
use crate::workspaces::{self, Workspace};
#[derive(Debug, Parser)]
pub struct Args {
/// Delete a workspace
#[arg(long = "delete", group = "action")]
delete: bool,
/// Show disk usage of workspace
#[arg(long = "usage", group = "action")]
usage: bool,
/// Skip confirmation
#[arg(short = 'f', long = "force")]
force: bool,
workspaces: Vec<Workspace>,
}
fn delete(workspace: Workspace, force: bool) -> Result<()> {
if !force && !utils::no_else_yes(&format!("Do you really want to delete {:?}", workspace.as_str()))? {
return Ok(());
}
term::info(&format!("Deleting workspace: {:?}", workspace.as_str()));
workspace.delete()?;
Ok(())
}
fn usage(mut workspaces: Vec<Workspace>) -> Result<()> {
if workspaces.is_empty() {
workspaces = workspaces::list()?;
}
for ws in workspaces {
println!("{:50}: {}", ws.as_str(), ws.usage_human()?);
}
Ok(())
}
fn change(rl: &mut Shell, workspace: Workspace) -> Result<()> {
workspace.migrate()?;
let blobs = BlobStorage::workspace(&workspace)?;
let db = Database::establish(workspace)?;
rl.set_blobstorage(blobs);
rl.set_db(db);
Ok(())
}
fn list() -> Result<()> {
for ws in workspaces::list()? {
println!("{}", ws.as_str());
}
Ok(())
}
fn run(mut args: Args, rl: Option<&mut Shell>) -> Result<()> {
if args.delete {
if args.workspaces.is_empty() {
bail!("--delete requires workspace");
}
for workspace in args.workspaces {
if let Some(rl) = &rl {
if *rl.db().workspace() == workspace {
bail!("Can't delete current workspace")
}
}
delete(workspace, args.force)?;
}
} else if args.usage {
usage(args.workspaces)?;
} else {
match args.workspaces.len() {
0 => list()?,
1 => if let Some(rl) = rl {
// we've already tested there's one item in the list
change(rl, args.workspaces.pop().unwrap())?;
},
_ => bail!("Only one argument allowed when switching workspaces"),
}
}
Ok(())
}
impl Cmd for Args {
#[inline]
fn run(self, rl: &mut Shell) -> Result<()> {
run(self, Some(rl))
}
}
impl LiteCmd for Args {
#[inline]
fn run(self, _config: &Config) -> Result<()> {
run(self, None)
}
}