Skip to content

Commit

Permalink
Merge pull request #15 from graelo/archive/format
Browse files Browse the repository at this point in the history
Change archive metadata format to json
  • Loading branch information
graelo committed Sep 21, 2022
2 parents 57ced1c + ccad25b commit 2a3a53f
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 65 deletions.
92 changes: 42 additions & 50 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ tempfile = "3"
tar = "0.4.38"
zstd = "0.11.2"
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.9"
serde_json = "1"
chrono = "0.4.20"

[build-dependencies]
Expand Down
5 changes: 3 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
## Global

- [x] use `thiserror` in the library
- [ ] cleanup error types, for instance `UnexpectedOutput`
- [x] cleanup error types, for instance `UnexpectedOutput`
- [ ] go over functions such as windows_related_to which return copies, and make
them return references instead
- [x] check clap config file support
- [ ] use the strategy option only in save and catalog commands
- [ ] use the `dirs` crate for XDG-related directories
- [ ] use `config-rs` crate for the upcoming config file

## Related to save

Expand All @@ -17,7 +19,6 @@

- [x] if in $TMUX, replace the existing session named `0` and switch to client
else display a message `tmux attach -t last-session-name`
- [ ] add `restore --attach` to automatically attach if running from the terminal
- [ ] add `restore --override` to replace each existing session by its version from
the archive
- [ ] add `restore --skip-last-lines n` to not restore the last n lines of each
Expand Down
19 changes: 13 additions & 6 deletions src/actions/restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,24 @@ pub async fn restore<P: AsRef<Path>>(backup_filepath: P) -> Result<v1::Overview>
"Attach to your last session with `tmux attach -t {}`",
&metadata.client.session_name
);
} else if tmux::server::kill_session("0").await.is_err() {
let message = "

// Return an overview of the archived tmux environment, which is identical, in principle,
// with the new one. We cannot do more because the client metadata cannot be fetched.
Ok(metadata.overview())
} else {
if tmux::server::kill_session("0").await.is_err() {
let message = "
Unusual start conditions:
- you started from outside tmux but no existing session named `0` was found
- check the state of your session
";
return Err(Error::ConfigError(message.to_string()));
}
return Err(Error::ConfigError(message.to_string()));
}

let metadata = v1::Metadata::new().await?;
Ok(metadata.overview())
// Return an overview of the restored tmux environment.
let metadata = v1::Metadata::new().await?;
Ok(metadata.overview())
}
}

/// Association between a pane from the backup with a new target pane id.
Expand Down
6 changes: 3 additions & 3 deletions src/actions/save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub async fn save<P: AsRef<Path>>(backup_dirpath: P) -> Result<(PathBuf, v1::Ove
// Prepare the temp directory.
let temp_dir = TempDir::new()?;

// Save sessions & windows into `metadata.yaml` in the temp folder.
// Save sessions & windows into `metadata.json` in the temp folder.
let metadata_task: task::JoinHandle<Result<(PathBuf, PathBuf, u16, u16)>> = {
let temp_dirpath = temp_dir.path().to_path_buf();

Expand All @@ -32,10 +32,10 @@ pub async fn save<P: AsRef<Path>>(backup_dirpath: P) -> Result<(PathBuf, v1::Ove

let metadata = v1::Metadata::new().await?;

let yaml = serde_yaml::to_string(&metadata)?;
let json = serde_json::to_string(&metadata)?;

let temp_metadata_filepath = temp_dirpath.join(v1::METADATA_FILENAME);
fs::write(temp_metadata_filepath.as_path(), yaml).await?;
fs::write(temp_metadata_filepath.as_path(), json).await?;

Ok((
temp_version_filepath,
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub enum Error {
Serde {
#[from]
/// Source error,
source: serde_yaml::Error,
source: serde_json::Error,
},

/// Some IO error.
Expand Down
4 changes: 2 additions & 2 deletions src/management/archive/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub const PANES_DIR_NAME: &str = "panes-content";
/// Name of the file storing the metadata in the backup.
///
/// This name is also used in the temporary directory when storing the catalog.
pub const METADATA_FILENAME: &str = "metadata.yaml";
pub const METADATA_FILENAME: &str = "metadata.json";

/// Describes the Tmux sessions, windows & panes stored in a backup.
///
Expand Down Expand Up @@ -106,7 +106,7 @@ impl Metadata {
)));
}

let metadata = serde_yaml::from_slice(&bytes)?;
let metadata = serde_json::from_slice(&bytes)?;

Ok(metadata)
}
Expand Down

0 comments on commit 2a3a53f

Please sign in to comment.