-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add 'config show' and 'config edit' commands
- Loading branch information
Showing
10 changed files
with
171 additions
and
14 deletions.
There are no files selected for viewing
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
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,58 @@ | ||
|
||
use std::fs::OpenOptions; | ||
use std::path::PathBuf; | ||
use std::io::{BufWriter, Result as IoResult, Write}; | ||
|
||
use anyhow::{bail, Result}; | ||
use handlebars::Handlebars; | ||
use modda_lib::config::{global_conf_dir, Config, Settings}; | ||
use modda_lib::progname::PROGNAME; | ||
|
||
const CONFIG_TEMPLATE: &'static str = include_str!("config_template.yml"); | ||
|
||
pub fn edit_global_config_dir(config: &Config) -> Result<()> { | ||
let directory = global_conf_dir() | ||
.expect("Could not determine the global config location"); | ||
if !directory.exists() { | ||
println!("Global config directory doesn't exist, creating {dir}", | ||
dir = directory.as_os_str().to_string_lossy()); | ||
if let Err(error) = std::fs::create_dir_all(&directory) { | ||
bail!("Could not create global config directory {dir}\n {error}", | ||
dir = directory.as_os_str().to_string_lossy()) | ||
} | ||
} | ||
let config_path = match Settings::find_config_in_dir(&directory)? { | ||
None => { | ||
println!("Global config file doesn't exist, creating in {dir}", | ||
dir = directory.as_os_str().to_string_lossy()); | ||
create_config_yml(&directory)? | ||
} | ||
Some(path) => path, | ||
}; | ||
println!("Opening global config file {config_file_path}", | ||
config_file_path = config_path.as_os_str().to_string_lossy()); | ||
match &config.code_editor { | ||
Some(editor) => match open::with_detached(config_path, editor) { | ||
IoResult::Ok(_) => Ok(()), | ||
IoResult::Err(error) => bail!("Could not start editor program {editor}\n {error}") | ||
} | ||
None => match open::that_detached(config_path) { | ||
IoResult::Ok(_) => Ok(()), | ||
IoResult::Err(error) => bail!("Could not start system editor\n {error}"), | ||
} | ||
} | ||
} | ||
|
||
fn create_config_yml(directory: &PathBuf) -> Result<PathBuf> { | ||
let registry = Handlebars::new(); | ||
let config_content = registry.render_template(CONFIG_TEMPLATE, &())?; | ||
|
||
let config_path = directory.join(format!("{}.yml", PROGNAME)); | ||
|
||
let mut dest = match OpenOptions::new().create(true).write(true).open(&config_path) { | ||
Err(err) => bail!("Could not create config file\n {}", err), | ||
std::io::Result::Ok(file) => file, | ||
}; | ||
dest.write_all(config_content.as_bytes())?; | ||
Ok(config_path) | ||
} |
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,16 @@ | ||
|
||
use anyhow::{bail, Result}; | ||
use modda_lib::config::global_conf_dir; | ||
|
||
pub fn open_global_config_dir() -> Result<()> { | ||
let directory = global_conf_dir() | ||
.expect("Could not determine the global config location"); | ||
if !directory.exists() { | ||
if let Err(error) = std::fs::create_dir_all(&directory) { | ||
bail!("Could not create global config directory {dir}\n {error}", | ||
dir = directory.as_os_str().to_string_lossy()) | ||
} | ||
} | ||
open::that_detached(directory)?; | ||
Ok(()) | ||
} |
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,37 @@ | ||
|
||
# Modda config file | ||
# All options are optional | ||
|
||
#### Path to the location were the archive cache is. | ||
#### After an archive is downloaded it will be stored in the cache. | ||
#### Before trying to download an archive, the program will check in the archive | ||
#### if it's already there (except if told otherwise). | ||
#archive_cache: ~ | ||
|
||
#### Temporary location where the archive are extracted before being copied to the game | ||
#### directory (and then installed). | ||
#### That's kind of an expert (perf optimization) that most people will not need to care about. | ||
#extract_location: ~ | ||
|
||
#### Enter the path to your weidu executable (`weidu` on linux or macos, `weidu.exe` on windows) | ||
#### If this is not set, it will use either a weidu executable in the game directory (if any) or | ||
#### try to use it from the path | ||
#weidu_path: ~ | ||
|
||
#### If this is set, a weidu executable in the game directory will be _ignored_ and only | ||
#### one defined in `weidu_path` or on the path will be used | ||
#ignore_current_dir_weidu: ~ | ||
|
||
#### Configuration for external programs used for uncommon archive types (RAR, 7Z etc.) | ||
#extractors: | ||
# rar: | ||
# command: unrar-nonfree | ||
# args: [ "x", "${input}", "${target}" ] | ||
# 7z: | ||
# command: 7z | ||
# args: [ "x", "${input}", "-o${target}" ] | ||
|
||
#### Program used for code editing (for example vscode (`code`), notepad++ etc.) | ||
#### This is used when calling `modda config edit` | ||
#### If not set, this will let the OS decide what to open `yaml` files with. | ||
#code_editor: code |
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,4 +1,6 @@ | ||
|
||
pub mod config_edit; | ||
pub mod config_show; | ||
pub mod discover; | ||
pub mod introspect; | ||
pub mod list_components; | ||
|
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
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