pub(crate) fn edit(
    entry: &RagenixRule,
    identity_paths: &[String],
    editor: &str,
    mut writer: impl Write,
) -> Result<()> {
    let dir = tempfile::tempdir()?;
    fs::set_permissions(&dir, PermissionsExt::from_mode(0o700))?;

    let input_path = dir.path().join("input");
    let output_path = &entry.path;

    if !output_path.exists() || util::is_stdin(editor) {
        // If the target file does not yet exist, we don't have to decrypt the result for editing.
        // Likewise, if we're reading from stdin, we're going to replace the target file completely.
        fs::File::create(&input_path)?;
        editor_hook(&input_path, editor)?;
    } else {
        // If the file already exists, first decrypt it, hash it, open it in `editor`,
        // hash the result, and if the hashes are equal, return.
        let identities = age::get_identities(identity_paths)?;
        age::decrypt(output_path, &input_path, &identities)?;

        // Calculate hash before editing
        let pre_edit_hash = util::sha256(&input_path)?;

        // Prompt user to edit file
        editor_hook(&input_path, editor)?;

        // Calculate hash after editing
        let post_edit_hash = util::sha256(&input_path)?;

        // Return if the file wasn't changed when editing
        if pre_edit_hash == post_edit_hash {
            writeln!(
                writer,
                "{} wasn't changed, skipping re-encryption.",
                output_path.display()
            )?;
            return Ok(());
        }
    }

    age::encrypt(input_path, output_path.clone(), &entry.public_keys)?;

    Ok(())
}
/// Edit/create an age-encrypted file
///
/// If the file doesn't exist yet, a new file is created and opened in `editor`.
Edit/create an age-encrypted file
If the file doesn't exist yet, a new file is created and opened in `editor`.
Edit / create an age - encrypted file If the file doesn ' t exist yet a new file is created and opened in ` editor ` .
pub(crate) fn edit(
    entry: &RagenixRule,
    identity_paths: &[String],
    editor: &str,
    mut writer: impl Write,
) -> Result<()> {
    let dir = tempfile::tempdir()?;
    fs::set_permissions(&dir, PermissionsExt::from_mode(0o700))?;
    let input_path = dir.path().join("input");
    let output_path = &entry.path;
    if !output_path.exists() || util::is_stdin(editor) {
        // If the target file does not yet exist, we don't have to decrypt the result for editing.
        // Likewise, if we're reading from stdin, we're going to replace the target file completely.
        fs::File::create(&input_path)?;
        editor_hook(&input_path, editor)?;
    } else {
        // If the file already exists, first decrypt it, hash it, open it in `editor`,
        // hash the result, and if the hashes are equal, return.
        let identities = age::get_identities(identity_paths)?;
        age::decrypt(output_path, &input_path, &identities)?;
        // Calculate hash before editing
        let pre_edit_hash = util::sha256(&input_path)?;
        // Prompt user to edit file
        editor_hook(&input_path, editor)?;
        // Calculate hash after editing
        let post_edit_hash = util::sha256(&input_path)?;
        // Return if the file wasn't changed when editing
        if pre_edit_hash == post_edit_hash {
            writeln!(
                writer,
                "{} wasn't changed, skipping re-encryption.",
                output_path.display()
            )?;
            return Ok(());
        }
    }
    age::encrypt(input_path, output_path.clone(), &entry.public_keys)?;
    Ok(())
}
pub ( crate ) fn edit ( entry : & RagenixRule , identity_paths : & [ String ] , editor : & str , mut writer : impl Write , ) -> Result < ( ) > { let dir = tempfile :: tempdir ( ) ? ; fs :: set_permissions ( & dir , PermissionsExt :: from_mode ( 0o700 ) ) ? ; let input_path = dir . path ( ) . join ( " " ) ; let output_path = & entry . path ; if ! output_path . exists ( ) || util :: is_stdin ( editor ) { // If the target file does not yet exist, we don't have to decrypt the result for editing. // Likewise, if we're reading from stdin, we're going to replace the target file completely. fs :: File :: create ( & input_path ) ? ; editor_hook ( & input_path , editor ) ? ; } else { // If the file already exists, first decrypt it, hash it, open it in `editor`, // hash the result, and if the hashes are equal, return. let identities = age :: get_identities ( identity_paths ) ? ; age :: decrypt ( output_path , & input_path , & identities ) ? ; // Calculate hash before editing let pre_edit_hash = util :: sha256 ( & input_path ) ? ; // Prompt user to edit file editor_hook ( & input_path , editor ) ? ; // Calculate hash after editing let post_edit_hash = util :: sha256 ( & input_path ) ? ; // Return if the file wasn't changed when editing if pre_edit_hash == post_edit_hash { writeln ! ( writer " " output_path display ( ) ) ? ; return Ok ( ( ) ) ; } } age :: encrypt ( input_path , output_path . clone ( ) , & entry . public_keys ) ? ; Ok ( ( ) ) }
Edit/create an age-encrypted file
If the file doesn't exist yet, a new file is created and opened in `editor`.
Edit / create an age - encrypted file If the file doesn ' t exist yet a new file is created and opened in ` editor ` .
entry
&RagenixRule
identity_paths
&[String]
editor
&str
writer
impl Write