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
extern crate serde_json;

use crate::commands::kv;
use crate::commands::kv::key::KeyList;
use crate::settings::global_user::GlobalUser;
use crate::settings::target::Target;

// Note: this function only prints keys in json form, given that
// the number of entries in each json blob is variable (so csv and tsv
// representation won't make sense)
pub fn list(
    target: &Target,
    user: &GlobalUser,
    namespace_id: &str,
    prefix: Option<&str>,
) -> Result<(), failure::Error> {
    kv::validate_target(target)?;
    let key_list = KeyList::new(target, user, namespace_id, prefix)?;

    print!("["); // Open json list bracket

    let mut first_key = true;

    for key_result in key_list {
        match key_result {
            Ok(key) => {
                if first_key {
                    first_key = false;
                } else {
                    print!(",");
                }

                print!("{}", serde_json::to_string(&key)?);
            }
            Err(e) => print!("{}", kv::format_error(e)),
        }
    }

    print!("]"); // Close json list bracket

    Ok(())
}