//! `koh init` — write the `.koh` file and a default ignore list. module cli import json2 import os import src.snapshot import src.storage pub fn cmd_init(raw []string) int { args := parse('init', raw, Options{ flags: ['json'] }) or { return usage_error(err.msg()) } root := os.getwd() // A subfolder of a repo is already inside history: creating a second `.koh` // there would nest one repo in another, and the outer repo would never see it. if enclosing := storage.find_root(root) { if enclosing != os.real_path(root) { return refuse(args.json(), 'already inside the koh repo at ${enclosing} — run "koh init" outside it') } } store := storage.create(root) or { return refuse(args.json(), err.msg()) } store.close() ignore_path := os.join_path(root, snapshot.ignore_file) mut wrote_ignore := false if !os.exists(ignore_path) { os.write_file(ignore_path, snapshot.default_ignore) or { return refuse(args.json(), err.msg()) } wrote_ignore = true } if args.json() { emit_json(json2.encode(InitJson{ repo: root ignore_created: wrote_ignore })) return 0 } ok('initialised koh in ${root}') if wrote_ignore { note('wrote ${snapshot.ignore_file} — edit it to suit the project') } return 0 } struct InitJson { repo string ignore_created bool }