feat: add cli

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2024-08-24 15:40:44 +02:00
parent 61db3da695
commit 5e88ffdbc9
4 changed files with 214 additions and 142 deletions

View File

@@ -1,7 +1,7 @@
use plan::{ClonedPlan, Plan};
use project::ProjectPlan;
use state::ValidatedState;
use cuddle_state::Cuddle;
use state::{validated_project::Project, ValidatedState};
mod cuddle_state;
mod plan;
mod project;
mod schema_validator;
@@ -12,7 +12,7 @@ async fn main() -> anyhow::Result<()> {
dotenv::dotenv().ok();
tracing_subscriber::fmt::init();
let _cuddle = Cuddle::default()
let cuddle = Cuddle::default()
.prepare_project()
.await?
.prepare_plan()
@@ -20,71 +20,76 @@ async fn main() -> anyhow::Result<()> {
.build_state()
.await?;
Cli::new(cuddle).setup().await?.execute().await?;
Ok(())
}
struct Start {}
struct PrepareProject {
project: Option<ProjectPlan>,
pub struct Cli {
cli: clap::Command,
cuddle: Cuddle<ValidatedState>,
}
struct PreparePlan {
project: Option<ProjectPlan>,
plan: Option<ClonedPlan>,
}
impl Cli {
pub fn new(cuddle: Cuddle<ValidatedState>) -> Self {
let cli = clap::Command::new("cuddle").subcommand_required(true);
struct Cuddle<S = Start> {
state: S,
}
// Cuddle maintains the context for cuddle to use
// Stage 1 figure out which state to display
// Stage 2 prepare plan
// Stage 3 validate settings, build actions, prepare
impl Cuddle {}
impl Cuddle<Start> {
pub fn default() -> Self {
Self { state: Start {} }
Self { cli, cuddle }
}
pub async fn prepare_project(&self) -> anyhow::Result<Cuddle<PrepareProject>> {
let project = ProjectPlan::from_current_path().await?;
pub async fn setup(mut self) -> anyhow::Result<Self> {
let s = self
.add_default()
.await?
.add_project_commands()
.await?
.add_plan_commands()
.await?;
Ok(Cuddle {
state: PrepareProject { project },
})
}
}
impl Cuddle<PrepareProject> {
pub async fn prepare_plan(&self) -> anyhow::Result<Cuddle<PreparePlan>> {
let plan = if let Some(project) = &self.state.project {
Plan::new().clone_from_project(project).await?
} else {
None
};
Ok(Cuddle {
state: PreparePlan {
project: self.state.project.clone(),
plan,
},
})
}
}
impl Cuddle<PreparePlan> {
pub async fn build_state(&self) -> anyhow::Result<Cuddle<ValidatedState>> {
let state = if let Some(project) = &self.state.project {
let state = state::State::new();
let raw_state = state.build_state(project, &self.state.plan).await?;
state.validate_state(&raw_state).await?
} else {
ValidatedState::default()
};
Ok(Cuddle { state })
// TODO: Add global
// TODO: Add components
Ok(s)
}
pub async fn execute(mut self) -> anyhow::Result<()> {
match self
.cli
.get_matches_from(std::env::args())
.subcommand()
.ok_or(anyhow::anyhow!("failed to find subcommand"))?
{
("do", args) => {
tracing::debug!("executing do");
}
_ => {}
}
Ok(())
}
async fn add_default(mut self) -> anyhow::Result<Self> {
self.cli = self
.cli
.subcommand(clap::Command::new("do").alias("x"))
.subcommand(clap::Command::new("get"));
Ok(self)
}
async fn add_project_commands(mut self) -> anyhow::Result<Self> {
if let Some(project) = self.cuddle.state.project.as_ref() {
// Add project level commands
}
Ok(self)
}
async fn add_plan_commands(mut self) -> anyhow::Result<Self> {
if let Some(plan) = self.cuddle.state.plan.as_ref() {
// Add plan level commands
}
Ok(self)
}
}