-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ac61ee9
commit 2bdd82e
Showing
14 changed files
with
256 additions
and
232 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
aeee |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,76 @@ | ||
use std::{collections::HashMap, fs, path::PathBuf}; | ||
use std::{fs, path::PathBuf}; | ||
|
||
use anyhow::{anyhow, bail, Context, Result}; | ||
use clap::{arg, value_parser, ArgMatches, Command}; | ||
use clap::{arg, ArgMatches, Command}; | ||
use console::style; | ||
use dialoguer::{theme::ColorfulTheme, Confirm}; | ||
use glob::glob; | ||
use pathdiff::diff_paths; | ||
|
||
use crate::{bootstrapper::BootstrapContext, model::Server}; | ||
use crate::model::Server; | ||
|
||
pub fn cli() -> Command { | ||
Command::new("pull") | ||
.about("Pull a config file from server/ to config/") | ||
.about("Pull files from server/ to config/") | ||
.arg( | ||
arg!(<file> "File to pull") | ||
.value_parser(value_parser!(PathBuf)) | ||
arg!(<file> "Files to pull") | ||
.required(true), | ||
) | ||
} | ||
|
||
pub fn run(matches: &ArgMatches) -> Result<()> { | ||
let server = Server::load().context("Failed to load server.toml")?; | ||
|
||
let path = matches.get_one::<PathBuf>("file").unwrap(); | ||
let cannon = fs::canonicalize(path).context("Resolving absolute path of file")?; | ||
let diff = | ||
diff_paths(&cannon, fs::canonicalize(&server.path)?).ok_or(anyhow!("Cannot diff paths"))?; | ||
let files = matches.get_one::<String>("file").unwrap(); | ||
|
||
if !diff.starts_with("server") { | ||
bail!("You aren't inside server/"); | ||
} | ||
for entry in glob(files)? { | ||
let entry = entry?; | ||
|
||
// i got lazy ok | ||
let diff = | ||
diff_paths(&entry, fs::canonicalize(&server.path)?).ok_or(anyhow!("Cannot diff paths"))?; | ||
|
||
let cx = BootstrapContext { | ||
output_dir: server.path.join("config"), | ||
vars: HashMap::new(), | ||
}; | ||
if !diff.starts_with("server") { | ||
bail!("You aren't inside server/"); | ||
} | ||
|
||
let destination = cx.get_output_path(&diff); | ||
let mut destination = PathBuf::new(); | ||
let mut iter = diff.components(); | ||
iter.next().expect("Path to have atleast 1 component"); | ||
destination.push(&server.path); | ||
destination.push("config"); | ||
destination.extend(iter); | ||
|
||
fs::create_dir_all(destination.parent().unwrap()).context("Failed to create dirs")?; | ||
|
||
fs::create_dir_all(destination.parent().unwrap()).context("Failed to create dirs")?; | ||
|
||
if destination.exists() | ||
if destination.exists() | ||
&& !Confirm::with_theme(&ColorfulTheme::default()) | ||
.with_prompt(format!( | ||
"File '{}' already exists, overwrite?", | ||
destination.to_string_lossy() | ||
destination.display() | ||
)) | ||
.default(false) | ||
.interact()? | ||
{ | ||
return Ok(()); | ||
} | ||
{ | ||
continue; | ||
} | ||
|
||
fs::copy(&cannon, &destination)?; | ||
fs::copy(&entry, &destination)?; | ||
|
||
println!( | ||
" {} => {}", | ||
style(&diff.to_string_lossy()).dim(), | ||
style( | ||
diff_paths( | ||
fs::canonicalize(&destination)?, | ||
fs::canonicalize(&server.path)? | ||
println!( | ||
" {} => {}", | ||
style(&diff.to_string_lossy()).dim(), | ||
style( | ||
diff_paths( | ||
fs::canonicalize(&destination)?, | ||
fs::canonicalize(&server.path)? | ||
) | ||
.unwrap_or_default() | ||
.to_string_lossy() | ||
) | ||
.unwrap_or_default() | ||
.to_string_lossy() | ||
) | ||
.dim(), | ||
); | ||
.dim(), | ||
); | ||
} | ||
|
||
Ok(()) | ||
} |
Oops, something went wrong.