diff options
| author | Ellie Huxtable <ellie@elliehuxtable.com> | 2024-04-25 07:52:23 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-25 07:52:23 +0100 |
| commit | d020c815c121f7f28cfcf1419f94109851fdc422 (patch) | |
| tree | 8b9476849f35b9614352132c9cc0835662f3d90f /crates/atuin-dotfiles/src/shell.rs | |
| parent | fix(ci): release workflow (#1978) (diff) | |
| download | atuin-d020c815c121f7f28cfcf1419f94109851fdc422.zip | |
feat(dotfiles): support syncing shell/env vars (#1977)
There's a bunch of duplication here!
I'd also like to support syncing shell "snippets", aka just bits of
shell config that don't fit into the structure here. Potentially special
handling for PATH too.
Rather than come up with some abstraction in the beginning, which
inevitably will not fit future uses, I'm duplicating code _for now_.
Once all the functionality is there, I can tidy things up and sort a
proper abstraction out.
Something in atuin-client for map/list style synced structures would
probably work best.
Diffstat (limited to 'crates/atuin-dotfiles/src/shell.rs')
| -rw-r--r-- | crates/atuin-dotfiles/src/shell.rs | 65 |
1 files changed, 62 insertions, 3 deletions
diff --git a/crates/atuin-dotfiles/src/shell.rs b/crates/atuin-dotfiles/src/shell.rs index a5cb0b7a..d4cacf8f 100644 --- a/crates/atuin-dotfiles/src/shell.rs +++ b/crates/atuin-dotfiles/src/shell.rs @@ -1,4 +1,5 @@ -use eyre::Result; +use eyre::{ensure, eyre, Result}; +use rmp::{decode, encode}; use serde::Serialize; use atuin_common::shell::{Shell, ShellError}; @@ -16,6 +17,64 @@ pub struct Alias { pub value: String, } +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] +pub struct Var { + pub name: String, + pub value: String, + + // False? This is a _shell var_ + // True? This is an _env var_ + pub export: bool, +} + +impl Var { + /// Serialize into the given vec + /// This is intended to be called by the store + pub fn serialize(&self, output: &mut Vec<u8>) -> Result<()> { + encode::write_array_len(output, 3)?; // 3 fields + + encode::write_str(output, self.name.as_str())?; + encode::write_str(output, self.value.as_str())?; + encode::write_bool(output, self.export)?; + + Ok(()) + } + + pub fn deserialize(bytes: &mut decode::Bytes) -> Result<Self> { + fn error_report<E: std::fmt::Debug>(err: E) -> eyre::Report { + eyre!("{err:?}") + } + + let nfields = decode::read_array_len(bytes).map_err(error_report)?; + + ensure!( + nfields == 3, + "too many entries in v0 dotfiles env create record, got {}, expected {}", + nfields, + 3 + ); + + let bytes = bytes.remaining_slice(); + + let (key, bytes) = decode::read_str_from_slice(bytes).map_err(error_report)?; + let (value, bytes) = decode::read_str_from_slice(bytes).map_err(error_report)?; + + let mut bytes = decode::Bytes::new(bytes); + let export = decode::read_bool(&mut bytes).map_err(error_report)?; + + ensure!( + bytes.remaining_slice().is_empty(), + "trailing bytes in encoded dotfiles env record, malformed" + ); + + Ok(Var { + name: key.to_owned(), + value: value.to_owned(), + export, + }) + } +} + pub fn parse_alias(line: &str) -> Option<Alias> { // consider the fact we might be importing a fish alias // 'alias' output @@ -158,14 +217,14 @@ mod tests { | inevitably two kinds of slaves: the | | prisoners of addiction and the | \\ prisoners of envy. / - ------------------------------------- + ------------------------------------- \\ ^__^ \\ (oo)\\_______ (__)\\ )\\/\\ ||----w | || || emacs='TERM=xterm-24bits emacs -nw --foo=bar' -k=kubectl +k=kubectl "; let aliases: Vec<Alias> = shell.lines().filter_map(parse_alias).collect(); |
