Skip to content

Commit

Permalink
Add the command line option --config-defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
getreu committed Nov 16, 2023
1 parent 3f03677 commit 031501f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 26 deletions.
2 changes: 1 addition & 1 deletion tests/functional/run-tests
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mkdir -p "$PWD"

# Prepare toml
# Generate new config file.
cargo run -- -V -b -c "$PWD/tpnote.toml"
cargo run -- -C "$PWD/tpnote.toml"
# Remove all `date` lines of all templates.
TP_NOTE_TEST_TOML="$PWD/tpnote-tmp.toml"
grep -v -e "key='fm_date'" "$PWD/tpnote.toml" > "$TP_NOTE_TEST_TOML"
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/run-tests2
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mkdir -p "$PWD"

# Prepare toml
# Generate new config file.
cargo run -- -V -b -c "$PWD/tpnote.toml" >/dev/null 2>&1
cargo run -- -C "$PWD/tpnote.toml"
# Remove all `date` lines of all templates.
TP_NOTE_TEST_TOML="$PWD/tpnote-tmp.toml"
grep -v -e "key='fm_date'" "$PWD/tpnote.toml" > "$TP_NOTE_TEST_TOML"
Expand Down
4 changes: 2 additions & 2 deletions tpnote/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ pub enum ConfigFileError {
/// Remedy: check the path and permissions of the to be generated
/// configuration file.
#[error(
"Unable write a configuration file with default values:\n\
"Can not write the default configuration:\n\
---\n\
{error}"
)]
ConfigFileWrite { error: String },

/// Remedy: restart, or check file permission of the configuration file.
#[error(
"Unable to load or parse the (merged)\n\
"Can not load or parse the (merged)\n\
configuration file(s):\n\
---\n\
{error}\n\n\
Expand Down
34 changes: 17 additions & 17 deletions tpnote/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,24 +154,24 @@ fn main() {
};
};

// Process `arg = `--config`.
// The output is YAML formatted for further automatic processing.
if ARGS.batch && ARGS.version && ARGS.debug.is_none() {
if let Some(path) = &ARGS.config {
let path = Path::new(&path);
// Process `arg = `--default-config`.
if let Some(path) = &ARGS.config_defaults {
let path = Path::new(&path);

if let Err(e) = Cfg::write_default_to_file(path) {
log::error!(
"{}",
ConfigFileError::ConfigFileWrite {
error: e.to_string()
}
.to_string()
);
AppLogger::flush();
process::exit(5);
};
}
if let Err(e) = Cfg::write_default_to_file(path) {
log::error!(
"{}",
ConfigFileError::ConfigFileWrite {
error: e.to_string()
}
.to_string()
);
AppLogger::flush();
process::exit(5);
};
// Exit.
AppLogger::flush();
process::exit(0);
}

// Process `arg = `--version`.
Expand Down
14 changes: 9 additions & 5 deletions tpnote/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,14 @@ pub struct Args {
/// Batch mode: does not launch editor or viewer
#[structopt(long, short = "b")]
pub batch: bool,
/// Loads an additional configuration file or writes default
/// Loads (and merges) an additional configuration file
#[structopt(long, short = "c")]
pub config: Option<String>,
/// Console debug level: "trace", "debug", "info", "warn", "error" (default) or "off"
/// Dumps the internal default configuration into a file
#[structopt(long, short = "C")]
pub config_defaults: Option<String>,
/// Console debug level: "trace", "debug", "info", "warn", "error"
/// (default) or "off"
#[structopt(long, short = "d")]
pub debug: Option<LevelFilter>,
/// Shows console debug messages also as popup windows
Expand All @@ -75,7 +79,7 @@ pub struct Args {
/// Launches only the editor, no browser
#[structopt(long, short = "e")]
pub edit: bool,
/// Scheme for new notes: "default", "zettel", (see config. for more)
/// Scheme for new notes: "default", "zettel", (cf. `--config-defaults`)
#[structopt(long, short = "s")]
pub scheme: Option<String>,
/// Forces console mode: opens console editor, no browser
Expand All @@ -87,8 +91,8 @@ pub struct Args {
/// Disables filename synchronization
#[structopt(long, short = "n")]
pub no_filename_sync: bool,
/// Disables the automatic language detection and uses `<force-lang>` instead;
/// or, if `` use `TPNOTE_LANG` or `LANG`
/// Disables the automatic language detection and uses `<force-lang>`
/// instead; or, if `` use `TPNOTE_LANG` or `LANG`
#[structopt(long, short = "l")]
pub force_lang: Option<String>,
/// Launches only the browser, no editor
Expand Down

0 comments on commit 031501f

Please sign in to comment.