-
-
Notifications
You must be signed in to change notification settings - Fork 662
Feat centralized configuration for sea-orm-cli #2848
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
Open
CCXLV
wants to merge
14
commits into
SeaQL:master
Choose a base branch
from
CCXLV:cli-config-file
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
edf1b07
Add sea-orm.toml config file feature
CCXLV 8e5e06b
Fix typo
CCXLV 03587f3
Change get_config_dir to private function
CCXLV 1c5a6ef
Improve sea-orm.toml migrations config
CCXLV d3760e8
Formats
CCXLV a5a8a78
Merge remote-tracking branch 'upstream/master' into cli-config-file
CCXLV f78cddd
Update sea-orm-cli generate command to use database_url from config
CCXLV fb2a43f
Refactor merge config.rs into commands/config.rs
CCXLV 6898903
Revert CLI flags
CCXLV f67b978
Fix use flag args when passed
CCXLV a1336ea
Address review comments
CCXLV 493e2e3
Format the code
CCXLV 8c353c5
Move config module to cli feature flag
CCXLV f58e148
Format .toml files
CCXLV File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| use serde::Deserialize; | ||
| use std::path::{Path, PathBuf}; | ||
| use std::{env, error::Error, fs}; | ||
|
|
||
| use crate::ConfigSubcommands; | ||
|
|
||
| #[derive(Deserialize)] | ||
| pub struct Config { | ||
| database: Database, | ||
| migrations: Migrations, | ||
| } | ||
|
|
||
| #[derive(Deserialize)] | ||
| struct Database { | ||
| url: String, | ||
| } | ||
|
|
||
| #[derive(Deserialize)] | ||
| struct Migrations { | ||
| directory: String, | ||
| } | ||
|
|
||
| pub fn run_config_command(command: ConfigSubcommands) -> Result<(), Box<dyn Error>> { | ||
| match command { | ||
| ConfigSubcommands::Init => run_config_init(), | ||
| } | ||
| } | ||
|
|
||
| /// The config file is expected to be in the current directory or a parent directory | ||
| pub fn get_config() -> Result<Config, Box<dyn Error>> { | ||
| let config_path = find_config_file()?; | ||
|
|
||
| let file_content = fs::read_to_string(config_path)?; | ||
| let config: Config = toml::from_str(&file_content)?; | ||
| Ok(config) | ||
| } | ||
|
|
||
| pub fn get_database_url() -> Result<String, Box<dyn Error>> { | ||
| let config = get_config()?; | ||
| if config.database.url.starts_with("env:") { | ||
| let env_var = config.database.url.split(":").nth(1).unwrap(); | ||
| let value = env::var(env_var)?; | ||
| Ok(value) | ||
| } else { | ||
| Ok(config.database.url) | ||
| } | ||
| } | ||
|
|
||
| pub fn get_migration_dir() -> Result<String, Box<dyn Error>> { | ||
| let config = get_config()?; | ||
|
|
||
| let migration_dir = config.migrations.directory; | ||
| if migration_dir.starts_with(".") { | ||
CCXLV marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let config_dir = get_config_dir()?; | ||
| let migration_dir = config_dir.join(migration_dir); | ||
| return Ok(migration_dir.to_string_lossy().to_string()); | ||
| } else { | ||
| return Ok(migration_dir); | ||
CCXLV marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| fn get_config_dir() -> Result<PathBuf, Box<dyn Error>> { | ||
| let config_path = find_config_file()?; | ||
| let config_dir = config_path.parent().unwrap_or(Path::new(".")).to_path_buf(); | ||
| Ok(config_dir) | ||
| } | ||
|
|
||
| fn find_config_file() -> Result<PathBuf, Box<dyn Error>> { | ||
| let current_dir = std::env::current_dir()?; | ||
| let config_path = current_dir.join("sea-orm.toml"); | ||
| if config_path.exists() { | ||
| return Ok(config_path); | ||
| } | ||
| let parent_dir = current_dir.parent(); | ||
| if let Some(parent_dir) = parent_dir { | ||
| let config_path = parent_dir.join("sea-orm.toml"); | ||
| if config_path.exists() { | ||
| return Ok(config_path); | ||
| } | ||
| } | ||
| Err(Box::new(std::io::Error::new( | ||
| std::io::ErrorKind::NotFound, | ||
| "SeaORM config file not found, use `sea-orm-cli config init` to create one", | ||
| ))) | ||
| } | ||
|
|
||
| fn run_config_init() -> Result<(), Box<dyn Error>> { | ||
| let config_path = Path::new("sea-orm.toml"); | ||
| let config_template = include_str!("../../template/sea-orm.toml"); | ||
|
|
||
| fs::write(config_path, config_template.to_string())?; | ||
CCXLV marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| println!("Config file created at {}", config_path.display()); | ||
| Ok(()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| [database] | ||
| url = "env:DATABASE_URL" # Support env: prefix or direct URL | ||
CCXLV marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| [migrations] | ||
| directory = "./migration" # Where migrations live, relative path | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.