cmd_save.v

2.1 KB · raw

1//! `koh save` and `koh checkpoint`.23module cli45import json6import src.history7import src.snapshot89pub fn cmd_save(raw []string, is_checkpoint bool) int {10	verb := if is_checkpoint { 'checkpoint' } else { 'save' }11	args := parse(verb, raw, Options{12		values: ['agent', 'session']13		flags:  ['json']14	}) or { return usage_error(err.msg()) }15	message := args.positional_at(0)16	if message == '' {17		example := if is_checkpoint {18			'before combine replay engine'19		} else {20			'fix off-by-one in chunk boundary'21		}22		return usage_error('${verb} needs a message, e.g. koh ${verb} "${example}"')23	}24	store := open_store() or { return refuse(args.json(), err.msg()) }25	defer {26		store.close()27	}28	save := history.commit_worktree(store, store.root, snapshot.load(store.root), history.SaveInput{29		message:       message30		agent:         args.value('agent')31		session:       args.value('session')32		is_checkpoint: is_checkpoint33	}) or { return refuse(args.json(), err.msg()) }3435	files := history.files_of(store, save.id) or { []snapshot.FileEntry{} }36	if args.json() {37		emit_json(json.encode(SaveOut{38			id:            save.id39			parent:        optional(save.parent)40			message:       save.message41			created_at:    save.created_at42			machine:       save.machine43			is_checkpoint: save.is_checkpoint44			agent:         optional(save.agent)45			session:       optional(save.session)46			files:         files.len47			size_bytes:    save.size_bytes48		}))49		return 050	}51	ok('saved [${save.id}]  ${save.message}')52	mut detail := []string{}53	if save.is_checkpoint {54		detail << 'checkpoint'55	}56	detail << stamp(save.created_at)57	detail << plural(files.len, 'file')58	detail << human_bytes(save.size_bytes)59	note(detail.join(' · '))60	mut attribution := []string{}61	if save.agent != '' {62		attribution << 'agent: ${save.agent}'63	}64	if save.session != '' {65		attribution << 'session: ${save.session}'66	}67	if attribution.len > 0 {68		note(attribution.join(' · '))69	}70	return 071}7273struct SaveOut {74	id            string75	parent        ?string76	message       string77	created_at    i6478	machine       string79	is_checkpoint bool80	agent         ?string81	session       ?string82	files         int83	size_bytes    i6484}