feature/fix-clippy-on-stable-channel
claude/smbcloud-github-app-k9641p
development
feature/apps
feature/build-release
feature/ci-docs
feature/ci-ruby-gems
feature/cicd
feature/completion
feature/dist-nuget
feature/fix-ci-sdk-pypi-rubygem
feature/fix-clippy-on-stable-channel
feature/fix-deployment-script
feature/github-email
feature/gresiq-v1
feature/project-new-structure
feature/rsync-deploy
feature/ruby-runner
feature/sdk-npm
feature/sdk-workflows
feature/smbcloud-cli-release-npm
feature/smbcloud-deploy
feature/wasm
main
release/v0.7.4
| 1 | use { |
| 2 | anyhow::{anyhow, Result}, |
| 3 | log::debug, |
| 4 | regex::Regex, |
| 5 | smbcloud_model::project::Config, |
| 6 | std::{fs::OpenOptions, io::Write}, |
| 7 | }; |
| 8 | |
| 9 | pub mod config; |
| 10 | pub mod write_config; |
| 11 | |
| 12 | pub fn email_validation(input: &str) -> Result<(), &'static str> { |
| 13 | let email_regex = Regex::new( |
| 14 | r"^([a-z0-9_+]([a-z0-9_+.]*[a-z0-9_+])?)@([a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,6})", |
| 15 | ) |
| 16 | .unwrap(); |
| 17 | |
| 18 | if email_regex.is_match(input) { |
| 19 | Ok(()) |
| 20 | } else { |
| 21 | Err("Username must be an email address.") |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | pub async fn get_config() -> Result<Config> { |
| 26 | match home::home_dir() { |
| 27 | Some(mut path) => { |
| 28 | path.push(".smb/config.json"); |
| 29 | if !path.exists() { |
| 30 | let config = Config { |
| 31 | current_project: None, |
| 32 | current_frontend_app: None, |
| 33 | current_auth_app: None, |
| 34 | }; |
| 35 | return Ok(config); |
| 36 | } |
| 37 | let config_string = std::fs::read_to_string(path).map_err(|e| { |
| 38 | debug!("Error while reading config file: {}", e); |
| 39 | anyhow!("Error while reading config file. Are you logged in?") |
| 40 | })?; |
| 41 | let config: Config = serde_json::from_str(&config_string).map_err(|e| { |
| 42 | debug!("Error while parsing config: {}", e); |
| 43 | anyhow!("Error while parsing config. Are you logged in?") |
| 44 | })?; |
| 45 | |
| 46 | Ok(config) |
| 47 | } |
| 48 | None => { |
| 49 | let config = Config { |
| 50 | current_project: None, |
| 51 | current_frontend_app: None, |
| 52 | current_auth_app: None, |
| 53 | }; |
| 54 | Ok(config) |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | pub fn write_config(config: Config) -> Result<Config> { |
| 60 | match home::home_dir() { |
| 61 | Some(path) => { |
| 62 | debug!("{}", path.to_str().unwrap()); |
| 63 | let mut file = OpenOptions::new() |
| 64 | .create(true) |
| 65 | .truncate(true) |
| 66 | .write(true) |
| 67 | .open([path.to_str().unwrap(), "/.smb/config.json"].join(""))?; |
| 68 | let json = serde_json::to_string(&config)?; |
| 69 | file.write_all(json.as_bytes())?; |
| 70 | |
| 71 | Ok(config) |
| 72 | } |
| 73 | None => Err(anyhow!("Error getting home directory.")), |
| 74 | } |
| 75 | } |