Skip to content

Commit b5d9c80

Browse files
Add support for JSON output
1 parent 341c403 commit b5d9c80

File tree

3 files changed

+51
-13
lines changed

3 files changed

+51
-13
lines changed

Diff for: Cargo.lock

+15-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
[package]
22
name = "kree"
3-
version = "0.1.0"
3+
version = "0.2.0"
44
edition = "2021"
55

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

88
[dependencies]
99
clap = { version = "4.0", features = ["derive"] }
1010
serde = { version = "1.0", features = ["derive"] }
11+
serde_json = "1.0.87"
1112
serde_yaml = "0.9"

Diff for: src/main.rs

+34-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use clap::Parser;
1+
use clap::{Parser, ValueEnum};
22
use serde::Deserialize;
3+
use serde_json::json;
34
use serde_yaml;
45
use std::{
56
fs,
@@ -11,7 +12,21 @@ use std::{
1112
#[derive(Parser)]
1213
#[command()]
1314
struct Args {
15+
/// Path to the kustomization file or directory
1416
path: PathBuf,
17+
18+
/// Output format
19+
#[arg(short, long, value_enum, default_value = "text")]
20+
format: Option<Format>,
21+
}
22+
23+
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
24+
enum Format {
25+
/// One path per line
26+
Text,
27+
28+
/// JSON
29+
Json,
1530
}
1631

1732

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

6176

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

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

79-
let mut branch = result.clone();
80-
81-
branch.push(canonical.clone());
82-
run(next_path, branch);
94+
run(next_path, result);
8395
};
8496
};
8597
}
8698

8799

88100
fn main() {
89101
let args = Args::parse();
90-
91-
run(args.path, Vec::new());
102+
let mut result = Vec::new();
103+
104+
run(args.path, &mut result);
105+
106+
match args.format {
107+
Some(Format::Json) => {
108+
let json = json!(result);
109+
println!("{}", json.to_string());
110+
},
111+
_ => {
112+
for r in result.iter() {
113+
println!("{r}");
114+
}
115+
}
116+
}
92117
}

0 commit comments

Comments
 (0)