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

[hab] Add hab pkg export subcommand #692

Merged
merged 1 commit into from
Jun 9, 2016
Merged
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
12 changes: 10 additions & 2 deletions components/hab/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,16 @@ pub fn get() -> App<'static, 'static> {
(aliases: &["p", "pa", "pat"])
(@arg PKG_IDENT: +required +takes_value
"A package identifier (ex: core/redis, core/busybox-static/1.42.2)")
)
)
)
(@subcommand export =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would this make more sense as a hab artifact subcommand? ping @fnichol

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we decided that users would get confused by pkg versus artifact so actually, hab artifact upload should get changed to hab pkg upload

(about: "Exports the package to the specified format")
(aliases: &["e", "ex", "exp"])
(@arg FORMAT: +required +takes_value
"The export format (ex: docker, aci)")
(@arg PKG_IDENT: +required +takes_value
"A package identifier (ex: core/redis, core/busybox-static/1.42.2)")
)
)
(@subcommand ring =>
(about: "Commands relating to Habitat rings")
(aliases: &["r", "ri", "rin"])
Expand Down
111 changes: 111 additions & 0 deletions components/hab/src/command/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,114 @@ pub mod path {
Ok(())
}
}

pub mod export {
use error::Result;
use hcore::package::PackageIdent;

pub struct ExportFormat {
pkg_ident: PackageIdent,
cmd: String
}

impl ExportFormat {
pub fn pkg_ident(&self) -> &PackageIdent {
&self.pkg_ident
}

pub fn cmd(&self) -> &str {
&self.cmd
}
}

pub fn start(ident: &PackageIdent, format: &ExportFormat) -> Result<()> {
inner::start(ident, format)
}

pub fn format_for(value: &str) -> Result<ExportFormat> {
inner::format_for(value)
}

#[cfg(target_os = "linux")]
mod inner {
use command::pkg::exec;
use common::command::package::install;
use error::{Error, Result};
use hcore::crypto::default_cache_key_path;
use hcore::fs::{cache_artifact_path, FS_ROOT_PATH};
use hcore::package::{PackageIdent, PackageInstall, Identifiable};
use hcore::url::default_depot_url;
use std::ffi::OsString;
use std::path::Path;
use std::str::FromStr;
use super::ExportFormat;

pub fn format_for(value: &str) -> Result<ExportFormat> {
match value {
"docker" => {
let format = ExportFormat{
pkg_ident: try!(PackageIdent::from_str("core/hab-pkg-dockerize")),
cmd: "hab-pkg-dockerize".to_string()
};
Ok(format)
},
"aci" => {
let format = ExportFormat{
pkg_ident: try!(PackageIdent::from_str("core/hab-pkg-aci")),
cmd: "hab-pkg-aci".to_string()
};
Ok(format)
}
_ => Err(Error::UnsupportedExportFormat(value.to_string()))
}
}

pub fn start(ident: &PackageIdent, format: &ExportFormat) -> Result<()> {
let format_ident = format.pkg_ident();
match PackageInstall::load(format.pkg_ident(), None) {
Ok(_) => {},
_ => {
println!("{} is not installed", &format_ident.to_string());
println!("Searching for {} in remote {}",
&format_ident.to_string(), &default_depot_url());
try!(install::from_url(&default_depot_url(),
format_ident,
Path::new(FS_ROOT_PATH),
&cache_artifact_path(None),
&default_cache_key_path(None)));
}
}
let pkg_arg = OsString::from(&ident.to_string());
exec::start(&format_ident, &format.cmd(), vec![pkg_arg])
}
}

#[cfg(not(target_os = "linux"))]
mod inner {
use ansi_term::Colour::Yellow;
use error::{Error, Result};
use hcore::package::PackageIdent;
use std::env;
use super::ExportFormat;

pub fn format_for(value: &str) -> Result<ExportFormat> {
let msg = format!("∅ Exporting {} packages from this operating system is not yet \
supported. Try running this command again on a 64-bit Linux \
operating system.\n", value);
println!("{}", Yellow.bold().paint(msg));
let e = Error::UnsupportedExportFormat(value.to_string());
Err(e)
}

pub fn start(_ident: &PackageIdent, _format: &ExportFormat) -> Result<()> {
let subcmd = env::args().nth(1).unwrap_or("<unknown>".to_string());
let subsubcmd = env::args().nth(2).unwrap_or("<unknown>".to_string());
let msg = format!("∅ Exporting packages from this operating system is not yet \
supported. Try running this command again on a 64-bit Linux \
operating system.\n");
println!("{}", Yellow.bold().paint(msg));
Err(Error::SubcommandNotSupported(format!("{} {}", subcmd, subsubcmd)))

}
}
}
5 changes: 4 additions & 1 deletion components/hab/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub enum Error {
PackageArchiveMalformed(String),
PathPrefixError(path::StripPrefixError),
SubcommandNotSupported(String),
UnsupportedExportFormat(String)
}

impl fmt::Display for Error {
Expand Down Expand Up @@ -59,7 +60,8 @@ impl fmt::Display for Error {
Error::PathPrefixError(ref err) => format!("{}", err),
Error::SubcommandNotSupported(ref e) => {
format!("Subcommand `{}' not supported on this operating system", e)
}
},
Error::UnsupportedExportFormat(ref e) => format!("Unsupported export format: {}", e)
};
write!(f, "{}", msg)
}
Expand All @@ -84,6 +86,7 @@ impl error::Error for Error {
}
Error::PathPrefixError(ref err) => err.description(),
Error::SubcommandNotSupported(_) => "Subcommand not supported on this operating system",
Error::UnsupportedExportFormat(_) => "Unsupported export format"
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions components/hab/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ extern crate habitat_core as hcore;
extern crate habitat_common as common;
extern crate habitat_depot_client as depot_client;
extern crate habitat_http_client as http_client;

extern crate ansi_term;
#[macro_use]
extern crate clap;
Expand Down Expand Up @@ -142,6 +143,7 @@ fn start() -> Result<()> {
("exec", Some(m)) => try!(sub_pkg_exec(m, remaining_args)),
("install", Some(m)) => try!(sub_pkg_install(m)),
("path", Some(m)) => try!(sub_pkg_path(m)),
("export", Some(m)) => try!(sub_pkg_export(m)),
_ => unreachable!(),
}
}
Expand Down Expand Up @@ -437,6 +439,13 @@ fn sub_pkg_path(m: &ArgMatches) -> Result<()> {
command::pkg::path::start(&ident, &fs_root_path)
}

fn sub_pkg_export(m: &ArgMatches) -> Result<()> {
let ident = try!(PackageIdent::from_str(m.value_of("PKG_IDENT").unwrap()));
let format = &m.value_of("FORMAT").unwrap();
let export_fmt = try!(command::pkg::export::format_for(&format));
command::pkg::export::start(&ident, &export_fmt)
}

fn sub_ring_key_export(m: &ArgMatches) -> Result<()> {
let fs_root = henv::var(FS_ROOT_ENVVAR).unwrap_or(FS_ROOT_PATH.to_string());
let fs_root_path = Some(Path::new(&fs_root));
Expand Down