cmd_init.v

1.3 KB · raw

1//! `koh init` — write the `.koh` file and a default ignore list.23module cli45import json26import os7import src.snapshot8import src.storage910pub fn cmd_init(raw []string) int {11	args := parse('init', raw, Options{12		flags: ['json']13	}) or { return usage_error(err.msg()) }14	root := os.getwd()15	// A subfolder of a repo is already inside history: creating a second `.koh`16	// there would nest one repo in another, and the outer repo would never see it.17	if enclosing := storage.find_root(root) {18		if enclosing != os.real_path(root) {19			return refuse(args.json(), 'already inside the koh repo at ${enclosing} — run "koh init" outside it')20		}21	}22	store := storage.create(root) or { return refuse(args.json(), err.msg()) }23	store.close()24	ignore_path := os.join_path(root, snapshot.ignore_file)25	mut wrote_ignore := false26	if !os.exists(ignore_path) {27		os.write_file(ignore_path, snapshot.default_ignore) or {28			return refuse(args.json(), err.msg())29		}30		wrote_ignore = true31	}32	if args.json() {33		emit_json(json2.encode(InitJson{34			repo:           root35			ignore_created: wrote_ignore36		}))37		return 038	}39	ok('initialised koh in ${root}')40	if wrote_ignore {41		note('wrote ${snapshot.ignore_file} — edit it to suit the project')42	}43	return 044}4546struct InitJson {47	repo           string48	ignore_created bool49}