feature/completion
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 std::{fs::OpenOptions, io::Write}; |
| 2 | |
| 3 | use anyhow::{anyhow, Result}; |
| 4 | use log::debug; |
| 5 | use regex::Regex; |
| 6 | use smbpndk_model::Config; |
| 7 | |
| 8 | pub fn email_validation(input: &str) -> Result<(), &'static str> { |
| 9 | let email_regex = Regex::new( |
| 10 | r"^([a-z0-9_+]([a-z0-9_+.]*[a-z0-9_+])?)@([a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,6})", |
| 11 | ) |
| 12 | .unwrap(); |
| 13 | |
| 14 | if email_regex.is_match(input) { |
| 15 | Ok(()) |
| 16 | } else { |
| 17 | Err("Username must be an email address.") |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | pub async fn get_config() -> Result<Config> { |
| 22 | match home::home_dir() { |
| 23 | Some(mut path) => { |
| 24 | path.push(".smb/config.json"); |
| 25 | if !path.exists() { |
| 26 | let config = Config { |
| 27 | current_project: None, |
| 28 | current_auth_app: None, |
| 29 | }; |
| 30 | return Ok(config); |
| 31 | } |
| 32 | let config_string = std::fs::read_to_string(path).map_err(|e| { |
| 33 | debug!("Error while reading config file: {}", &e); |
| 34 | anyhow!("Error while reading config file. Are you logged in?") |
| 35 | })?; |
| 36 | let config: Config = serde_json::from_str(&config_string).map_err(|e| { |
| 37 | debug!("Error while parsing config: {}", &e); |
| 38 | anyhow!("Error while parsing config. Are you logged in?") |
| 39 | })?; |
| 40 | |
| 41 | Ok(config) |
| 42 | } |
| 43 | None => { |
| 44 | let config = Config { |
| 45 | current_project: None, |
| 46 | current_auth_app: None, |
| 47 | }; |
| 48 | Ok(config) |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | pub fn write_config(config: Config) -> Result<Config> { |
| 54 | match home::home_dir() { |
| 55 | Some(path) => { |
| 56 | debug!("{}", path.to_str().unwrap()); |
| 57 | let mut file = OpenOptions::new() |
| 58 | .create(true) |
| 59 | .write(true) |
| 60 | .open([path.to_str().unwrap(), "/.smb/config.json"].join(""))?; |
| 61 | let json = serde_json::to_string(&config)?; |
| 62 | file.write_all(json.as_bytes())?; |
| 63 | |
| 64 | Ok(config) |
| 65 | } |
| 66 | None => Err(anyhow!("Error getting home directory.")), |
| 67 | } |
| 68 | } |