zuzu-rust 0.4.0

Rust implementation of ZuzuScript
Documentation
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#!/usr/bin/env zuzu

from std/getopt import Getopt;
from std/io import Path, STDERR, STDOUT;
from std/proc import Env, Proc;
from std/string import join, starts_with;
from std/tui import readline;
from std/zuzuzoo import Zuzuzoo;

function _usage () {
	return join( "\n", [
		"Usage:",
		"  zuzuzoo install [options] TARGET...",
		"  zuzuzoo remove [options] TARGET...",
		"  zuzuzoo remove --dist NAME...",
		"  zuzuzoo list [options]",
		"  zuzuzoo query [options] MODULE",
		"  zuzuzoo query --dist NAME",
		"  zuzuzoo verify [options] MODULE...",
		"  zuzuzoo verify --dist NAME...",
		"  zuzuzoo latest [options] MODULE",
		"  zuzuzoo help",
		"",
		"Options:",
		"  --dry-run       show the plan without changing files",
		"  --force         continue install when packaged tests fail",
		"  --no-test       skip packaged tests during install",
		"  --global        install to the global Zuzu root",
		"  --lib-dir DIR   override the module installation directory",
		"  --bin-dir DIR   override the script installation directory",
		"  --meta-dir DIR  override the metadata directory",
		"  --cache-dir DIR cache downloaded archives in DIR",
		"  --lock-timeout SECONDS",
		"                 fail if the install/remove lock is not acquired",
		"  --quiet        suppress install/latest progress messages",
		"  --dist          resolve targets as distribution names",
		"  --json          print machine-readable JSON where supported",
		"  --yes           confirm removal without prompting",
	] ) _ "\n";
}

function _is_command ( value ) {
	return (
		value eq "install" or
		value eq "remove" or
		value eq "list" or
		value eq "query" or
		value eq "verify" or
		value eq "latest" or
		value eq "help"
	);
}

function _has_option ( options, key ) {
	return options.exists(key) and options.get(key);
}

function _tail ( items ) {
	let out := [];
	let i := 1;
	while ( i < items.length() ) {
		out.push(items[i]);
		i++;
	}
	return out;
}

function _zuzu_command () {
	let configured := Env.get("ZUZU_COMMAND");
	return configured if configured != null and configured ne "";

	let repo_zuzu := new Path("bin/zuzu");
	return repo_zuzu.absolute().to_String()
		if repo_zuzu.exists() and repo_zuzu.is_file();

	return "zuzu";
}

function _operation_options ( options ) {
	let out := {
		zuzu_command: _zuzu_command(),
	};

	out.add( "dry_run", true ) if _has_option( options, "dry-run" );
	out.add( "force", true ) if _has_option( options, "force" );
	out.add( "no_test", true ) if _has_option( options, "no-test" );
	out.add( "global", true ) if _has_option( options, "global" );
	out.add( "dist", true ) if _has_option( options, "dist" );
	out.add( "base_url", options.get("base-url") )
		if options.exists("base-url");
	out.add( "lib_dir", options.get("lib-dir") )
		if options.exists("lib-dir");
	out.add( "bin_dir", options.get("bin-dir") )
		if options.exists("bin-dir");
	out.add( "meta_dir", options.get("meta-dir") )
		if options.exists("meta-dir");
	out.add( "cache_dir", options.get("cache-dir") )
		if options.exists("cache-dir");
	out.add( "lock_timeout", options.get("lock-timeout") )
		if options.exists("lock-timeout");
	out.add( "progress", true )
		if not _has_option( options, "quiet" );

	return out;
}

function _looks_like_install_target ( target ) {
	let text := "" _ target;
	return true if starts_with( text, "http://" );
	return true if starts_with( text, "https://" );
	return true if text ~ /\.(?:tar|tar\.gz|tgz)$/;
	return true if text ~ /\//;

	let path := new Path(text);
	return path.exists();
}

function _valid_implicit_install ( targets ) {
	return false if targets.length() == 0;
	for ( let target in targets ) {
		return false if not _looks_like_install_target(target);
	}
	return true;
}

function _invalid_with_command ( command, options ) {
	if ( _has_option( options, "dist" ) ) {
		return "--dist is only valid with remove, query, and verify"
			if command ne "remove" and command ne "query" and
				command ne "verify";
	}
	if ( _has_option( options, "json" ) ) {
		return "--json is only valid with list, query, verify, and latest"
			if command ne "list" and command ne "query" and
				command ne "verify" and command ne "latest";
	}
	if ( _has_option( options, "yes" ) ) {
		return "--yes is only valid with remove"
			if command ne "remove";
	}
	return null;
}

