Skip to main content

cedar_policy_cli/command/
format.rs

1/*
2 * Copyright Cedar Contributors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use 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    /// File containing the static Cedar policies and/or templates. If not provided, read policies from stdin.
29    #[arg(short, long = "policies", value_name = "FILE")]
30    pub policies_file: Option<String>,
31
32    /// Custom line width (default: 80).
33    #[arg(short, long, value_name = "UINT", default_value_t = 80)]
34    pub line_width: usize,
35
36    /// Custom indentation width (default: 2).
37    #[arg(short, long, value_name = "INT", default_value_t = 2)]
38    pub indent_width: isize,
39
40    /// Automatically write back the formatted policies to the input file.
41    #[arg(short, long, group = "action", requires = "policies_file")]
42    pub write: bool,
43
44    /// Check that the policies formats without any changes. Mutually exclusive with `write`.
45    #[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
60/// Format the policies in the given file or stdin.
61///
62/// Returns a boolean indicating whether the formatted policies are the same as the original
63/// policies.
64fn 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}