mkit_cli/commands/
init.rs1use std::io::Write;
4use std::path::Path;
5
6use clap::Parser;
7use mkit_core::refs;
8use mkit_core::store::{ObjectStore, StoreError};
9
10use crate::clap_shim;
11use crate::exit;
12
13#[derive(Debug, Parser)]
14#[command(
15 name = "mkit init",
16 about = "Create a new mkit repository in the current directory."
17)]
18struct InitOpts {}
19
20#[must_use]
21pub fn run(args: &[String]) -> u8 {
22 if let Err(code) = clap_shim::parse::<InitOpts>("mkit init", args) {
23 return code;
24 }
25 let cwd = match std::env::current_dir() {
26 Ok(p) => p,
27 Err(e) => return emit_err(&format!("cannot read cwd: {e}"), exit::NOINPUT),
28 };
29 let layout = match super::resolve_layout(&cwd) {
30 Ok(layout) => layout,
31 Err(code) => return code,
32 };
33 match ObjectStore::init(&layout) {
34 Ok(_) => {}
35 Err(StoreError::AlreadyInitialized) => {
36 return emit_err("already a mkit repository", exit::GENERAL_ERROR);
37 }
38 Err(e) => return emit_err(&format!("init failed: {e}"), exit::CANTCREAT),
39 }
40 if let Err(e) = refs::init(&layout) {
42 return emit_err(&format!("refs init failed: {e}"), exit::CANTCREAT);
43 }
44 let mut stderr = std::io::stderr().lock();
45 let _ = writeln!(
46 stderr,
47 "initialized empty mkit repository in {}/.mkit/",
48 display(&cwd)
49 );
50 exit::OK
51}
52
53fn display(p: &Path) -> String {
54 p.display().to_string()
55}
56
57use super::error as emit_err;