Skip to content

Commit

Permalink
Add support for JSON output
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamin-bergia committed Oct 26, 2022
1 parent 341c403 commit b5d9c80
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 13 deletions.
18 changes: 15 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
[package]
name = "kree"
version = "0.1.0"
version = "0.2.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "4.0", features = ["derive"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.87"
serde_yaml = "0.9"
43 changes: 34 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use clap::Parser;
use clap::{Parser, ValueEnum};
use serde::Deserialize;
use serde_json::json;
use serde_yaml;
use std::{
fs,
Expand All @@ -11,7 +12,21 @@ use std::{
#[derive(Parser)]
#[command()]
struct Args {
/// Path to the kustomization file or directory
path: PathBuf,

/// Output format
#[arg(short, long, value_enum, default_value = "text")]
format: Option<Format>,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
enum Format {
/// One path per line
Text,

/// JSON
Json,
}


Expand Down Expand Up @@ -59,9 +74,9 @@ fn deserialize(path: PathBuf) -> Vec<Kustomization> {
}


fn run(path: PathBuf, result: Vec<PathBuf>) {
fn run(path: PathBuf, result: &mut Vec<String>) {
if let Ok(canonical) = canonical_path(path.clone()) {
println!("{}", canonical.display());
result.push(format!("{}", canonical.display()));

let resources: Vec<String> = deserialize(canonical.clone())
.iter()
Expand All @@ -76,17 +91,27 @@ fn run(path: PathBuf, result: Vec<PathBuf>) {
.to_path_buf();
next_path.push(PathBuf::from(r));

let mut branch = result.clone();

branch.push(canonical.clone());
run(next_path, branch);
run(next_path, result);
};
};
}


fn main() {
let args = Args::parse();

run(args.path, Vec::new());
let mut result = Vec::new();

run(args.path, &mut result);

match args.format {
Some(Format::Json) => {
let json = json!(result);
println!("{}", json.to_string());
},
_ => {
for r in result.iter() {
println!("{r}");
}
}
}
}

0 comments on commit b5d9c80

Please sign in to comment.