cedar_policy_cli/command/
format.rs1use std::fs::OpenOptions;
18use std::io::Write;
19
20use cedar_policy_formatter::{policies_str_to_pretty, Config};
21use clap::Args;
22use miette::{IntoDiagnostic, Result, WrapErr};
23
24use crate::{read_from_file_or_stdin, CedarExitCode};
25
26#[derive(Args, Debug)]
27pub struct FormatArgs {
28 #[arg(short, long = "policies", value_name = "FILE")]
30 pub policies_file: Option<String>,
31
32 #[arg(short, long, value_name = "UINT", default_value_t = 80)]
34 pub line_width: usize,
35
36 #[arg(short, long, value_name = "INT", default_value_t = 2)]
38 pub indent_width: isize,
39
40 #[arg(short, long, group = "action", requires = "policies_file")]
42 pub write: bool,
43
44 #[arg(short, long, group = "action")]
46 pub check: bool,
47}
48
49pub fn format_policies(args: &FormatArgs) -> CedarExitCode {
50 match format_policies_inner(args) {
51 Ok(false) if args.check => CedarExitCode::Failure,
52 Err(err) => {
53 println!("{err:?}");
54 CedarExitCode::Failure
55 }
56 _ => CedarExitCode::Success,
57 }
58}
59
60fn format_policies_inner(args: &FormatArgs) -> Result<bool> {
65 let policies_str = read_from_file_or_stdin(args.policies_file.as_ref(), "policy set")?;
66 let config = Config {
67 line_width: args.line_width,
68 indent_width: args.indent_width,
69 };
70 let formatted_policy = policies_str_to_pretty(&policies_str, &config)?;
71 let are_policies_equivalent = policies_str == formatted_policy;
72
73 match &args.policies_file {
74 Some(policies_file) if args.write => {
75 let mut file = OpenOptions::new()
76 .write(true)
77 .truncate(true)
78 .open(policies_file)
79 .into_diagnostic()
80 .wrap_err(format!("failed to open {policies_file} for writing"))?;
81 file.write_all(formatted_policy.as_bytes())
82 .into_diagnostic()
83 .wrap_err(format!(
84 "failed to write formatted policies to {policies_file}"
85 ))?;
86 }
87 _ => print!("{formatted_policy}"),
88 }
89 Ok(are_policies_equivalent)
90}