-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
78 lines (76 loc) · 2.54 KB
/
main.rs
File metadata and controls
78 lines (76 loc) · 2.54 KB
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
use clap::Parser;
use dialoguer::{console::Term, theme::ColorfulTheme, Select};
use std::{
fs::{self, canonicalize},
process::Command,
};
#[derive(Parser)]
struct Cli {
path: std::path::PathBuf,
project_type: Option<String>,
}
fn main() {
let docker_files_source = canonicalize("./Dockerfiles/")
.expect("could not read file")
.into_os_string()
.into_string()
.unwrap();
let args = Cli::parse();
let path = std::fs::canonicalize(args.path)
.expect("could not read file")
.into_os_string()
.into_string()
.unwrap();
let file_names_collection = [
["Dockerfile", "Dockerfile"],
["docker-compose.yml", "docker-compose.yml"],
];
let items = fs::read_dir(docker_files_source.clone())
.unwrap()
.filter_map(|entry| entry.ok())
.filter(|entry| entry.path().is_dir())
.map(|entry| {
entry
.path()
.file_name()
.unwrap()
.to_string_lossy()
.into_owned()
})
.collect::<Vec<_>>();
let mut project_type: String = "".to_owned();
if args.project_type.is_some() && items.contains(&args.project_type.as_ref().unwrap()) {
project_type = args.project_type.as_ref().unwrap().to_string();
} else {
let selection = Select::with_theme(&ColorfulTheme::default())
.with_prompt("What type is your project?")
.items(&items)
.default(0)
.interact_on_opt(&Term::stderr());
match selection {
Ok(Some(index)) => {
project_type = items[index].clone();
}
Ok(None) => println!("User did not select anything"),
Err(_) => todo!(),
}
}
for file_names in file_names_collection {
let origin_file_path =
format!("{}/{}/{}", docker_files_source, project_type, file_names[1]);
let destiny_path = format!("{}/{}", path, file_names[1]);
std::fs::copy(origin_file_path, destiny_path).ok();
}
let mut child = Command::new("docker-compose")
.arg("-f")
.arg(format!("{}/{}", path, "docker-compose.yml"))
.arg("up")
.spawn()
.expect("failed to execute docker-compose");
let status = child.wait().expect("failed to wait for child process");
if status.success() {
println!("Docker Compose command executed successfully");
} else {
eprintln!("Docker Compose command failed with error code {}", status);
}
}