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

Add ability to update git-based project #4

Open
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion src/bin/devbox/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn exec(args: &ArgMatches) -> CliResult {
let mut project = args.project()?;

if let Some(name) = args.value_of("SERVICE") {
let mut service = project.find_service(name)?;
let service = project.find_service(name)?;
let _ = service.clone_repo();
service.build()
} else {
Expand Down
3 changes: 3 additions & 0 deletions src/bin/devbox/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub fn builtins() -> Vec<App> {
logs::cli(),
new::cli(),
ps::cli(),
renew::cli(),
start::cli(),
stop::cli(),
tasks::cli(),
Expand All @@ -23,6 +24,7 @@ pub fn builtin_exec(cmd: &str) -> Option<fn(&ArgMatches) -> CliResult> {
"logs" => logs::exec,
"new" => new::exec,
"ps" => ps::exec,
"renew" => renew::exec,
"start" => start::exec,
"stop" => stop::exec,
"tasks" => tasks::exec,
Expand All @@ -38,6 +40,7 @@ pub mod doctor;
pub mod logs;
pub mod new;
pub mod ps;
pub mod renew;
pub mod start;
pub mod stop;
pub mod tasks;
Expand Down
22 changes: 22 additions & 0 deletions src/bin/devbox/commands/renew.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use prelude::*;

pub fn cli() -> App {
subcommand("renew")
.about("Re-generates an existing devbox project template from a git repository")
.arg(project())
.arg(
Arg::with_name("git")
.long("git")
.takes_value(true)
.required(true)
.help("A URL to a git repository containing configuration for this project"),
)
}

pub fn exec(args: &ArgMatches) -> CliResult {
let name = args.value_of("PROJECT")
.ok_or_else(|| format_err!("Missing project name"))?;
let repo = args.value_of("git")
.ok_or_else(|| format_err!("Missing git repository URL!"))?;
Project::update_from_git(name, repo)
}
39 changes: 39 additions & 0 deletions src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,31 @@ impl Project {
Ok(())
}

pub fn update_from_git(name: &str, git: &str) -> Result<()> {

let dir = TempDir::new("devbox")?;

let _ = Command::new("git")
.arg("clone")
.arg(git)
.arg(&dir.path())
.spawn()?
.wait();

let toml_path = &dir.path().join("config.toml");
let yaml_path = &dir.path().join("docker-compose.yml");
let toml_contents = read_file(&toml_path)?;
let yaml_contents = read_file(&yaml_path)?;

ensure_directory_exists(&devbox_dir(name)?);
create_or_overwrite_file(&toml_config_path(name)?, &toml_contents)?;
create_or_overwrite_file(&yaml_config_path(name)?, &yaml_contents)?;

dir.close()?;

Ok(())
}

pub fn find_service(&mut self, name: &str) -> Result<&mut Service> {
let service = self.services
.iter_mut()
Expand Down Expand Up @@ -255,3 +280,17 @@ fn create_file_if_not_exists(path: &Path, content: &str) -> Result<()> {

Ok(())
}

fn create_or_overwrite_file(path: &Path, content: &str) -> Result<()> {
if path.exists() {
let mut file = OpenOptions::new().write(true).truncate(true).open(path)?;

println!("Overwriting file {:?}", &path);

file.write_all(content.as_bytes())?;
} else {
create_file_if_not_exists(&path, &content)?;
}

Ok(())
}