function _print_test_output ( result ) {
	for ( let dist_result in result.get( "tests", [] ) ) {
		for ( let test_result in dist_result{tests} ) {
			STDOUT.print(test_result{stdout})
				if test_result{stdout} ne "";
			STDERR.print(test_result{stderr})
				if test_result{stderr} ne "";
		}
	}
}

function _run_install ( zoo, targets, options, raw_options ) {
	if ( targets.length() == 0 ) {
		STDERR.say("install requires at least one target");
		return 2;
	}

	options.add( "print_plan", true );
	let result := zoo.install( targets, options );
	_print_test_output(result);

	if ( not result{ok} ) {
		STDERR.say(result.get( "error", "install failed" ));
		return 1;
	}

	if ( result.get( "forced", false ) ) {
		say("Test failures ignored due to --force");
	}

	say( result{dry_run} ? "Dry run complete" : "Install complete" );
	return 0;
}

function _confirmed_remove () {
	let answer := readline(
		"Remove the planned files? [y/N] ",
		"n",
		null,
	);
	return ( "" _ answer ) ~ /^(?:y|yes)$/i;
}

function _run_remove ( zoo, targets, options, raw_options ) {
	if ( targets.length() == 0 ) {
		STDERR.say("remove requires at least one target");
		return 2;
	}

	let lock := zoo.acquire_lock( "remove", options );
	try {
		let locked_options := options;
		locked_options.set( "lock", false );
		let plan := zoo.plan_remove( targets, locked_options );
		STDOUT.print( zoo.format_remove_plan(plan) );
		if ( not plan{ok} ) {
			lock.release();
			return 1;
		}

		if ( _has_option( raw_options, "dry-run" ) ) {
			lock.release();
			say("Dry run complete");
			return 0;
		}

		if ( not _has_option( raw_options, "yes" ) ) {
			if ( not _confirmed_remove() ) {
				lock.release();
				say("Remove declined");
				return 4;
			}
		}

		let result := zoo.remove( targets, locked_options );
		if ( not result{ok} ) {
			lock.release();
			STDERR.say("remove failed");
			return 1;
		}

		lock.release();
		say("Remove complete");
		return 0;
	}
	catch ( Exception e ) {
		lock.release();
		throw e;
	}
}

function _run_list ( zoo, targets, options, raw_options ) {
	if ( targets.length() != 0 ) {
		STDERR.say("list does not accept targets");
		return 2;
	}

	let installed := zoo.list_installed(options);
	if ( _has_option( raw_options, "json" ) ) {
		say( zoo.format_json(installed) );
		return 0;
	}

	for ( let dist in installed ) {
		say(
			dist{name} _ "\t" _
			dist{version} _ "\t" _
			dist{metadata_file}
		);
	}
	return 0;
}

function _run_query ( zoo, targets, options, raw_options ) {
	if ( targets.length() != 1 ) {
		STDERR.say("query requires exactly one target");
		return 2;
	}

	let found := _has_option( raw_options, "dist" )
		? zoo.query_distribution( targets[0], options )
		: zoo.query( targets[0], options );
	if ( found == null ) {
		STDERR.say("query target is not installed");
		return 1;
	}

	say( zoo.format_json(found) );
	return 0;
}

function _run_verify ( zoo, targets, options, raw_options ) {
	if ( targets.length() == 0 ) {
		STDERR.say("verify requires at least one target");
		return 2;
	}

	let result := zoo.verify( targets, options );
	if ( _has_option( raw_options, "json" ) ) {
		say( zoo.format_json(result) );
	}
	else {
		say( result{ok} ? "Verification ok" : "Verification failed" );
		say( "Distributions checked: " _ result{distributions}.length() );
		say( "Files checked: " _ result{checked_files}.length() );
		say( "Missing files: " _ result{missing_files}.length() );
		say( "Hash mismatches: " _ result{hash_mismatches}.length() );
		say( "Errors: " _ result{errors}.length() );
		for ( let error in result{errors} ) {
			say( "  - " _ error{code} _ ": " _ error{message} );
		}
	}
	return result{ok} ? 0 : 3;
}

