1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use std::fs::{File, read_dir};
use std::path::Path;
use std::io::BufReader;
use std::io::prelude::*;

const SECRET_PATH: &'static str = "/run/secrets";

pub fn get_list() -> Result<Vec<String>, &'static str> {
    let path = Path::new(SECRET_PATH);
    match read_dir(path) {
        Ok(dir) => {
            let list: Vec<String> = dir
                .filter(|entry| match entry { Ok(_) => true, Err(_) => false })
                .map(|entry| entry.unwrap().file_name().into_string().unwrap_or_default())
                .collect();

            Ok(list)
        },
        Err(_) => {
            Err("Read secrets path is failed")
        }
    }
}

pub fn get(secret_name: &str) -> Result<String, &str> {
    match File::open(format!("{}/{}", SECRET_PATH, secret_name)) {
        Ok(file) => {
            let mut buf_reader = BufReader::new(file);
            let mut contents = String::new();
            
            if let Err(_) = buf_reader.read_to_string(&mut contents) {
               return Err("Read secrets is failed")
            }

            Ok(contents.trim().to_string())
        },
        Err(_) => {
            Err("Read secrets path is failed")
        }
    }
}