Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better format for cage source ls #63

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ itertools = "0.5.6"
lazy_static = "0.2.1"
log = "0.3.6"
phf = "0.7.16"
prettytable-rs = "0.2.0"
rand = "0.3.14"
rayon = "0.4.2"
regex = "0.1.73"
Expand Down
46 changes: 35 additions & 11 deletions src/cmd/source.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
//! The `source` subcommand.

use colored::*;

use command_runner::CommandRunner;
use errors::*;
use project::Project;

extern crate prettytable;
use self::prettytable::Table;
use self::prettytable::format::FORMAT_BLANK;

/// We implement `source` with a trait so we put it in its own module.
pub trait CommandSource {
/// List all the source trees associated with a project.
Expand All @@ -29,24 +31,46 @@ impl CommandSource for Project {
fn source_list<CR>(&self, _runner: &CR) -> Result<()>
where CR: CommandRunner
{
let mut table = Table::new();
table.set_format(FORMAT_BLANK);

table.add_row(row!["SERVICE", "IMAGE", "STATUS", "LOCAL", "REMOTE"]);

for source in self.sources().iter() {
println!("{:25} {}", source.alias().green(), source.context());
if source.is_available_locally(self) {

let available_locally = source.is_available_locally(self);

let status = if available_locally && source.mounted() {
"mounted"
} else {
"unmounted"
};

let local = if available_locally {
let canonical = source.path(self).canonicalize()?;
// Try to strip the prefix, but this may fail on Windows
// or if the source is in a weird location.
let path = match canonical.strip_prefix(self.root_dir()) {
Ok(stripped) => stripped.to_owned(),
Err(_) => canonical.to_owned(),
};
let mounted = if source.mounted() {
"(mounted)".normal()
} else {
"(NOT MOUNTED)".red().bold()
};
println!(" Available at {} {}", path.display(), mounted);
}
format!("{}", path.display()).to_owned()
} else {
String::from("")
};

table.add_row(row![
source.alias(),
source.image(),
status,
local,
source.context()
]);

}

table.printstd();

Ok(())
}

Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ extern crate lazy_static;
#[macro_use]
extern crate log;
extern crate phf;
#[macro_use(row)]
extern crate prettytable;
#[cfg(test)]
extern crate rand;
extern crate rayon;
Expand Down
23 changes: 20 additions & 3 deletions src/sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,25 @@ impl Sources {
/// Add a source tree to a map, keyed by its alias. Returns the alias.
fn add_source(sources: &mut BTreeMap<String, Source>,
mounted_sources: &BTreeMap<String, bool>,
context: &dc::Context)
context: &dc::Context,
image: Option<String>)
-> Result<String> {
// Figure out what alias we want to use.
let alias = context.human_alias()?;

// Get the fully qualified image name
let fq_image: String = match image {
Some(i) => { format!("{}", i) }
None => { String::from("") }
};

// Look up whether we've mounted this container or not.
let mounted = mounted_sources.get(&alias).cloned().unwrap_or(true);

// Build our Source object.
let source = Source {
alias: alias.clone(),
image: fq_image,
context: context.clone(),
mounted: mounted,
};
Expand Down Expand Up @@ -104,7 +112,8 @@ impl Sources {
for file in pod.all_files() {
for service in file.services.values() {
if let Some(context) = service.context()? {
Self::add_source(&mut sources, &mounted, context)?;
let image = format!("{}", service.image.clone().unwrap().to_string());
Self::add_source(&mut sources, &mounted, context, Some(image))?;
}
}
}
Expand All @@ -116,7 +125,7 @@ impl Sources {
let libs: BTreeMap<String, SourceConfig> = load_yaml(&path)?;
for (lib_key, lib_info) in &libs {
let context = lib_info.context.value()?;
let alias = Self::add_source(&mut sources, &mounted, context)?;
let alias = Self::add_source(&mut sources, &mounted, context, None)?;
lib_keys.insert(lib_key.clone(), alias);
}
}
Expand Down Expand Up @@ -195,6 +204,8 @@ impl<'a> Iterator for Iter<'a> {
pub struct Source {
/// A short name for this source tree.
alias: String,
/// The fully qualified image used by the services this source maps to
image: String,
/// The remote location from which we can clone this source tree, or
/// the local directory where we can find it.
context: dc::Context,
Expand All @@ -210,6 +221,11 @@ impl Source {
&self.alias
}

/// The fully qualified image used by the services this source maps to
pub fn image(&self) -> &str {
&self.image
}

/// The remote git URL from which we can clone this source tree.
pub fn context(&self) -> &dc::Context {
&self.context
Expand Down Expand Up @@ -284,6 +300,7 @@ fn are_loaded_with_projects() {
assert_eq!(hello.context(), &dc::Context::new(url));
assert_eq!(hello.path(&proj),
proj.src_dir().join("dockercloud-hello-world"));
assert_eq!(hello.image(), "dockercloud/hello-world");
}

#[test]
Expand Down