show transition information when new version runs for first time

This commit is contained in:
steven-omaha
2023-02-23 13:12:25 +01:00
parent d064416a37
commit ed9ed4f589
5 changed files with 48 additions and 27 deletions
-1
View File
@@ -1,5 +1,4 @@
# To Do # To Do
- make transition for existing user easier
- tutorial - tutorial
- update completion for new subcommands (WIP) - update completion for new subcommands (WIP)
+21 -17
View File
@@ -17,9 +17,10 @@ Main program for `pacdef`. All internal logic happens in [`pacdef_core`].
use std::process::{ExitCode, Termination}; use std::process::{ExitCode, Termination};
use anyhow::{bail, Context, Result}; use anyhow::{Context, Result};
use pacdef_core::{get_args, get_config_path, get_group_dir, Config, Group, Pacdef}; use pacdef_core::path::{get_config_path, get_config_path_old_version, get_group_dir};
use pacdef_core::{get_args, Config, Group, Pacdef};
fn main() -> ExitCode { fn main() -> ExitCode {
handle_final_result(main_inner()) handle_final_result(main_inner())
@@ -45,7 +46,17 @@ fn main_inner() -> Result<()> {
let args = get_args(); let args = get_args();
let config_file = get_config_path().context("getting config file")?; let config_file = get_config_path().context("getting config file")?;
let config = load_config(config_file)?;
let config = Config::load(&config_file)
.or_else(|_| {
get_config_path_old_version()?
.exists()
.then(show_transition_link);
let default = Config::default();
default.save(&config_file)?;
Ok::<Config, anyhow::Error>(default)
})
.context("loading config")?;
let group_dir = get_group_dir().context("resolving group dir")?; let group_dir = get_group_dir().context("resolving group dir")?;
let groups = Group::load(&group_dir, config.warn_not_symlinks) let groups = Group::load(&group_dir, config.warn_not_symlinks)
@@ -55,18 +66,11 @@ fn main_inner() -> Result<()> {
pacdef.run_action_from_arg().context("running action") pacdef.run_action_from_arg().context("running action")
} }
fn load_config(config_file: std::path::PathBuf) -> Result<Config> { fn show_transition_link() {
let config = match Config::load(&config_file) println!("VERSION UPGRADE");
.with_context(|| format!("loading config file {}", config_file.to_string_lossy())) println!("You seem to have used version 0.x of pacdef before.");
{ println!("Version 1.x changes the syntax of the config files and the command line arguments.");
Ok(config) => config, println!("Check out https://github.com/steven-omaha/pacdef for new syntax information.");
Err(e) => { println!("This message will not appear again.");
let e = e.root_cause().downcast_ref::<pacdef_core::Error>(); println!("------");
match e {
Some(pacdef_core::Error::ConfigFileNotFound) => Config::default(),
_ => bail!("huh"),
}
}
};
Ok(config)
} }
+8 -5
View File
@@ -38,10 +38,13 @@ impl Config {
serde_yaml::from_str(&content).context("parsing yaml config") serde_yaml::from_str(&content).context("parsing yaml config")
} }
fn use_default_and_save_to(file: &Path) -> Result<Self> { /// Save the instance of [`Config`] to disk.
let result = Self::default(); ///
/// # Errors
let content = serde_yaml::to_string(&result).context("converting Config to yaml")?; ///
/// This function will return an error if the config file cannot be saved to disk.
pub fn save(&self, file: &Path) -> Result<()> {
let content = serde_yaml::to_string(&self).context("converting Config to yaml")?;
let parent = file.parent().context("getting parent of config dir")?; let parent = file.parent().context("getting parent of config dir")?;
if !parent.is_dir() { if !parent.is_dir() {
@@ -52,7 +55,7 @@ impl Config {
let mut output = File::create(file).context("creating default config file")?; let mut output = File::create(file).context("creating default config file")?;
write!(output, "{content}").context("writing default config")?; write!(output, "{content}").context("writing default config")?;
Ok(result) Ok(())
} }
} }
+1 -2
View File
@@ -30,7 +30,7 @@ mod core;
mod env; mod env;
mod errors; mod errors;
mod grouping; mod grouping;
mod path; pub mod path;
mod review; mod review;
mod search; mod search;
mod ui; mod ui;
@@ -41,6 +41,5 @@ pub use crate::core::Pacdef;
pub use crate::errors::Error; pub use crate::errors::Error;
pub use crate::grouping::Group; pub use crate::grouping::Group;
pub(crate) use crate::grouping::Package; pub(crate) use crate::grouping::Package;
pub use crate::path::{get_config_path, get_group_dir};
extern crate pacdef_macros; extern crate pacdef_macros;
+18 -2
View File
@@ -1,9 +1,14 @@
/*!
All functions related to `pacdef`'s internal paths.
*/
use std::env; use std::env;
use std::path::PathBuf; use std::path::PathBuf;
use anyhow::{Context, Result}; use anyhow::{Context, Result};
const CONFIG_FILE_NAME: &str = "pacdef.yaml"; const CONFIG_FILE_NAME: &str = "pacdef.yaml";
const CONFIG_FILE_NAME_OLD: &str = "pacdef.conf";
/// Get the group directory where all group files are located. This is /// Get the group directory where all group files are located. This is
/// `$XDG_CONFIG_HOME/pacdef/groups`, which defaults to `$HOME/.config/pacdef/groups`. /// `$XDG_CONFIG_HOME/pacdef/groups`, which defaults to `$HOME/.config/pacdef/groups`.
@@ -17,7 +22,7 @@ pub fn get_group_dir() -> Result<PathBuf> {
Ok(result) Ok(result)
} }
pub fn get_pacdef_base_dir() -> Result<PathBuf> { pub(crate) fn get_pacdef_base_dir() -> Result<PathBuf> {
let mut dir = get_xdg_config_home().context("getting XDG_CONFIG_HOME")?; let mut dir = get_xdg_config_home().context("getting XDG_CONFIG_HOME")?;
dir.push("pacdef"); dir.push("pacdef");
Ok(dir) Ok(dir)
@@ -33,7 +38,7 @@ fn get_xdg_config_home() -> Result<PathBuf> {
} }
} }
pub fn get_home_dir() -> Result<PathBuf> { pub(crate) fn get_home_dir() -> Result<PathBuf> {
Ok(env::var("HOME").context("getting $HOME variable")?.into()) Ok(env::var("HOME").context("getting $HOME variable")?.into())
} }
@@ -47,3 +52,14 @@ pub fn get_config_path() -> Result<PathBuf> {
file.push(CONFIG_FILE_NAME); file.push(CONFIG_FILE_NAME);
Ok(file) Ok(file)
} }
/// Get the path to the pacdef config file. This is `$XDG_CONFIG_HOME/pacdef/pacdef.yaml`.
///
/// # Errors
///
/// This function returns an error if both `$XDG_CONFIG_HOME` and `$HOME` are undefined.
pub fn get_config_path_old_version() -> Result<PathBuf> {
let mut file = get_pacdef_base_dir().context("getting pacdef base dir for config file")?;
file.push(CONFIG_FILE_NAME_OLD);
Ok(file)
}