mirror of
https://github.com/zaphar/kitchen.git
synced 2025-07-22 19:40:14 -04:00
Fix panic when the 'serve' subcommand is used for the first time (#25)
When the 'serve' subcommand is executed for the first time, sqlite will panic on missing .session_store directory.
This commit is contained in:
parent
1ff29fbe1b
commit
410548529c
@ -38,13 +38,13 @@ fn create_app<'a>() -> clap::App<'a> {
|
|||||||
)
|
)
|
||||||
(@subcommand groceries =>
|
(@subcommand groceries =>
|
||||||
(about: "print out a grocery list for a set of recipes")
|
(about: "print out a grocery list for a set of recipes")
|
||||||
(@arg csv: --csv "output ingredeints as csv")
|
(@arg csv: --csv "output ingredients as csv")
|
||||||
(@arg INPUT: +required "Input menu file to parse. One recipe file per line.")
|
(@arg INPUT: +required "Input menu file to parse. One recipe file per line.")
|
||||||
)
|
)
|
||||||
(@subcommand serve =>
|
(@subcommand serve =>
|
||||||
(about: "Serve the interface via the web")
|
(about: "Serve the interface via the web")
|
||||||
(@arg recipe_dir: -d --dir +takes_value "Directory containing recipe files to use")
|
(@arg recipe_dir: -d --dir +takes_value "Directory containing recipe files to use")
|
||||||
(@arg session_dir: --session_dir +takes_value "Session store directory to use")
|
(@arg session_dir: --session_dir +takes_value +required "Session store directory to use")
|
||||||
(@arg tls: --tls "Use TLS to serve.")
|
(@arg tls: --tls "Use TLS to serve.")
|
||||||
(@arg cert_path: --cert +takes_value "Certificate path. Required if you specified --tls.")
|
(@arg cert_path: --cert +takes_value "Certificate path. Required if you specified --tls.")
|
||||||
(@arg key_path: --cert_key +takes_value "Certificate key path. Required if you specified --tls")
|
(@arg key_path: --cert_key +takes_value "Certificate key path. Required if you specified --tls")
|
||||||
@ -55,7 +55,7 @@ fn create_app<'a>() -> clap::App<'a> {
|
|||||||
(@arg recipe_dir: -d --dir +takes_value "Directory containing recipe files to load for user")
|
(@arg recipe_dir: -d --dir +takes_value "Directory containing recipe files to load for user")
|
||||||
(@arg user: -u --user +takes_value +required "username to add")
|
(@arg user: -u --user +takes_value +required "username to add")
|
||||||
(@arg pass: -p --pass +takes_value +required "password to add for this user")
|
(@arg pass: -p --pass +takes_value +required "password to add for this user")
|
||||||
(@arg session_dir: --session_dir +takes_value "Session store directory to use")
|
(@arg session_dir: --session_dir +takes_value +required "Session store directory to use")
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.setting(clap::AppSettings::SubcommandRequiredElseHelp)
|
.setting(clap::AppSettings::SubcommandRequiredElseHelp)
|
||||||
@ -65,9 +65,10 @@ fn get_session_store_path(matches: &ArgMatches) -> PathBuf {
|
|||||||
if let Some(dir) = matches.value_of("session_dir") {
|
if let Some(dir) = matches.value_of("session_dir") {
|
||||||
PathBuf::from(dir)
|
PathBuf::from(dir)
|
||||||
} else {
|
} else {
|
||||||
let mut dir =
|
let mut dir = std::env::var("HOME")
|
||||||
std::env::current_dir().expect("Unable to get current directory. Bailing out.");
|
.map(PathBuf::from)
|
||||||
dir.push(".session_store");
|
.expect("Unable to get user home directory. Bailing out.");
|
||||||
|
dir.push(".kitchen");
|
||||||
dir
|
dir
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -256,6 +256,7 @@ pub struct SqliteStore {
|
|||||||
|
|
||||||
impl SqliteStore {
|
impl SqliteStore {
|
||||||
pub async fn new<P: AsRef<Path>>(path: P) -> sqlx::Result<Self> {
|
pub async fn new<P: AsRef<Path>>(path: P) -> sqlx::Result<Self> {
|
||||||
|
std::fs::create_dir_all(&path)?;
|
||||||
let url = format!("sqlite://{}/store.db", path.as_ref().to_string_lossy());
|
let url = format!("sqlite://{}/store.db", path.as_ref().to_string_lossy());
|
||||||
let options = SqliteConnectOptions::from_str(&url)?
|
let options = SqliteConnectOptions::from_str(&url)?
|
||||||
.journal_mode(SqliteJournalMode::Wal)
|
.journal_mode(SqliteJournalMode::Wal)
|
||||||
@ -267,7 +268,7 @@ impl SqliteStore {
|
|||||||
|
|
||||||
#[instrument(fields(conn_string=self.url), skip_all)]
|
#[instrument(fields(conn_string=self.url), skip_all)]
|
||||||
pub async fn run_migrations(&self) -> sqlx::Result<()> {
|
pub async fn run_migrations(&self) -> sqlx::Result<()> {
|
||||||
info!("Running databse migrations");
|
info!("Running database migrations");
|
||||||
sqlx::migrate!("./migrations")
|
sqlx::migrate!("./migrations")
|
||||||
.run(self.pool.as_ref())
|
.run(self.pool.as_ref())
|
||||||
.await?;
|
.await?;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user