Allow disabling steps from the configuration file
This commit is contained in:
111
src/config.rs
111
src/config.rs
@@ -27,7 +27,8 @@ lazy_static! {
|
||||
};
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
#[derive(Debug, Clone, PartialEq, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum Step {
|
||||
/// Don't perform system upgrade
|
||||
System,
|
||||
@@ -59,14 +60,19 @@ impl std::str::FromStr for Step {
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Default)]
|
||||
pub struct Config {
|
||||
/// Configuration file
|
||||
pub struct ConfigFile {
|
||||
pre_commands: Option<Commands>,
|
||||
commands: Option<Commands>,
|
||||
git_repos: Option<Vec<String>>,
|
||||
disable: Option<Vec<Step>>,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn read(base_dirs: &BaseDirs) -> Result<Config, Error> {
|
||||
impl ConfigFile {
|
||||
/// Read the configuration file.
|
||||
///
|
||||
/// If the configuration file does not exist the function returns the default ConfigFile.
|
||||
fn read(base_dirs: &BaseDirs) -> Result<ConfigFile, Error> {
|
||||
let config_path = base_dirs.config_dir().join("topgrade.toml");
|
||||
if !config_path.exists() {
|
||||
return Ok(Default::default());
|
||||
@@ -83,40 +89,101 @@ impl Config {
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub fn pre_commands(&self) -> &Option<Commands> {
|
||||
&self.pre_commands
|
||||
}
|
||||
|
||||
pub fn commands(&self) -> &Option<Commands> {
|
||||
&self.commands
|
||||
}
|
||||
|
||||
pub fn git_repos(&self) -> &Option<Vec<String>> {
|
||||
&self.git_repos
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(StructOpt, Debug)]
|
||||
#[structopt(name = "Topgrade")]
|
||||
pub struct Opt {
|
||||
/// Command line arguments
|
||||
pub struct CommandLineArgs {
|
||||
/// Run inside tmux
|
||||
#[structopt(short = "t", long = "tmux")]
|
||||
pub run_in_tmux: bool,
|
||||
run_in_tmux: bool,
|
||||
|
||||
/// Cleanup temporary or old files
|
||||
#[structopt(short = "c", long = "cleanup")]
|
||||
pub cleanup: bool,
|
||||
cleanup: bool,
|
||||
|
||||
/// Print what would be done
|
||||
#[structopt(short = "n", long = "dry-run")]
|
||||
pub dry_run: bool,
|
||||
dry_run: bool,
|
||||
|
||||
/// Do not ask to retry failed steps
|
||||
#[structopt(long = "no-retry")]
|
||||
pub no_retry: bool,
|
||||
no_retry: bool,
|
||||
|
||||
/// Do not perform upgrades for the given steps
|
||||
#[structopt(long = "disable", raw(possible_values = "&Step::possible_values()"))]
|
||||
pub disable: Vec<Step>,
|
||||
disable: Vec<Step>,
|
||||
}
|
||||
|
||||
/// Represents the application configuration
|
||||
///
|
||||
/// The struct holds the loaded configuration file, as well as the arguments parsed from the command line.
|
||||
/// Its provided methods decide the appropriate options based on combining the configuraiton file and the
|
||||
/// command line arguments.
|
||||
pub struct Config {
|
||||
opt: CommandLineArgs,
|
||||
config_file: ConfigFile,
|
||||
}
|
||||
|
||||
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> {
|
||||
Ok(Self {
|
||||
opt: CommandLineArgs::from_args(),
|
||||
config_file: ConfigFile::read(base_dirs)?,
|
||||
})
|
||||
}
|
||||
|
||||
/// The list of commands to run before performing any step.
|
||||
pub fn pre_commands(&self) -> &Option<Commands> {
|
||||
&self.config_file.pre_commands
|
||||
}
|
||||
|
||||
/// The list of custom steps.
|
||||
pub fn commands(&self) -> &Option<Commands> {
|
||||
&self.config_file.commands
|
||||
}
|
||||
|
||||
/// The list of additional git repositories to pull.
|
||||
pub fn git_repos(&self) -> &Option<Vec<String>> {
|
||||
&self.config_file.git_repos
|
||||
}
|
||||
|
||||
/// Tell whether the specified step should run.
|
||||
///
|
||||
/// If the step appears either in the `--disable` command line argument
|
||||
/// or the `disable` option in the configuration, the function returns false.
|
||||
pub fn should_run(&self, step: Step) -> bool {
|
||||
!(self
|
||||
.config_file
|
||||
.disable
|
||||
.as_ref()
|
||||
.map(|d| d.contains(&step))
|
||||
.unwrap_or(false)
|
||||
|| self.opt.disable.contains(&step))
|
||||
}
|
||||
|
||||
/// Tell whether we should run in tmux.
|
||||
pub fn run_in_tmux(&self) -> bool {
|
||||
self.opt.run_in_tmux
|
||||
}
|
||||
|
||||
/// Tell whether we should perform cleanup steps.
|
||||
#[cfg(not(windows))]
|
||||
pub fn cleanup(&self) -> bool {
|
||||
self.opt.cleanup
|
||||
}
|
||||
|
||||
/// Tell whether we are dry-running.
|
||||
pub fn dry_run(&self) -> bool {
|
||||
self.opt.dry_run
|
||||
}
|
||||
|
||||
/// Tell whether we should not attempt to retry anything.
|
||||
pub fn no_retry(&self) -> bool {
|
||||
self.opt.no_retry
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user