From 1182675170d40601768d67cd47b5e2a93dfeb6b4 Mon Sep 17 00:00:00 2001 From: Roey Darwish Dror Date: Mon, 4 Nov 2019 22:55:06 +0200 Subject: [PATCH] Fix editing broken configuration (fix #241) --- src/config.rs | 15 +++++++-------- src/main.rs | 11 +++++++---- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/config.rs b/src/config.rs index 0a43d084..3843ffa5 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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 { - let opt = CommandLineArgs::from_args(); - + pub fn load(base_dirs: &BaseDirs, opt: CommandLineArgs) -> Result { 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) diff --git a/src/main.rs b/src/main.rs index 7f5e1776..a9856c6c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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)] {