Skip to content

Commit

Permalink
馃悰 fix error when no .surrealdb configuration found
Browse files Browse the repository at this point in the history
  • Loading branch information
Odonno committed Jun 6, 2023
1 parent ed87a30 commit 40966de
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 53 deletions.
2 changes: 1 addition & 1 deletion src/branch/merge/overwrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub async fn main(args: MergeOverwriteBranchArgs<'_>) -> Result<()> {
config_file,
} = args;

let folder_path = config::retrieve_folder_path(config_file)?;
let folder_path = config::retrieve_folder_path(config_file);
let dump_file_path = io::concat_path(&folder_path, DUMP_FILENAME);

let branch_client = create_branch_client(config_file, &branch.name, db_configuration).await?;
Expand Down
4 changes: 2 additions & 2 deletions src/branch/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ pub async fn main(args: NewBranchArgs<'_>) -> Result<()> {
config_file,
} = args;

let db_config = config::retrieve_db_config(config_file)?;
let db_config = config::retrieve_db_config(config_file);
let db_configuration = merge_db_config(db_configuration, &db_config);

let folder_path = config::retrieve_folder_path(config_file)?;
let folder_path = config::retrieve_folder_path(config_file);
let dump_file_path = io::concat_path(&folder_path, DUMP_FILENAME);

let branching_feature_client =
Expand Down
6 changes: 2 additions & 4 deletions src/config/common.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use anyhow::Result;
use ini::Ini;
use std::path::Path;

