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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
mod bench;
mod build;
mod commands;
mod doctor;
mod dorkerfile;
mod errors;
mod images;
mod log;
mod mcp;
mod runtime;
mod sdk;
mod store;
mod style;
mod testcmd;
#[cfg(test)]
mod testutil;
mod units;
use anyhow::Result;
use clap::{Parser, Subcommand};
#[derive(Parser, Debug)]
#[command(
name = "dorker",
version,
about = "Docker for headless Android emulator environments"
)]
struct Cli {
/// Print what dorker decides and runs (emulator command line, boot mode, timings) to stderr.
/// Also enabled by DORKER_VERBOSE=1
#[arg(short, long, global = true)]
verbose: bool,
#[command(subcommand)]
command: Command,
}
#[derive(Subcommand, Debug)]
enum Command {
/// Check system requirements
Doctor {
/// Machine-readable output
#[arg(long)]
json: bool,
},
/// Build an image from a Dorkerfile
Build {
/// Build context directory
#[arg(default_value = ".")]
context: std::path::PathBuf,
/// Dorkerfile path (default: <context>/Dorkerfile)
#[arg(short, long)]
file: Option<std::path::PathBuf>,
/// Name and optionally tag the image, e.g. myapp:latest
#[arg(short, long)]
tag: Option<String>,
/// Rebuild even if an identical image is cached
#[arg(long)]
no_cache: bool,
/// Seconds to wait for Android to boot
#[arg(long, default_value_t = 300)]
timeout: u64,
},
/// List images
Images {
#[arg(long)]
json: bool,
},
/// Remove an image
Rmi { image: String },
/// Install a system image, e.g. `dorker pull android:35`
Pull {
/// Image to install; omit to install the `FROM` image of ./Dorkerfile
image: Option<String>,
/// Dorkerfile to read when no image is given (default: ./Dorkerfile)
#[arg(short, long)]
file: Option<std::path::PathBuf>,
},
/// Create a starter Dorkerfile in the current directory
Init,
/// Run a Model Context Protocol server on stdio, so AI agents can drive dorker
Mcp,
/// Run instrumentation tests in an instance
Test {
instance: String,
/// Instrumentation runner, e.g. com.example.test/androidx.test.runner.AndroidJUnitRunner
#[arg(long)]
runner: String,
/// Limit to a class or class#method (comma separated)
#[arg(long)]
class: Option<String>,
/// Seconds before the run is aborted
#[arg(long, default_value_t = 600)]
timeout: u64,
/// Print the result as JSON on stdout
#[arg(long)]
json: bool,
},
/// Measure boot, restore, install and resource numbers for an image
Bench {
/// android:<api> or a built image
image: String,
#[arg(long, default_value_t = 3)]
runs: u32,
/// APK to time the install of
#[arg(long)]
apk: Option<std::path::PathBuf>,
#[arg(long)]
json: bool,
#[arg(long, default_value_t = 300)]
timeout: u64,
/// GPU mode for base `android:<api>` images
#[arg(long)]
gpu: Option<String>,
/// Skip the (slow) idle CPU and memory measurement
#[arg(long)]
skip_idle: bool,
/// Package to launch and time (default: the image's `CMD launch` package)
#[arg(long)]
launch: Option<String>,
/// Also time the plain official emulator (cold, and with its own quickboot
/// snapshot) as a baseline. Base `android:<api>` images only.
#[arg(long)]
baseline: bool,
},
/// Launch an instance from an image (e.g. android:35)
Run {
image: String,
/// Instance name (default: generated)
#[arg(long)]
name: Option<String>,
/// Return once Android is ready instead of staying attached
#[arg(short, long)]
detach: bool,
/// Remove the instance when it stops
#[arg(long)]
rm: bool,
/// Guest RAM, e.g. 2GB (overriding an image's value disables its snapshot)
#[arg(long)]
memory: Option<String>,
/// Virtual CPU count (overriding an image's value disables its snapshot)
#[arg(long)]
cpus: Option<u32>,
/// Emulator console port (even, 5554..=5680); ADB uses port+1
#[arg(long)]
adb_port: Option<u16>,
/// Seconds to wait for Android to boot
#[arg(long, default_value_t = 300)]
timeout: u64,
/// Emulator GPU mode for base images: swiftshader_indirect (default), host,
/// angle_indirect, guest, off, auto. Built images fix this in their Dorkerfile.
#[arg(long)]
gpu: Option<String>,
/// Print the started instance as JSON on stdout (requires --detach)
#[arg(long)]
json: bool,
},
/// List instances
Ps {
/// Show stopped instances too
#[arg(short, long)]
all: bool,
#[arg(long)]
json: bool,
},
/// Run an Android shell command in an instance
Exec {
/// Print {exit_code, stdout, stderr} as JSON (put before the instance)
#[arg(long)]
json: bool,
instance: String,
/// Command to run, e.g. "pm list packages"
#[arg(required = true, trailing_var_arg = true, allow_hyphen_values = true)]
command: Vec<String>,
},
/// Install an APK into an instance
Install {
instance: String,
apk: std::path::PathBuf,
/// Print the result as JSON on stdout
#[arg(long)]
json: bool,
},
/// Capture a PNG screenshot (use `-` as the output to write the PNG to stdout)
Screenshot {
instance: String,
output: std::path::PathBuf,
/// Print {path, bytes} as JSON on stdout
#[arg(long)]
json: bool,
/// Seconds to wait for a non-blank frame (e.g. just after a cold boot); 0 disables
#[arg(long, default_value_t = 10)]
wait: u64,
},
/// Show Android logcat for an instance
Logs {
instance: String,
/// Stream until interrupted
#[arg(short, long)]
follow: bool,
/// Print only the last N lines and exit
#[arg(long)]
tail: Option<u32>,
},
/// Restart a stopped instance (cold boot; keeps its data)
Start {
instance: String,
/// Seconds to wait for Android to boot
#[arg(long, default_value_t = 300)]
timeout: u64,
/// Print the instance as JSON on stdout
#[arg(long)]
json: bool,
},
/// Stop a running instance
Stop {
#[arg(required = true)]
instances: Vec<String>,
},
/// Remove an instance and its data
Rm {
#[arg(required = true)]
instances: Vec<String>,
/// Stop the instance first if it is running
#[arg(short, long)]
force: bool,
},
}
fn main() {
let cli = Cli::parse();
let env_on = std::env::var_os("DORKER_VERBOSE").is_some_and(|v| !v.is_empty() && v != "0");
log::set_verbose(cli.verbose || env_on);
log::vlog!(
"dorker {} on {} {}",
env!("CARGO_PKG_VERSION"),
std::env::consts::OS,
std::env::consts::ARCH
);
match run(cli) {
Ok(code) => std::process::exit(code),
Err(e) => {
// Parse errors already carry `file:line:col` and an `error:` line.
let is_parse = e
.downcast_ref::<errors::Coded>()
.is_some_and(|c| c.inner.downcast_ref::<dorkerfile::ParseError>().is_some());
if is_parse {
eprintln!("{e:#}");
} else {
eprintln!("error: {e:#}");
}
std::process::exit(errors::code_of(&e));
}
}
}
fn run(cli: Cli) -> Result<i32> {
match cli.command {
Command::Doctor { json } => doctor::run(json),
Command::Run {
image,
name,
detach,
rm,
memory,
cpus,
adb_port,
timeout,
gpu,
json,
} => commands::run(commands::RunArgs {
image,
name,
detach,
rm,
memory,
cpus,
adb_port,
timeout,
gpu,
json,
}),
Command::Build {
context,
file,
tag,
no_cache,
timeout,
} => build::build(build::BuildArgs {
file,
tag,
context,
timeout,
no_cache,
}),
Command::Pull { image, file } => commands::pull(image.as_deref(), file.as_deref()),
Command::Init => commands::init(),
Command::Mcp => mcp::serve(),
Command::Test {
instance,
runner,
class,
timeout,
json,
} => testcmd::run(&instance, &runner, class.as_deref(), timeout, json),
Command::Bench {
image,
runs,
apk,
json,
timeout,
gpu,
skip_idle,
launch,
baseline,
} => bench::run(bench::BenchArgs {
image,
runs: runs.max(1),
apk,
json,
timeout,
gpu,
skip_idle,
launch,
baseline,
}),
Command::Images { json } => commands::images(json),
Command::Rmi { image } => commands::rmi(&image),
Command::Ps { all, json } => commands::ps(all, json),
Command::Exec {
json,
instance,
command,
} => commands::exec(&instance, &command, json),
Command::Install {
instance,
apk,
json,
} => commands::install(&instance, &apk, json),
Command::Screenshot {
instance,
output,
json,
wait,
} => commands::screenshot(&instance, &output, json, wait),
Command::Logs {
instance,
follow,
tail,
} => commands::logs(&instance, follow, tail),
Command::Start {
instance,
timeout,
json,
} => commands::start_existing(&instance, timeout, json),
Command::Stop { instances } => commands::each(&instances, commands::stop),
Command::Rm { instances, force } => commands::each(&instances, |i| commands::rm(i, force)),
}
}