Files
NLnetLabs-dnst/src/util.rs
T
617b623ba3 Add keygen command. (#9)
* Keygen skeleton.

* [keygen] Implement the basic features

* [keygen] synchronize files before exiting

* [keygen] Add help documentation

* [keygen] Generate '.ds' files for KSKs

* [keygen] Use 'display_as_bind()'

* [keygen] Add support for symlinks (Unix only)

* [keygen] Improve errors and support '.ds' symlinks

* Implement ldns-specific parsing for 'keygen'

* [keygen] Implement '-v' with version info

* [keygen] Add 'cfg(unix)' in ldns-parsing

* [keygen] Correctly handle duplicate options in ldns parsing

* Revert "[keygen] Implement '-v' with version info"

This reverts commit 643ab86bd4.

See: <https://github.com/NLnetLabs/dnst/pull/9#discussion_r1843460496>

* [keygen] Integrate the use of 'Env'

* [workflows/ci] Add OpenSSL installation steps

* [workflows/ci] Integrate OpenSSL for 'minimal_versions'

* [keygen] Improve the 'dnst' interface

* [keygen] Satisfy clippy

* [keygen] Simplify symlink CLI

* Add basic filesystem operations to 'Env'

* [keygen] Use symlink ops provided by 'Env'

* [keygen] Add basic tests for argument parsing

* [keygen] Add tests

* [keygen] Satisfy 'minimal-versions'

* [keygen] Satisfy clippy

* [keygen] Add Windows-specific missing branch

* [keygen] Document parsing for 'symlink'

* [keygen] Report error on '-r'

* [env] Refactor util fns into a 'util' module

* [keygen::symlink] Mark params as used, for Windows

* [keygen] Fix double error message

See: <https://github.com/NLnetLabs/dnst/pull/9#discussion_r1862213985>

* [keygen] Use 'Args::Report'

* [keygen] Allow invalid HTML in docs for Clap

* Use 'domain'-style imports

* [keygen] Use uppercase for expected clap values

* [keygen] use lowercase value names in Clap

---------

Co-authored-by: arya dradjica <arya@nlnetlabs.nl>
2024-12-02 11:20:21 +01:00

76 lines
2.0 KiB
Rust

//! A utility module for common operations.
use std::fs::File;
use std::path::Path;
use crate::env::Env;
use crate::error::Result;
/// Create and open a file.
pub fn create_new_file(env: &impl Env, path: impl AsRef<Path>) -> Result<File> {
let path = path.as_ref();
let abs_path = env.in_cwd(&path);
File::create_new(abs_path)
.map_err(|err| format!("cannot create '{}': {err}", path.display()).into())
}
/// Rename a file.
pub fn rename_path(env: &impl Env, old: impl AsRef<Path>, new: impl AsRef<Path>) -> Result<()> {
let (old, new) = (old.as_ref(), new.as_ref());
let abs_old = env.in_cwd(&old);
let abs_new = env.in_cwd(&new);
std::fs::rename(abs_old, abs_new).map_err(|err| {
format!(
"could not move '{}' to '{}': {err}",
old.display(),
new.display()
)
.into()
})
}
/// Create a symlink.
#[cfg(unix)]
pub fn symlink(env: &impl Env, target: impl AsRef<Path>, link: impl AsRef<Path>) -> Result<()> {
let (target, link) = (target.as_ref(), link.as_ref());
let target_path = env.in_cwd(&target);
let link_path = env.in_cwd(&link);
std::os::unix::fs::symlink(target_path, link_path).map_err(|err| {
format!(
"could not create symlink '{}' to '{}': {err}",
link.display(),
target.display(),
)
.into()
})
}
/// Create a symlink, overwriting if it already exists.
#[cfg(unix)]
pub fn symlink_force(
env: &impl Env,
target: impl AsRef<Path>,
link: impl AsRef<Path>,
) -> Result<()> {
use crate::error::in_context;
let (target, link) = (target.as_ref(), link.as_ref());
let mut temp = link.to_path_buf();
temp.as_mut_os_string().push(".new");
in_context(
|| {
format!(
"creating symlink '{}' to '{}'",
link.display(),
target.display()
)
},
|| {
symlink(env, target, &temp)?;
rename_path(env, &temp, link)?;
Ok(())
},
)
}