pub fn load_config(config_file: Option<&str>) -> Result<Ini> {
pub fn load_config(config_file: Option<&str>) -> Option<Ini> {
let config_file_path = config_file.unwrap_or(".surrealdb");
let surrealdb_config_file = Path::new(&config_file_path);

let ini = Ini::load_from_file(surrealdb_config_file)?;
Ok(ini)
Ini::load_from_file(surrealdb_config_file).ok()
}

pub fn retrieve_config_value(config: &Ini, section: &str, key: &str) -> Option<String> {
Expand Down
33 changes: 17 additions & 16 deletions src/config/core.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use anyhow::Result;

use super::common::{load_config, retrieve_config_value};

#[allow(dead_code)]
Expand All @@ -8,26 +6,29 @@ pub enum TableSchemaDesign {
Schemaless,
}

pub fn retrieve_folder_path(config_file: Option<&str>) -> Result<Option<String>> {
let config = load_config(config_file)?;
let value = retrieve_config_value(&config, "core", "path");
pub fn retrieve_folder_path(config_file: Option<&str>) -> Option<String> {
let config = load_config(config_file);

Ok(value)
if let Some(config) = config {
retrieve_config_value(&config, "core", "path")
} else {
None
}
}

#[allow(dead_code)]
pub fn retrieve_table_schema_design(
config_file: Option<&str>,
) -> Result<Option<TableSchemaDesign>> {
let config = load_config(config_file)?;
let schema_str = retrieve_config_value(&config, "core", "schema");
pub fn retrieve_table_schema_design(config_file: Option<&str>) -> Option<TableSchemaDesign> {
let config = load_config(config_file);

let schema_str = if let Some(config) = config {
retrieve_config_value(&config, "core", "schema")
} else {
None
};

match schema_str {
Some(schema_str) => {
let value = parse_table_schema_design(schema_str);
Ok(value)
}
_ => Ok(None),
Some(schema_str) => parse_table_schema_design(schema_str),
_ => None,
}
}

Expand Down
35 changes: 21 additions & 14 deletions src/config/db.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use anyhow::Result;

use super::common::{load_config, retrieve_config_value};

#[allow(dead_code)]
Expand All @@ -13,17 +11,26 @@ pub struct DbConfig {
}

#[allow(dead_code)]
pub fn retrieve_db_config(config_file: Option<&str>) -> Result<DbConfig> {
let config = load_config(config_file)?;

let db_config = DbConfig {
address: retrieve_config_value(&config, "db", "address"),
url: retrieve_config_value(&config, "db", "url"),
username: retrieve_config_value(&config, "db", "username"),
password: retrieve_config_value(&config, "db", "password"),
ns: retrieve_config_value(&config, "db", "ns"),
db: retrieve_config_value(&config, "db", "db"),
};
pub fn retrieve_db_config(config_file: Option<&str>) -> DbConfig {
let config = load_config(config_file);

Ok(db_config)
if let Some(config) = config {
DbConfig {
address: retrieve_config_value(&config, "db", "address"),
url: retrieve_config_value(&config, "db", "url"),
username: retrieve_config_value(&config, "db", "username"),
password: retrieve_config_value(&config, "db", "password"),
ns: retrieve_config_value(&config, "db", "ns"),
db: retrieve_config_value(&config, "db", "db"),
}
} else {
DbConfig {
address: None,
url: None,
username: None,
password: None,
ns: None,
db: None,
}
}
}
4 changes: 2 additions & 2 deletions src/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn main(args: CreateArgs) -> Result<()> {
CreateOperation::Migration(_) => false,
};

let folder_path = config::retrieve_folder_path(config_file)?;
let folder_path = config::retrieve_folder_path(config_file);

let dir_name = match operation {
CreateOperation::Schema(_) => SCHEMAS_DIR_NAME,
Expand Down Expand Up @@ -169,7 +169,7 @@ fn get_table_schema_design_str(
return Ok(SCHEMAFULL);
}

let table_schema_design = retrieve_table_schema_design(config_file)?;
let table_schema_design = retrieve_table_schema_design(config_file);

let value = match table_schema_design {
Some(table_schema_design) => match table_schema_design {
Expand Down
14 changes: 7 additions & 7 deletions src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn concat_path(folder_path: &Option<String>, dir_name: &str) -> PathBuf {
}

pub fn can_use_filesystem(config_file: Option<&str>) -> Result<bool> {
let folder_path = config::retrieve_folder_path(config_file)?;
let folder_path = config::retrieve_folder_path(config_file);
let script_migration_path = concat_path(&folder_path, SCHEMAS_DIR_NAME)
.join(format!("{}.surql", SCRIPT_MIGRATION_TABLE_NAME));
let script_migration_file_try_exists = script_migration_path.try_exists().ok();
Expand Down Expand Up @@ -206,7 +206,7 @@ fn extract_surql_files_from_filesystem(
) -> Result<Vec<SurqlFile>> {
let dir_path_str = dir_path.display().to_string();

let folder_path = config::retrieve_folder_path(config_file)?;
let folder_path = config::retrieve_folder_path(config_file);
let dir_path = concat_path(&folder_path, &dir_path_str);

let mut config = HashSet::new();
Expand Down Expand Up @@ -345,7 +345,7 @@ fn extract_json_definition_files_from_filesystem(
) -> Result<Vec<JsonDefinitionFile>> {
let dir_path_str = dir_path.display().to_string();

let folder_path = config::retrieve_folder_path(config_file)?;
let folder_path = config::retrieve_folder_path(config_file);
let dir_path = concat_path(&folder_path, &dir_path_str);

if !dir_path.exists() {
Expand Down Expand Up @@ -444,7 +444,7 @@ fn update_migration_definition_file(
)?,
};

let folder_path = config::retrieve_folder_path(config_file)?;
let folder_path = config::retrieve_folder_path(config_file);
let definitions_path = match &folder_path {
Some(folder_path) => Path::new(&folder_path).join(definitions_path),
None => definitions_path,
Expand Down Expand Up @@ -528,7 +528,7 @@ fn create_initial_definition_file(
schema_definitions: String,
event_definitions: String,
) -> Result<String> {
let folder_path = config::retrieve_folder_path(config_file)?;
let folder_path = config::retrieve_folder_path(config_file);
let definitions_path = match &folder_path {
Some(folder_path) => Path::new(&folder_path).join(definitions_path),
None => definitions_path.clone(),
Expand Down Expand Up @@ -665,7 +665,7 @@ fn extract_initial_definition_content(
Ok(content)
}
None => {
let folder_path = config::retrieve_folder_path(config_file)?;
let folder_path = config::retrieve_folder_path(config_file);
let definitions_path = match &folder_path {
Some(folder_path) => Path::new(&folder_path).join(definitions_path),
None => definitions_path,
Expand Down Expand Up @@ -730,7 +730,7 @@ fn extract_definition_diff_content(
}
}
None => {
let folder_path = config::retrieve_folder_path(config_file)?;
let folder_path = config::retrieve_folder_path(config_file);
let definitions_path = match &folder_path {
Some(folder_path) => Path::new(&folder_path).join(definitions_path),
None => definitions_path,
Expand Down
8 changes: 4 additions & 4 deletions src/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn main(config_file: Option<&str>) -> Result<()> {
}

fn remove_migration_file(config_file: Option<&str>, last_migration: &SurqlFile) -> Result<()> {
let folder_path = config::retrieve_folder_path(config_file)?;
let folder_path = config::retrieve_folder_path(config_file);
let migrations_path = io::concat_path(&folder_path, MIGRATIONS_DIR_NAME);

let file_path = migrations_path.join(&last_migration.full_name);
Expand All @@ -48,7 +48,7 @@ fn remove_definition_file_if_exists(
config_file: Option<&str>,
last_migration: &SurqlFile,
) -> Result<()> {
let folder_path = config::retrieve_folder_path(config_file)?;
let folder_path = config::retrieve_folder_path(config_file);
let migrations_path = io::concat_path(&folder_path, MIGRATIONS_DIR_NAME);

let migration_definition_file_path = Path::new(&migrations_path)
Expand Down Expand Up @@ -76,7 +76,7 @@ fn remove_nested_down_migration_file_if_exists(
config_file: Option<&str>,
last_migration: &SurqlFile,
) -> Result<()> {
let folder_path = config::retrieve_folder_path(config_file)?;
let folder_path = config::retrieve_folder_path(config_file);
let migrations_path = io::concat_path(&folder_path, MIGRATIONS_DIR_NAME);

let down_migration_file_path = Path::new(&migrations_path)
Expand All @@ -94,7 +94,7 @@ fn remove_inlined_down_migration_file_if_exists(
config_file: Option<&str>,
last_migration: &SurqlFile,
) -> Result<()> {
let folder_path = config::retrieve_folder_path(config_file)?;
let folder_path = config::retrieve_folder_path(config_file);
let migrations_path = io::concat_path(&folder_path, MIGRATIONS_DIR_NAME);

let inlined_down_migration_file_path =
Expand Down
2 changes: 1 addition & 1 deletion src/scaffold/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn main(args: ScaffoldFromSchemaArgs) -> Result<()> {
config_file,
} = args;

let folder_path = config::retrieve_folder_path(config_file)?;
let folder_path = config::retrieve_folder_path(config_file);

apply_before_scaffold(folder_path.to_owned())?;

Expand Down
2 changes: 1 addition & 1 deletion src/scaffold/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn main(args: ScaffoldFromTemplateArgs) -> Result<()> {
config_file,
} = args;

let folder_path = config::retrieve_folder_path(config_file)?;
let folder_path = config::retrieve_folder_path(config_file);

apply_before_scaffold(folder_path.to_owned())?;

Expand Down
2 changes: 1 addition & 1 deletion src/surrealdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub async fn create_surrealdb_client(
db,
} = db_configuration;

let db_config = config::retrieve_db_config(config_file)?;
let db_config = config::retrieve_db_config(config_file);

let client = create_surrealdb_connection(url.clone(), address.clone(), &db_config).await?;
sign_in(username.clone(), password.clone(), &db_config, &client).await?;
Expand Down

0 comments on commit 40966de

Please sign in to comment.