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
//! Command List
use crate::{registry::Registry, result::Result};
use std::cmp::Ordering;
use structopt::StructOpt;

const MAX_PACKAGE_NAME_LEN: usize = 50;

/// List registry source or tags
#[derive(Debug, StructOpt)]
pub enum List {
    /// List source crates of registry
    Source {
        /// Filter by tag
        #[structopt(short, long)]
        tag: Option<String>,
    },
    /// List tags of registry
    Tag {
        /// Limit the number of tags
        #[structopt(short, long, default_value = "10")]
        limit: usize,
    },
}

fn cap(mut name: String) -> String {
    name.push_str(&" ".repeat(MAX_PACKAGE_NAME_LEN - name.len()));
    name
}

/// Exec command `list`
pub fn exec(registry: Registry, list: List) -> Result<()> {
    match list {
        List::Source { tag } => {
            let mut should_checkout = false;
            let mut source = registry.source()?;
            source.sort_by(|(np, _), (nq, _)| np.partial_cmp(nq).unwrap_or(Ordering::Greater));

            if let Some(tag) = tag {
                if !tag.is_empty() {
                    should_checkout = true;
                    registry.checkout(&tag)?;
                }
            }

            // Prints source
            println!(
                "{}",
                source
                    .iter()
                    .map(|(name, ver)| { format!("{} - {}", cap(name.to_string()), ver) })
                    .collect::<Vec<_>>()
                    .join("\n")
            );

            // Checkout back to the latest commit
            if should_checkout {
                registry.checkout("master")?;
            }
        }
        List::Tag { limit } => {
            // Get tags
            let mut tags = registry.tag()?;
            if tags.is_empty() {
                registry.update()?;
            }

            let last = if limit < tags.len() || limit < 1 {
                limit
            } else {
                tags.len()
            };

            tags.reverse();
            println!("{}", &tags[..last].join("\n"));
        }
    }

    Ok(())
}