-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.rs
162 lines (140 loc) · 4.9 KB
/
main.rs
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use std::path::PathBuf;
use std::process::exit;
use std::str::FromStr;
use clap::Parser;
use colored::{ColoredString, Colorize};
use regex::Regex;
use golem_examples::model::{ComponentName, Example, ExampleParameters, PackageName};
use golem_examples::{Examples, GolemExamples};
#[derive(Parser, Debug)]
#[command()]
pub struct Command {
// Filter examples by name, checks if the example name contains the filter string
#[arg(short, long)]
filter: Option<String>,
// Skip running instructions
#[arg(long)]
skip_instructions: bool,
// Skip instantiating projects
#[arg(long)]
skip_instantiate: bool,
#[arg(long)]
target_path: Option<String>,
}
pub fn main() {
let command = Command::parse();
let filter = command
.filter
.as_ref()
.map(|filter| Regex::from_str(filter.as_str()).expect("failed to compile regex"));
let results: Vec<(Example, Result<(), String>)> = GolemExamples::list_all_examples()
.iter()
.filter(|example| match &filter {
Some(filter) => filter.is_match(example.name.as_string()),
None => true,
})
.map(|example| {
let result = test_example(&command, example);
if let Err(err) = &result {
println!("{}", err.bright_red())
}
(example.clone(), result)
})
.collect();
println!();
for result in &results {
println!(
"{}: {}",
result.0.name.to_string().bold(),
match &result.1 {
Ok(_) => "OK".bright_green(),
Err(err) =>
ColoredString::from(format!("{}\n{}", "Failed".bright_red(), err.red())),
}
)
}
println!();
if results.iter().any(|r| r.1.is_err()) {
exit(1)
}
}
fn test_example(command: &Command, example: &Example) -> Result<(), String> {
println!();
println!(
"{} {}",
"Generating and testing:".bold().bright_white(),
example.name.to_string().blue()
);
let target_path = PathBuf::from(
command
.target_path
.clone()
.unwrap_or_else(|| "examples-test".to_string()),
);
let component_name = ComponentName::new(example.name.as_string().to_string() + "-comp");
let package_name =
PackageName::from_string("golemx:componentx").ok_or("failed to create package name")?;
let component_path = target_path.join(component_name.as_string());
println!("Target path: {}", target_path.display().to_string().blue());
println!("Component name: {}", component_name.as_string().blue());
println!("Package name: {}", package_name.to_string().blue());
println!(
"Component path: {}",
component_path.display().to_string().blue()
);
let example_parameters = ExampleParameters {
component_name,
package_name,
target_path,
};
let run = |command: &str, args: Vec<&str>| -> Result<(), String> {
let command_formatted = format!("{} {}", command, args.join(" "));
let run_failed = |e| format!("{} failed: {}", command_formatted, e);
println!(
"Running {} in {}",
command_formatted.blue(),
component_path.display().to_string().blue()
);
let status = std::process::Command::new(command)
.args(args.clone())
.current_dir(&component_path)
.status()
.map_err(|e| run_failed(e.to_string()))?;
match status.code() {
Some(0) => Ok(()),
Some(code) => Err(run_failed(format!("non-zero exit code: {}", code))),
None => Err(run_failed("terminated".to_string())),
}
};
if command.skip_instantiate {
println!("Skipping instantiate")
} else {
println!("Instantiating");
if component_path.exists() {
println!("Deleting {}", component_path.display().to_string().blue());
std::fs::remove_dir_all(&component_path)
.map_err(|e| format!("remove dir all failed: {}", e))?;
}
let _ = GolemExamples::instantiate(example, &example_parameters)
.map_err(|e| format!("instantiate failed: {}", e))?;
println!("Successfully instantiated the example");
}
if command.skip_instructions {
println!("Skipping instructions\n");
} else {
println!("Executing instructions\n");
let instructions = GolemExamples::instructions(example, &example_parameters);
for line in instructions.lines() {
if line.starts_with(" ") {
match run("bash", vec!["-c", line]) {
Ok(_) => {}
Err(err) => return Err(err.to_string()),
}
} else {
println!("> {}", line.magenta())
}
}
println!("Successfully executed instructions\n");
}
Ok(())
}