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
use crate::errors::*;

use crate::blobs::BlobStorage;
use crate::cmd::Cmd;
use crate::db::Database;
use crate::shell::Shell;
use crate::term;
use crate::utils;
use structopt::StructOpt;
use structopt::clap::AppSettings;
use crate::workspaces::{self, Workspace};


#[derive(Debug, StructOpt)]
#[structopt(global_settings = &[AppSettings::ColoredHelp])]
pub struct Args {
    /// Delete a workspaceb
    #[structopt(long = "delete", group = "action")]
    delete: bool,
    /// Show disk usage of workspace
    #[structopt(long = "usage", group = "action")]
    usage: bool,
    /// Skip confirmation
    #[structopt(short = "f", long = "force")]
    force: bool,
    workspace: Option<Workspace>,
}

fn delete(workspace: Workspace, force: bool) -> Result<()> {
    if !force {
        if !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(workspace: Option<Workspace>) -> Result<()> {
    if let Some(ws) = workspace {
        println!("{}", ws.usage_human()?);
    } else {
        for ws in workspaces::list()? {
            println!("{:50}: {}", ws.as_str(), ws.usage_human()?);
        }
    }

    Ok(())
}

fn change(rl: &mut Shell, workspace: Workspace) -> Result<()> {
    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(())
}

impl Cmd for Args {
    fn run(self, rl: &mut Shell) -> Result<()> {
        if self.delete {
            if let Some(workspace) = self.workspace {
                if *rl.db().workspace() == workspace {
                    bail!("Can't delete current workspace")
                }

                delete(workspace, self.force)
            } else {
                bail!("--delete requires workspace")
            }
        } else if self.usage {
            usage(self.workspace)
        } else if let Some(workspace) = self.workspace {
            change(rl, workspace)
        } else {
            list()
        }
    }
}

#[inline]
pub fn run(rl: &mut Shell, args: &[String]) -> Result<()> {
    Args::run_str(rl, args)
}