Skip to content

Commit

Permalink
feat(config): support creating config via --write-config (#605)
Browse files Browse the repository at this point in the history
* feat(config): support creating config via `--write-config`

* refactor(cli): remove short argument for --write-config

* docs(cli): mention the new argument

---------

Co-authored-by: Raphael Amorim <[email protected]>
  • Loading branch information
orhun and raphamorim authored Aug 22, 2024
1 parent 6b7a68a commit 8b245f8
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 44 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

## Unreleased

- Support writing the config to a custom/default location via `--write-config` (Ref: #605)
- Fix scale update on transitioning between screens with different DPI.
- Support a short variant (`-w`) for `--working-dir` argument

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Usage: rio [OPTIONS]
Options:
-e, --command <COMMAND>... Command and args to execute (must be last argument)
-w, --working-dir <WORKING_DIR> Start the shell in the specified working directory
--write-config [<PATH>] Writes the config to a given path or the default location
-h, --help Print help
-V, --version Print version
```
Expand Down
5 changes: 5 additions & 0 deletions frontends/rioterm/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use clap::{Args, Parser, ValueHint};
use rio_backend::config::Shell;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

#[derive(Parser, Default, Debug)]
#[clap(author, about, version)]
Expand All @@ -29,6 +30,10 @@ pub struct TerminalOptions {
/// Start the shell in the specified working directory.
#[clap(short, long, value_hint = ValueHint::FilePath)]
pub working_dir: Option<String>,

/// Writes the config to a given path or the default location.
#[clap(long, value_name = "PATH", value_hint = ValueHint::FilePath)]
pub write_config: Option<Option<PathBuf>>,
}

impl TerminalOptions {
Expand Down
7 changes: 7 additions & 0 deletions frontends/rioterm/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Load command line options.
let args = cli::Cli::parse();

let write_config_path = args.window_options.terminal_options.write_config.clone();
if write_config_path.is_some() {
let _ = setup_logs_by_filter_level("TRACE");
rio_backend::config::create_config_file(write_config_path.unwrap());
return Ok(());
}

let (mut config, config_error) = match rio_backend::config::Config::try_load() {
Ok(config) => (config, None),
Err(err) => (rio_backend::config::Config::default(), Some(err)),
Expand Down
44 changes: 1 addition & 43 deletions frontends/rioterm/src/router/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ use rio_backend::config::Config as RioConfig;
use rio_backend::error::{RioError, RioErrorLevel, RioErrorType};
use std::collections::HashMap;
use std::error::Error;
use std::fs::File;
use std::io::Write;
use winit::event_loop::{ActiveEventLoop, EventLoop};
use winit::keyboard::{Key, NamedKey};
#[cfg(not(any(target_os = "macos", windows)))]
Expand Down Expand Up @@ -129,53 +127,13 @@ impl Route {
}

if self.path == RoutePath::Welcome && is_enter {
self.create_config_file();
rio_backend::config::create_config_file(None);
self.path = RoutePath::Terminal;
return true;
}

true
}

#[inline]
pub fn create_config_file(&self) {
let default_file_path = rio_backend::config::config_file_path();
if default_file_path.exists() {
return;
}

let default_dir_path = rio_backend::config::config_dir_path();
match std::fs::create_dir_all(&default_dir_path) {
Ok(_) => {
log::info!("configuration path created {}", default_dir_path.display());
}
Err(err_message) => {
log::error!("could not create config directory: {err_message}");
}
}

match File::create(&default_file_path) {
Err(err_message) => {
log::error!(
"could not create config file {}: {err_message}",
default_file_path.display()
)
}
Ok(mut created_file) => {
log::info!("configuration file created {}", default_file_path.display());

if let Err(err_message) = writeln!(
created_file,
"{}",
rio_backend::config::config_file_content()
) {
log::error!(
"could not update config file with defaults: {err_message}"
)
}
}
}
}
}

pub struct Router {
Expand Down
44 changes: 43 additions & 1 deletion rio-backend/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ use crate::config::window::Window;
use colors::Colors;
use log::warn;
use serde::{Deserialize, Serialize};
use std::default::Default;
use std::io::Write;
use std::path::PathBuf;
use std::{default::Default, fs::File};
use sugarloaf::font::fonts::SugarloafFonts;
use theme::{AdaptiveColors, AdaptiveTheme, Theme};

Expand Down Expand Up @@ -153,6 +154,47 @@ pub fn config_file_content() -> String {
default_config_file_content()
}

#[inline]
pub fn create_config_file(path: Option<PathBuf>) {
let default_file_path = path.clone().unwrap_or(config_file_path());
if default_file_path.exists() {
log::info!(
"configuration file already exists at {}",
default_file_path.display()
);
return;
}

if path.is_none() {
let default_dir_path = config_dir_path();
match std::fs::create_dir_all(&default_dir_path) {
Ok(_) => {
log::info!("configuration path created {}", default_dir_path.display());
}
Err(err_message) => {
log::error!("could not create config directory: {err_message}");
}
}
}

match File::create(&default_file_path) {
Err(err_message) => {
log::error!(
"could not create config file {}: {err_message}",
default_file_path.display()
)
}
Ok(mut created_file) => {
log::info!("configuration file created {}", default_file_path.display());

if let Err(err_message) = writeln!(created_file, "{}", config_file_content())
{
log::error!("could not update config file with defaults: {err_message}")
}
}
}
}

impl Config {
#[cfg(test)]
fn load_from_path(path: &PathBuf) -> Self {
Expand Down

0 comments on commit 8b245f8

Please sign in to comment.