cmd_peek.v

2.0 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 json29import 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{ mode: 0o700 }) or { return refuse(args.json(), err.msg()) }34	// A peek is a copy of the repo; the root stays private so nothing below it35	// is readable by other users on a shared machine.36	os.chmod(path, 0o700) or { return refuse(args.json(), err.msg()) }37	snapshot.restore_files(store, path, snapshot.Ignore{}, files, []) or {38		return refuse(args.json(), err.msg())39	}40	if args.json() {41		emit_json(json2.encode(PeekJson{42			id:    save.id43			path:  path44			files: files.len45		}))46		return 047	}48	ok('peeked [${save.id}] into')49	println(path)50	note('${fmt.plural(files.len, 'file')} — delete the folder when you are done')51	return 052}5354// peek_path is where a save's scratch copy lives. `doctor` sweeps these folders55// out of the temp directory.56pub fn peek_path(save_id string) string {57	return os.join_path(os.temp_dir(), '${peek_prefix}${save_id}')58}5960pub const peek_prefix = 'koh-peek-'6162struct PeekJson {63	id    string64	path  string65	files int66}