-
Notifications
You must be signed in to change notification settings - Fork 175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use $XDG_STATE_HOME
to store history.db
#349
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
use crate::cli::{Cli, SubCommand}; | ||
use crate::shell_history; | ||
use clap::Parser; | ||
use directories_next::{ProjectDirs, UserDirs}; | ||
use std::env; | ||
use std::path::PathBuf; | ||
use etcetera::base_strategy::Xdg; | ||
use etcetera::BaseStrategy; | ||
use std::path::{Path, PathBuf}; | ||
use std::str::FromStr; | ||
use std::time::SystemTime; | ||
use std::time::UNIX_EPOCH; | ||
use std::{env, fs}; | ||
|
||
#[derive(Debug)] | ||
pub enum Mode { | ||
|
@@ -372,35 +373,56 @@ impl Settings { | |
settings | ||
} | ||
|
||
// Use ~/.mcfly only if it already exists, otherwise create 'mcfly' folder in XDG_CACHE_DIR | ||
// Use or create the 'mcfly' folder in `$XDG_CACHE_DIR` | ||
pub fn mcfly_training_cache_path() -> PathBuf { | ||
let cache_dir = Settings::mcfly_xdg_dir().cache_dir().to_path_buf(); | ||
|
||
Settings::mcfly_base_path(cache_dir).join(PathBuf::from("training-cache.v1.csv")) | ||
Settings::xdg_base_strategy() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the difference between mcfly_xdg_dir and xdg_base_strategy? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could reuse the name if you want. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just didn't understand what the difference is between them. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
.cache_dir() | ||
.join(PathBuf::from("mcfly/training-cache.v1.csv")) | ||
} | ||
|
||
// Use ~/.mcfly only if it already exists, otherwise create 'mcfly' folder in XDG_DATA_DIR | ||
// Use or migrate to the 'mcfly' folder in `$XDG_STATE_DIR` | ||
pub fn mcfly_db_path() -> PathBuf { | ||
let data_dir = Settings::mcfly_xdg_dir().data_dir().to_path_buf(); | ||
|
||
Settings::mcfly_base_path(data_dir).join(PathBuf::from("history.db")) | ||
} | ||
|
||
fn mcfly_xdg_dir() -> ProjectDirs { | ||
ProjectDirs::from("", "", "McFly").unwrap() | ||
let basedirs = Settings::xdg_base_strategy(); | ||
let path = basedirs | ||
.state_dir() | ||
.unwrap() | ||
.join(PathBuf::from("mcfly/history.db")); | ||
if !path.exists() { | ||
println!("McFly: history.db not found: `{}`", path.display()); | ||
let old = basedirs.data_dir().join(PathBuf::from("mcfly/history.db")); | ||
if !Settings::migrate_old_path(old, &path) { | ||
let old = basedirs.home_dir().join(PathBuf::from(".mcfly/history.db")); | ||
if !Settings::migrate_old_path(old, &path) { | ||
#[cfg(target_os = "macos")] | ||
{ | ||
let old = etcetera::base_strategy::Apple::new() | ||
.unwrap() | ||
.data_dir() | ||
.join(PathBuf::from("McFly/history.db")); | ||
Settings::migrate_old_path(old, &path); | ||
} | ||
} | ||
} | ||
} | ||
path | ||
} | ||
|
||
fn mcfly_base_path(base_dir: PathBuf) -> PathBuf { | ||
Settings::mcfly_dir_in_home().unwrap_or(base_dir) | ||
fn xdg_base_strategy() -> impl BaseStrategy { | ||
Xdg::new().unwrap() | ||
} | ||
|
||
fn mcfly_dir_in_home() -> Option<PathBuf> { | ||
let user_dirs_file = UserDirs::new() | ||
.unwrap() | ||
.home_dir() | ||
.join(PathBuf::from(".mcfly")); | ||
|
||
user_dirs_file.exists().then_some(user_dirs_file) | ||
fn migrate_old_path(old: PathBuf, new: &Path) -> bool { | ||
println!("McFly: Checking old history.db: `{}`", old.display()); | ||
let ret = old.exists(); | ||
if ret { | ||
fs::create_dir_all(new.parent().unwrap()).unwrap(); | ||
fs::copy(&old, new).unwrap(); | ||
println!( | ||
"McFly: history.db migrated.\nYou can now delete the old directory: `rm -r '{}'`", | ||
old.parent().unwrap().display() | ||
); | ||
} | ||
ret | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think instead of migrating, it should keep using the old location if it exists.