config file is dropped to disk when installing via cargo

This commit is contained in:
epi
2021-06-15 08:00:21 -05:00
parent 9b4092ea8c
commit 329d04252f
2 changed files with 29 additions and 1 deletions

View File

@@ -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"}

View File

@@ -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");
}
}