@smbcloud / cli / commits / c539a5c

wip

Seto Elkahfi committed May 28, 2025 at 20:36 UTC c539a5c99ee06b6b097aaa0eaa144fe7027613f2
4 files changed +42 -19
.smb/config.toml
+2
@@ -1,4 +1,6 @@
1 name = 'smbCloud CLI'
2 +description = 'smbCloud CLI monorepo'
3
4 [repository]
5 +id = 1
6 name = 'smbcloudcli'
crates/cli/src/deploy/config.rs
+35 -18
@@ -1,16 +1,15 @@
1 use std::{fs, path::Path};
2
3 use crate::ui::{fail_message, fail_symbol, succeed_message, succeed_symbol};
4 -use anyhow::Result;
5 -use console::style;
4 use git2::{Cred, CredentialType, Error};
5 +use serde::Deserialize;
6 use spinners::Spinner;
8 -use toml::Value;
7 +use thiserror::Error;
8
10 -pub(crate) async fn check_config() -> Result<Config> {
9 +pub(crate) async fn check_config() -> Result<Config, ConfigError> {
10 let mut spinner: Spinner = Spinner::new(
11 spinners::Spinners::SimpleDotsScrolling,
13 - style("Checking config...").green().bold().to_string(),
12 + succeed_message("Checking config"),
13 );
14
15 // Check .smb directory
@@ -19,30 +18,40 @@ pub(crate) async fn check_config() -> Result<Config> {
18 let config_path = Path::new(".smb/config.toml");
19 if !config_path.exists() {
20 spinner.stop_and_persist(&fail_symbol(), fail_message("Invalid config."));
22 - return Err(anyhow::anyhow!(fail_message(
23 - "No config file found. Please run `smbcloud init` command."
24 - )));
21 + return Err(ConfigError::MissingConfig);
22 }
23
24 // Parse toml file
28 - let config_content = fs::read_to_string(config_path)?;
29 - let config: Value = toml::from_str(&config_content)?;
25 + let config_content = fs::read_to_string(config_path).map_err(|_| ConfigError::MissingConfig)?;
26
31 - let repo_name = config
32 - .get("repository")
33 - .and_then(|repo| repo.get("name"))
34 - .and_then(|name| name.as_str())
35 - .ok_or_else(|| anyhow::anyhow!(fail_message("Repo name not found in config file.")))?;
27 + let config: Config = match toml::from_str(&config_content) {
28 + Ok(value) => value,
29 + Err(_) => {
30 + spinner.stop_and_persist(&fail_symbol(), fail_message("Config unsync."));
31 + handle_config_error()?
32 + }
33 + };
34
35 spinner.stop_and_persist(&succeed_symbol(), succeed_message("Valid config."));
36
39 - Ok(Config {
40 - name: repo_name.to_owned(),
41 - })
37 + Ok(config)
38 +}
39 +
40 +fn handle_config_error() -> Result<Config, ConfigError> {
41 + todo!()
42 }
43
44 +#[derive(Deserialize)]
45 pub struct Config {
46 pub name: String,
47 + pub description: String,
48 + pub repository: Repository,
49 +}
50 +
51 +#[derive(Deserialize)]
52 +pub struct Repository {
53 + pub id: i32,
54 + pub name: String,
55 }
56
57 impl Config {
@@ -65,3 +74,11 @@ impl Config {
74 key_path_str
75 }
76 }
77 +
78 +#[derive(Error, Debug, PartialEq, Eq)]
79 +pub enum ConfigError {
80 + #[error("Missing config.")]
81 + MissingConfig,
82 + #[error("Missing id in repository")]
83 + MissingId,
84 +}
crates/cli/src/deploy/mod.rs
+1 -1
@@ -1,4 +1,4 @@
1 -mod config;
1 +pub mod config;
2 mod git;
3 mod remote_messages;
4
crates/cli/src/project/crud_create.rs
+4
@@ -1,3 +1,4 @@
1 +use crate::deploy::config::check_config;
2 use crate::{
3 account::lib::protected_request,
4 cli::CommandResult,
@@ -14,6 +15,9 @@ use spinners::Spinner;
15 pub async fn process_project_init(env: Environment) -> Result<CommandResult> {
16 protected_request(env).await?;
17
18 + // Check config.
19 + let config = check_config().await?;
20 +
21 let project_name = match Input::<String>::with_theme(&ColorfulTheme::default())
22 .with_prompt("Project name")
23 .interact()