function _run_latest ( zoo, targets, options, raw_options ) {
	if ( targets.length() != 1 ) {
		STDERR.say("latest requires exactly one module target");
		return 2;
	}

	let result := zoo.latest( targets[0], options );
	if ( _has_option( raw_options, "json" ) ) {
		say( zoo.format_json(result) );
		return 0;
	}

	say( "Module: " _ result{module_name} );
	say( "Latest version: " _ result{remote_version} );
	say(
		"Installed version: " _
		( result{installed_version} == null
			? "not installed"
			: result{installed_version} )
	);
	say( "Status: " _ result{status} );
	return 0;
}

function _contains_version_option ( argv ) {
	for ( let arg in argv ) {
		return true if arg eq "--version";
		return true if starts_with( "" _ arg, "--version=" );
	}
	return false;
}

function _option_specs () {
	return [
		"help|h",
		"dry-run",
		"force",
		"no-test",
		"global",
		"lib-dir=s",
		"bin-dir=s",
		"meta-dir=s",
		"cache-dir=s",
		"lock-timeout=f",
		"quiet|q",
		"dist",
		"json",
		"yes|y",
		"remove=s",
		"base-url=s",
	];
}

function _merge_options ( base, extra ) {
	for ( let key in extra.keys() ) {
		base.set( key, extra.get(key) );
	}
	return base;
}

function _run_command ( argv ) {
	let effective_argv := (
		argv.length() > 0 and argv[0] eq "--"
	) ? _tail(argv) : argv;

	if ( _contains_version_option(effective_argv) ) {
		STDERR.say(
			"--version is not supported for removal; use " _
			"remove --dist NAME to remove an installed distribution"
		);
		return 2;
	}

	let parsed := Getopt.parse(
		effective_argv,
		_option_specs(),
	);
	if ( not parsed{ok} ) {
		STDERR.say(parsed{error});
		STDERR.print(_usage());
		return 2;
	}

	let options := parsed{options};
	let rest := parsed{argv};

	if ( _has_option( options, "help" ) ) {
		STDOUT.print(_usage());
		return 0;
	}
	if ( rest.length() > 0 and rest[0] eq "help" ) {
		STDOUT.print(_usage());
		return 0;
	}

	let command := "";
	let targets := [];
	if ( options.exists("remove") ) {
		if ( rest.length() != 0 ) {
			STDERR.say("--remove does not accept positional targets");
			return 2;
		}
		command := "remove";
		targets := [ options.get("remove") ];
		options.add( "dist", true );
		STDERR.say("--remove=NAME is deprecated; use remove --dist NAME");
	}
	else if ( rest.length() == 0 ) {
		STDERR.print(_usage());
		return 2;
	}
	else if ( _is_command(rest[0]) ) {
		command := rest[0];
		let command_parsed := Getopt.parse(
			_tail(rest),
			_option_specs(),
		);
		if ( not command_parsed{ok} ) {
			STDERR.say(command_parsed{error});
			STDERR.print(_usage());
			return 2;
		}
		_merge_options( options, command_parsed{options} );
		targets := command_parsed{argv};
	}
	else if ( _valid_implicit_install(rest) ) {
		command := "install";
		targets := rest;
	}
	else {
		STDERR.say("Unknown command: " _ rest[0]);
		STDERR.print(_usage());
		return 2;
	}

	let invalid := _invalid_with_command( command, options );
	if ( invalid != null ) {
		STDERR.say(invalid);
		return 2;
	}

	let operation_options := _operation_options(options);
	let zoo := new Zuzuzoo(
		lib_dir: operation_options.get( "lib_dir", null ),
		bin_dir: operation_options.get( "bin_dir", null ),
		meta_dir: operation_options.get( "meta_dir", null ),
		global: operation_options.get( "global", false ),
		zuzu_command: operation_options.get( "zuzu_command", "zuzu" ),
	);

	if ( command eq "install" ) {
		return _run_install( zoo, targets, operation_options, options );
	}
	if ( command eq "remove" ) {
		return _run_remove( zoo, targets, operation_options, options );
	}
	if ( command eq "list" ) {
		return _run_list( zoo, targets, operation_options, options );
	}
	if ( command eq "query" ) {
		return _run_query( zoo, targets, operation_options, options );
	}
	if ( command eq "verify" ) {
		return _run_verify( zoo, targets, operation_options, options );
	}
	if ( command eq "latest" ) {
		return _run_latest( zoo, targets, operation_options, options );
	}

	STDERR.say("Unknown command: " _ command);
	return 2;
}

function __main__ ( argv ) {
	try {
		Proc.exit(_run_command(argv));
	}
	catch ( Exception e ) {
		STDERR.say( "zuzuzoo: " _ e{message} );
		Proc.exit(1);
	};
}