mirror of
https://codeberg.org/icewind/secretfile.git
synced 2026-06-03 16:44:08 +02:00
parent
9f4da54d03
commit
2dd223c249
2 changed files with 26 additions and 1 deletions
25
README.md
Normal file
25
README.md
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
# Secretfile
|
||||||
|
|
||||||
|
A small library for helping with loading secrets from files including systemd
|
||||||
|
service credentials support.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
Load a value from a file
|
||||||
|
|
||||||
|
```rust
|
||||||
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
let secret = secretfile::load("/path/to/supper/secret/file")?;
|
||||||
|
println!("{secret}");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
If the provided path includes the `$CREDENTIALS_DIRECTORY` placeholder, it will
|
||||||
|
be replaced with the systemd service credential directory. Any trailing
|
||||||
|
whitespace will be stripped from the returned secret.
|
||||||
|
|
||||||
|
## Why make this a library?
|
||||||
|
|
||||||
|
While the code is simple enough, I found myself copy-pasting it enough times
|
||||||
|
that it seemed useful to put into a library.
|
||||||
|
|
@ -30,7 +30,7 @@ impl Error for SecretError {}
|
||||||
/// If the provided path includes the `$CREDENTIALS_DIRECTORY` placeholder, it will be replaced with the
|
/// If the provided path includes the `$CREDENTIALS_DIRECTORY` placeholder, it will be replaced with the
|
||||||
/// systemd service credential directory.
|
/// systemd service credential directory.
|
||||||
///
|
///
|
||||||
/// any leading whitespace will be stripped from the returned secret.
|
/// any trailing whitespace will be stripped from the returned secret.
|
||||||
pub fn load(path: &str) -> Result<String, SecretError> {
|
pub fn load(path: &str) -> Result<String, SecretError> {
|
||||||
let file = if path.contains("$CREDENTIALS_DIRECTORY") {
|
let file = if path.contains("$CREDENTIALS_DIRECTORY") {
|
||||||
let dir = var("CREDENTIALS_DIRECTORY")
|
let dir = var("CREDENTIALS_DIRECTORY")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue