cmd_peek.v

1.7 KB · raw

1//! `koh peek` — put a save's tree in a temporary folder and print where.2//!3//! The working tree and history are untouched: a peek is a read of the object4//! store into a scratch folder the user can look at, then throw away.56module cli78import json9import os10import src.fmt11import src.history12import src.snapshot1314pub fn cmd_peek(raw []string) int {15	args := parse('peek', raw, Options{16		flags: ['json']17	}) or { return usage_error(err.msg()) }18	store := open_store() or { return refuse(args.json(), err.msg()) }19	defer {20		store.close()21	}22	id := args.positional_at(0)23	if id == '' {24		return usage_error('peek needs a save ID')25	}26	save := history.resolve(store, id) or { return refuse(args.json(), err.msg()) }27	files := history.files_of(store, save.id) or { return refuse(args.json(), err.msg()) }28	path := peek_path(save.id)29	// Re-peeking the same save replaces its folder rather than merging into it.30	if os.exists(path) {31		os.rmdir_all(path) or { return refuse(args.json(), err.msg()) }32	}33	os.mkdir_all(path, os.MkdirParams{}) or { return refuse(args.json(), err.msg()) }34	snapshot.restore_files(store, path, snapshot.Ignore{}, files, []) or {35		return refuse(args.json(), err.msg())36	}37	if args.json() {38		emit_json(json.encode(PeekJson{39			id:    save.id40			path:  path41			files: files.len42		}))43		return 044	}45	ok('peeked [${save.id}] into')46	println(path)47	note('${fmt.plural(files.len, 'file')} — delete the folder when you are done')48	return 049}5051// peek_path is where a save's scratch copy lives. `doctor` sweeps these folders52// out of the temp directory.53pub fn peek_path(save_id string) string {54	return os.join_path(os.temp_dir(), '${peek_prefix}${save_id}')55}5657pub const peek_prefix = 'koh-peek-'5859struct PeekJson {60	id    string61	path  string62	files int63}