Skip to content

Commit

Permalink
add init subcommand to export the default configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
foxfriends committed Nov 1, 2023
1 parent ebec41d commit 8b03bd3
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
31 changes: 31 additions & 0 deletions syncat/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,37 @@ fn config_exists<P: AsRef<Path>>(file: P) -> bool {
.any(|res| res.unwrap().path().unwrap().as_ref() == file.as_ref())
}

pub(crate) fn dump_config<P: AsRef<Path>>(file: P) -> crate::Result<()> {
let mut config_reader = DEFAULT_CONFIG;
for mut entry in tar::Archive::new(&mut config_reader)
.entries()
.unwrap()
.map(|entry| entry.unwrap())
{
let input_path = entry.path().unwrap();
let output_path = file.as_ref().join(input_path);
let parent_path = output_path.parent().unwrap();
if !parent_path.exists() {
fs::create_dir(parent_path).map_err(|er| {
crate::Error::new("failed to unpack directory")
.with_source(er)
.with_path(parent_path)
})?;
}
let mut file = fs::File::create(&output_path).map_err(|er| {
crate::Error::new("failed to create file")
.with_source(er)
.with_path(&output_path)
})?;
io::copy(&mut entry, &mut file).map_err(|er| {
crate::Error::new("failed to write file")
.with_source(er)
.with_path(&output_path)
})?;
}
Ok(())
}

pub(crate) fn read_to_string<P: AsRef<Path>>(file: P) -> crate::Result<String> {
let path = config().join(&file);
if path.exists() {
Expand Down
8 changes: 8 additions & 0 deletions syncat/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ pub struct Opts {

#[derive(Parser, Debug)]
enum Subcommand {
/// Initialize the config directory by filling it with the default configuration.
///
/// If the config directory already exists, it will not be created. An alternative path
/// may be specified.
Init {
#[arg(short, long)]
out: Option<PathBuf>,
},
/// Installs all languages listed in the `languages.toml` file. Previously installed packages
/// will be updated, if updates are available. This process may take a long time, depending on
/// how many languages are being installed.
Expand Down
21 changes: 19 additions & 2 deletions syncat/src/package_manager.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::config::stylesheet_existence;
use crate::dirs::libraries;
use crate::config::{dump_config, stylesheet_existence};
use crate::dirs::{config, libraries};
use crate::language::{Lang, LangMap};
use crate::Subcommand;
use cc::Build;
Expand Down Expand Up @@ -156,6 +156,23 @@ pub(crate) fn main(opts: &Subcommand) -> crate::Result<()> {
}

match opts {
Subcommand::Init { out } => {
let out = out.clone().unwrap_or(config());
if out.exists() {
eprintln!(
"syncat: {}: configuration directory already exists, will not overwrite",
out.display()
);
std::process::exit(1);
}
fs::create_dir_all(&out).map_err(|er| {
crate::Error::new("failed to create config directory")
.with_source(er)
.with_path(&out)
.with_hint("ensure you have adequate permissions to create the new directory and try again")
})?;
dump_config(&out)?;
}
Subcommand::Install { languages } => {
let mut any_errors = false;
if languages.is_empty() {
Expand Down

0 comments on commit 8b03bd3

Please sign in to comment.