Skip to content

Commit

Permalink
feat: simple backup command
Browse files Browse the repository at this point in the history
  • Loading branch information
pxseu committed Sep 17, 2023
1 parent 876c1db commit 68b6b70
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 11 deletions.
3 changes: 3 additions & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod whoami;
use anyhow::Result;
use clap::Subcommand;
use ignite::from_compose;
use volumes::backup;

use crate::state::State;

Expand Down Expand Up @@ -60,6 +61,7 @@ pub enum Commands {
Volumes(volumes::Options),
#[clap(alias = "compose")]
FromCompose(from_compose::Options),
Backup(backup::Options),
}

pub async fn handle_command(command: Commands, mut state: State) -> Result<()> {
Expand Down Expand Up @@ -99,6 +101,7 @@ pub async fn handle_command(command: Commands, mut state: State) -> Result<()> {
Commands::FromCompose(options) => from_compose::handle(options, state).await,
Commands::Payment(options) => payment::handle(options, state).await,
Commands::Volumes(options) => volumes::handle(options, state).await,
Commands::Backup(options) => backup::handle(options, state).await,
}
}
}
Expand Down
38 changes: 38 additions & 0 deletions src/commands/volumes/backup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use anyhow::{Context, Result};
use clap::Parser;

use super::copy::fslike::FsLike;
use crate::state::State;

#[derive(Debug, Parser)]
#[clap(about = "Backup files from a deployment to local machine")]
#[group(skip)]
pub struct Options {
#[clap(help = "Deployment name or id")]
pub source: String,
}

pub async fn handle(options: Options, state: State) -> Result<()> {
let source = FsLike::from_str(&state, &format!("{}:/", options.source)).await?;

let backup_file = dirs::download_dir()
.or(dirs::home_dir().map(|home| home.join("Downloads")))
.context("Could not find a download directory")?
.join(format!(
"hop-backup_{}_{}.tar.gz",
options.source,
chrono::Local::now().format("%Y-%m-%d_%H-%M-%S")
))
.to_string_lossy()
.to_string();

let (_, data) = source.read().await?;

tokio::fs::write(&backup_file, data)
.await
.with_context(|| format!("Could not write to {backup_file}"))?;

log::info!("Backup saved to {backup_file}");

Ok(())
}
3 changes: 1 addition & 2 deletions src/commands/volumes/copy/fslike.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use async_zip::ZipEntryBuilder;
use futures_util::io::AsyncWriteExt as _;
use ignore::WalkBuilder;
use tokio::fs;
use tokio::io::AsyncWriteExt as _;
use tokio::io::{BufReader, BufWriter};
use tokio::io::{AsyncWriteExt as _, BufReader, BufWriter};
use tokio_tar::Archive;

use super::utils::{get_files_from_volume, send_files_to_volume};
Expand Down
2 changes: 1 addition & 1 deletion src/commands/volumes/copy/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod fslike;
pub mod fslike;
mod utils;

use anyhow::{bail, Result};
Expand Down
7 changes: 3 additions & 4 deletions src/commands/volumes/mkdir.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use anyhow::Result;
use clap::Parser;

use super::utils::{parse_target_from_path_like, path_into_uri_safe};
use crate::{commands::volumes::utils::create_directory, state::State};
use super::utils::parse_target_from_path_like;
use crate::commands::volumes::utils::create_directory;
use crate::state::State;

#[derive(Debug, Parser)]
#[clap(about = "Delete files")]
Expand All @@ -28,8 +29,6 @@ pub async fn handle(options: Options, state: State) -> Result<()> {

match target {
(Some((deployment, volume)), path) => {
let path = path_into_uri_safe(&path);

create_directory(
&state.http,
&deployment.id,
Expand Down
3 changes: 3 additions & 0 deletions src/commands/volumes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod backup;
mod copy;
mod delete;
mod list;
Expand All @@ -22,6 +23,7 @@ pub enum Commands {
#[clap(name = "mv", alias = "move")]
Move(r#move::Options),
Mkdir(mkdir::Options),
Backup(backup::Options),
}

#[derive(Debug, Parser)]
Expand All @@ -41,5 +43,6 @@ pub async fn handle(options: Options, state: State) -> Result<()> {
Commands::Delete(options) => delete::handle(options, state).await,
Commands::Move(options) => r#move::handle(options, state).await,
Commands::Mkdir(options) => mkdir::handle(options, state).await,
Commands::Backup(options) => backup::handle(options, state).await,
}
}
2 changes: 2 additions & 0 deletions src/commands/volumes/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ pub struct MoveRequest {

#[derive(Debug, Serialize, Clone)]
pub struct CreateDirectory {
#[serde(rename = "name")]
pub path: String,
pub recursive: bool,
}
10 changes: 6 additions & 4 deletions src/commands/volumes/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,15 @@ pub async fn create_directory(
path: &str,
recursive: bool,
) -> Result<()> {
let path = path_into_uri_safe(path);

http.request::<()>(
"POST",
&format!("/ignite/deployments/{deployment}/volumes/{volume}/{path}"),
&format!("/ignite/deployments/{deployment}/volumes/{volume}/folder"),
Some((
serde_json::to_vec(&CreateDirectory { recursive })?.into(),
serde_json::to_vec(&CreateDirectory {
path: path.to_owned(),
recursive,
})?
.into(),
"application/json",
)),
)
Expand Down

0 comments on commit 68b6b70

Please sign in to comment.