From 329d04252f9bcb302bdd5508cd14ee72df75b909 Mon Sep 17 00:00:00 2001 From: epi Date: Tue, 15 Jun 2021 08:00:21 -0500 Subject: [PATCH] config file is dropped to disk when installing via cargo --- Cargo.toml | 1 + build.rs | 29 ++++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 30b4efa..f443664 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ maintenance = { status = "actively-developed" } clap = "2.33" regex = "1" lazy_static = "1.4" +dirs = "3.0" [dependencies] futures = { version = "0.3.14"} diff --git a/build.rs b/build.rs index 19d39ca..531dcab 100644 --- a/build.rs +++ b/build.rs @@ -1,6 +1,7 @@ -use std::fs::OpenOptions; +use std::fs::{copy, create_dir_all, OpenOptions}; use std::io::{Read, Seek, SeekFrom, Write}; extern crate clap; +extern crate dirs; use clap::Shell; @@ -47,4 +48,30 @@ fn main() { bash_file .write_all(contents.as_bytes()) .expect("Couldn't write updated bash completion script to disk"); + + // hunter0x8 let me know that when installing via cargo, it would be nice if we dropped a + // config file during the build process. The following code will place an example config in + // the user's configuration directory + // - linux: $XDG_CONFIG_HOME or $HOME/.config + // - macOS: $HOME/Library/Application Support + // - windows: {FOLDERID_RoamingAppData} + let mut config_dir = dirs::config_dir().expect("Couldn't resolve user's config directory"); + config_dir = config_dir.join("feroxbuster"); // $HOME/.config/feroxbuster + + if !config_dir.exists() { + // recursively create the feroxbuster directory and all of its parent components if + // they are missing + create_dir_all(&config_dir) + .expect("Couldn't create one or more directories needed to copy the config file"); + } + + // hard-coding config name here to not rely on the crate we're building, if DEFAULT_CONFIG_NAME + // ever changes, this will need to be updated + let config_file = config_dir.join("ferox-config.toml"); + + if !config_file.exists() { + // config file doesn't exist, add it to the config directory + copy("ferox-config.toml.example", config_file) + .expect("Couldn't copy example config into config directory"); + } }