Skip to content

Commit

Permalink
feat: "status" command (ALPHA) (comtrya#400)
Browse files Browse the repository at this point in the history
Plan to add status of manifests
  • Loading branch information
shawn111 committed May 2, 2024
1 parent 98babba commit 6c7e183
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
38 changes: 35 additions & 3 deletions app/src/commands/apply.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use super::ComtryaCommand;
use crate::Runtime;
use clap::Parser;
use comfy_table::{Cell, ContentArrangement, Table};
use comtrya_lib::contexts::to_rhai;
use comtrya_lib::manifests::{load, Manifest};
use core::panic;
use petgraph::{visit::DfsPostOrder, Graph};
use rhai::Engine;
use std::path::PathBuf;
use std::{collections::HashMap, ops::Deref};
use tracing::{debug, error, info, instrument, span, trace, warn};

Expand All @@ -24,9 +26,8 @@ pub(crate) struct Apply {
pub label: Option<String>,
}

impl ComtryaCommand for Apply {
#[instrument(skip(self, runtime))]
fn execute(&self, runtime: &Runtime) -> anyhow::Result<()> {
impl Apply {
fn manifest_path(&self, runtime: &Runtime) -> anyhow::Result<PathBuf> {
let first_manifest_path = runtime.config.manifest_paths.first().ok_or_else(|| {
anyhow::anyhow!(
"No manifest paths found in config file, please add at least one path to your manifests"
Expand All @@ -44,9 +45,40 @@ impl ComtryaCommand for Apply {
};

trace!(manifests = self.manifests.join(",").deref(),);
Ok(manifest_path)
}

#[instrument(skip(self, runtime))]
pub fn status(&self, runtime: &Runtime) -> anyhow::Result<()> {
let contexts = &runtime.contexts;
let manifest_path = self.manifest_path(&runtime)?;

println!("Load manifests from path: {:#?}", manifest_path);

let manifests = load(manifest_path, contexts);

let mut table = Table::new();
table
.set_content_arrangement(ContentArrangement::Dynamic)
.set_width(40)
.set_header(vec!["Manifest", "Count of Actions"]);

for (name, manifest) in manifests.iter() {
table.add_row(vec![
Cell::new(format!("{name}")),
Cell::new(format!("{}", manifest.actions.len())),
]);
}
println!("{table}");
Ok(())
}
}

impl ComtryaCommand for Apply {
#[instrument(skip(self, runtime))]
fn execute(&self, runtime: &Runtime) -> anyhow::Result<()> {
let contexts = &runtime.contexts;
let manifest_path = self.manifest_path(&runtime)?;
let manifests = load(manifest_path, contexts);

// Build DAG
Expand Down
4 changes: 4 additions & 0 deletions app/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ pub enum Commands {
#[clap(aliases = &["do", "run"])]
Apply(Apply),

/// List manifests status (ALPHA)
Status(Apply),

/// Print version information
Version(Version),

Expand All @@ -43,6 +46,7 @@ impl Commands {
pub fn execute(self, runtime: &Runtime) -> anyhow::Result<()> {
match self {
Self::Apply(apply) => apply.execute(&runtime),
Self::Status(apply) => apply.status(&runtime),
Self::Version(version) => version.execute(&runtime),
Self::Contexts(contexts) => contexts.execute(&runtime),
Self::GenCompletions(gen_completions) => gen_completions.execute(&runtime),
Expand Down

0 comments on commit 6c7e183

Please sign in to comment.