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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
#![forbid(unsafe_code)]
#![feature(gethostname)]
use crate::commands::{ Command, CommandMap, Mode };
use crate::error::*;
use std::io::Write;
use std::process;
use ::clap::Parser;
mod commands;
mod error;
#[derive(Parser, Debug)]
struct CommandLineParameters {
flake_path: String,
#[arg(long = "store")]
nix_store_path: Option<String>,
}
struct TopLevelMode {
flake_path: String,
configuration_name: String,
command_map: Box<CommandMap<TopLevelMode>>,
}
impl TopLevelMode {
fn new(flake_path: String, configuration_name: String) -> Result<Self> {
let mut command_map = Box::new(CommandMap::new());
command_map.insert(Command::new("help", |mode, _params| {
println!("list List all defined secrets.");
println!("");
println!("help Print this message.");
println!("quit Exit the program.");
Ok(Some(mode))
}))?;
command_map.insert(Command::new("quit", |_mode, _params| {
Ok(None)
}))?;
command_map.insert(Command::<TopLevelMode>::new("list", |mode, _params| {
let _secrets = read_secret_metadata(&mode.flake_path,
&mode.configuration_name)?;
// TODO do something with them
Ok(Some(mode))
}))?;
Ok(TopLevelMode { flake_path, configuration_name, command_map })
}
}
impl Mode for TopLevelMode {
fn prompt_text(&self) -> Result<&str> {
Ok("secrets>")
}
fn dispatch(self: Box<Self>, params: &[&str])
-> Result<Option<Box<dyn Mode>>>
{
if params.len() > 0 {
if let Some(command) = self.command_map.get(params[0]) {
(command.implementation)(self, ¶ms[1..])
} else {
println!("No command named {}. Type help if you need help.",
params[0]);
Ok(Some(self))
}
} else {
default_empty_command()?;
Ok(Some(self))
}
}
}
fn default_empty_command() -> Result<()> {
println!("No command? Type help if you need help.");
Ok(())
}
fn main() -> () {
match main_h() {
Ok(()) => { },
Err(error) => {
println!("{}", error.to_string());
},
}
}
fn main_h() -> Result<()> {
let options = CommandLineParameters::try_parse()?;
println!("{:?}", options);
let hostname: String = std::net::hostname()?
.into_string().map_err(|_| Error::UTF8)?;
let mut current_mode: Option<Box<dyn Mode>>
= Some(Box::new(TopLevelMode::new(options.flake_path.clone(),
hostname)?));
while let Some(mode) = current_mode {
if let Some(input) = prompt(mode.prompt_text()?)? {
let words: Vec<&str> = input.split_whitespace().collect();
current_mode = mode.dispatch(&words[..])?;
} else {
current_mode = None;
}
}
Ok(())
}
fn prompt(raw: &str) -> Result<Option<String>> {
let mut stdout = std::io::stdout();
stdout.write_all(format!("\n{} ", raw).as_bytes())?;
stdout.flush()?;
Ok(std::io::stdin().lines().next().transpose()?)
}
fn read_secret_metadata(flake_path: &str, configuration_name: &str)
-> Result<()>
{
let mut nix_expression = String::new();
nix_expression.push_str("let config = (builtins.getFlake \"");
nix_expression.push_str(flake_path);
nix_expression.push_str("\")");
nix_expression.push_str(".nixosConfigurations.");
nix_expression.push_str(configuration_name);
nix_expression.push_str(".config; in ");
nix_expression.push_str("if config ? \"secrets\" ");
nix_expression.push_str("then config.secrets.export ");
nix_expression.push_str("else { }");
let nix_output = process::Command::new("nix")
.env_clear()
.args([
"--extra-experimental-features",
"nix-command",
"eval",
"--impure",
"--json",
"--expr",
&nix_expression,
])
.output()?;
if !nix_output.status.success() {
println!("{}", String::from_utf8(nix_output.stderr)?);
return Err(Error::Internal("nix subprocess failed".to_string()));
}
let nix_json = String::from_utf8(nix_output.stdout)?;
println!("{}", nix_json);
// TODO parse the JSON
Ok(())
}
|