Fix editing broken configuration (fix #241)

This commit is contained in:
Roey Darwish Dror
2019-11-04 22:55:06 +02:00
parent 4e1e0788f8
commit 1182675170
2 changed files with 14 additions and 12 deletions

View File

@@ -166,6 +166,12 @@ pub struct CommandLineArgs {
yes: bool,
}
impl CommandLineArgs {
pub fn edit_config(&self) -> bool {
self.edit_config
}
}
/// Represents the application configuration
///
/// The struct holds the loaded configuration file, as well as the arguments parsed from the command line.
@@ -181,9 +187,7 @@ impl Config {
/// Load the configuration.
///
/// The function parses the command line arguments and reading the configuration file.
pub fn load(base_dirs: &BaseDirs) -> Result<Self, Error> {
let opt = CommandLineArgs::from_args();
pub fn load(base_dirs: &BaseDirs, opt: CommandLineArgs) -> Result<Self, Error> {
let mut builder = formatted_timed_builder();
if opt.verbose {
@@ -292,11 +296,6 @@ impl Config {
self.opt.keep_at_end || env::var("TOPGRADE_KEEP_END").is_ok()
}
/// Whether to edit the configuration file
pub fn edit_config(&self) -> bool {
self.opt.edit_config
}
/// Whether to set the terminal title
pub fn set_title(&self) -> bool {
self.config_file.set_title.unwrap_or(true)

View File

@@ -10,7 +10,7 @@ mod steps;
mod terminal;
mod utils;
use self::config::{Config, Step};
use self::config::{CommandLineArgs, Config, Step};
use self::error::{Error, ErrorKind};
use self::report::Report;
use self::steps::*;
@@ -24,6 +24,7 @@ use std::env;
use std::fmt::Debug;
use std::io;
use std::process::exit;
use structopt::StructOpt;
fn execute<'a, F, M>(report: &mut Report<'a>, key: M, func: F, no_retry: bool) -> Result<(), Error>
where
@@ -65,14 +66,16 @@ fn run() -> Result<(), Error> {
ctrlc::set_handler();
let base_dirs = directories::BaseDirs::new().ok_or(ErrorKind::NoBaseDirectories)?;
let config = Config::load(&base_dirs)?;
terminal::set_title(config.set_title());
if config.edit_config() {
let opt = CommandLineArgs::from_args();
if opt.edit_config() {
Config::edit(&base_dirs)?;
return Ok(());
};
let config = Config::load(&base_dirs, opt)?;
terminal::set_title(config.set_title());
if config.run_in_tmux() && env::var("TOPGRADE_INSIDE_TMUX").is_err() {
#[cfg(unix)]
{