@@ -140,6 +140,12 @@ impl Cx<'_> {
140140 self.live.shared.get(key).map(String::as_str)
141141 }
142142
143+ /// A secret the user gave a widget, such as an API key, set with `set_secret`. Read it where
144+ /// you use it (a handler, or before starting a thread), not in `view`: it's a file read.
145+ pub fn secret(&self, name: &str) -> Option<String> {
146+ self.live.secret(name)
147+ }
148+
143149 /// Ask for frames at the display's rate while this keeps being called: for animation.
144150 pub fn animate(&self) {
145151 self.live.animate.set(true);
@@ -220,6 +226,11 @@ pub fn share(h: &mut HostState, key: &str, value: &str) {
220226 Host::live(h).share(key, value)
221227 }
222228
229+/// Keeps a secret, such as an API key, where build tickets never see it. Empty forgets it.
230+pub fn set_secret(h: &mut HostState, name: &str, value: &str) {
231+ Host::live(h).set_secret(name, value)
232+}
233+
223234 /// Sends one request to conveyord. The ops are in conveyor's server.rs.
224235 pub fn call(h: &mut HostState, request: String) {
225236 Host::live(h).call(request)
@@ -268,6 +279,8 @@ pub struct Live {
268279 /// Instances whose code panicked, with the message. They draw `stopped` until the next build.
269280 broken: RefCell<BTreeMap<String, String>>,
270281 base: PathBuf,
282+ /// Beside `base`, never in it: the app's state is copied into every build ticket.
283+ secrets: PathBuf,
271284 instances: HashMap<String, (String, Box<dyn View>)>,
272285 shared: BTreeMap<String, String>,
273286 wanted: RefCell<Vec<(String, String)>>,
@@ -291,6 +304,21 @@ impl Live {
291304 self.dirty = true;
292305 }
293306
307+ /// Keeps a secret where build tickets never see it, readable by the user alone. Empty forgets it.
308+ pub fn set_secret(&mut self, name: &str, value: &str) {
309+ use std::{io::Write, os::unix::fs::{DirBuilderExt, OpenOptionsExt}};
310+ let path = self.secrets.join(name.replace('/', "_"));
311+ if value.is_empty() { let _ = std::fs::remove_file(path); return; }
312+ let _ = std::fs::DirBuilder::new().recursive(true).mode(0o700).create(&self.secrets);
313+ let file = std::fs::OpenOptions::new().write(true).create(true).truncate(true).mode(0o600).open(path);
314+ let _ = file.and_then(|mut f| f.write_all(value.as_bytes()));
315+ }
316+
317+ /// A secret, as `cx.secret` reads it.
318+ pub fn secret(&self, name: &str) -> Option<String> {
319+ std::fs::read_to_string(self.secrets.join(name.replace('/', "_"))).ok().filter(|s| !s.is_empty())
320+ }
321+
294322 /// Sends one request to conveyord, as `conveyor call` does. The ops are in conveyor's
295323 /// server.rs. Results show up in `cx.data` within a second.
296324 pub fn call(&mut self, request: String) {
@@ -344,7 +372,8 @@ impl Live {
344372 }
345373
346374 impl Live {
347- /// Instances of `kinds` keep their folders under `base`, and shared values in `base/shared.tsv`.
375+ /// Instances of `kinds` keep their folders under `base`, shared values in `base/shared.tsv`,
376+ /// and secrets in `secrets` beside it.
348377 pub fn new(base: PathBuf, kinds: Kinds, about: Abouts) -> Self {
349378 let shared = std::fs::read_to_string(base.join("shared.tsv")).unwrap_or_default().lines()
350379 .filter_map(|l| l.split_once('\t').map(|(k, v)| (k.to_owned(), v.replace("\\n", "\n"))))
@@ -352,7 +381,7 @@ impl Live {
352381 // Instances that were keeping work going come back now, shown or not.
353382 let wanted: Vec<(String, String)> = std::fs::read_to_string(base.join("running.tsv")).unwrap_or_default().lines()
354383 .filter_map(|l| l.split_once('\t').map(|(id, kind)| (id.to_owned(), kind.to_owned()))).collect();
355- Live { data: Data::default(), kinds, about, broken: RefCell::new(BTreeMap::new()), waker: std::sync::Arc::new(|| {}), images: OnceCell::new(), base, instances: HashMap::new(), shared, wanted: RefCell::new(wanted),
384+ Live { data: Data::default(), kinds, about, broken: RefCell::new(BTreeMap::new()), waker: std::sync::Arc::new(|| {}), images: OnceCell::new(), secrets: base.with_file_name("secrets"), base, instances: HashMap::new(), shared, wanted: RefCell::new(wanted),
356385 animate: Cell::new(false), outbox: vec![], spare: TextEdit::new(""), dirty: false,
357386 later: RefCell::new(vec![]), running: RefCell::new(BTreeSet::new()), kept: BTreeSet::new() }
358387 }