1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//! Godwit Core
//!
//! General abstraction over the operations used as an API with call
//! forward wrappers. It contains full API definitions and endpoints to
//! integrate Godwit core.
mod setup;

use crate::{
	core::setup::{setup_gw_dir, setup_init_state},
	glyph::Glyph,
	plugins,
	statehandler::{self, State},
	tui,
};
use std::{error::Error, path::PathBuf};

/// One-time Godwit setup call.
pub fn init(path: Option<PathBuf>, headless: bool, refresh: bool) -> Result<(), Box<dyn Error>> {
	setup_gw_dir(path, headless, refresh)?;
	setup_init_state()?;
	Ok(())
}

/// Add project to Godwit.
pub fn add(
	glyph: Glyph,
	location: PathBuf,
	existing: bool,
	active: bool,
	default: bool,
) -> Result<(), Box<dyn Error>> {
	if !existing {
		plugins::invoke("Weaver", None)?;
	}

	statehandler::add_state(glyph, location, None, active, default)?;
	Ok(())
}

/// Remove project from Godwit
pub fn remove(glyph: Glyph) -> Result<(), Box<dyn Error>> {
	statehandler::purge_state(glyph)?;
	Ok(())
}

/// List projects under Godwit.
pub fn list() -> Result<Vec<State>, Box<dyn Error>> {
	let states = statehandler::load_stategraph()?.get_states().to_vec();
	Ok(states)
}

/// Switch to another project under Godwit.
pub fn switch(glyph: Glyph, default: bool) -> Result<(), Box<dyn Error>> {
	statehandler::set_active(&glyph)?;

	if default {
		statehandler::set_default(&glyph)?;
	}

	Ok(())
}

/// Forward to splash TUI.
pub fn runsplash() -> Result<(), Box<dyn Error>> {
	tui::run()?;
	Ok(())
}