@smbcloud / cli / commits / 4c75491

add env argument

Seto Elkahfi committed May 1, 2025 at 00:04 UTC 4c754918094f90b27c5730b61ed5dff2b42a8475
4 files changed +51 -9
smbcloud-cli/Cargo.toml
+2 -2
@@ -16,8 +16,8 @@ name = "smb"
16 path = "src/main.rs"
17
18 [dependencies]
19 -anyhow = { workspace = true }
20 -clap = { workspace = true, features = ["derive"] }
19 +anyhow = { workspace = true }
20 +clap = { workspace = true, features = ["derive", "env"] }
21 clap_mangen = { workspace = true }
22 console = { workspace = true }
23 dialoguer = { workspace = true }
smbcloud-cli/src/account/login/process.rs
+1 -1
@@ -48,7 +48,7 @@ pub async fn process_login() -> Result<CommandResult> {
48 .map(|i| signup_methods[i.unwrap()])
49 {
50 Ok(method) => method,
51 - Err(e) => {
51 + Err(_) => {
52 let error = anyhow!("No selection made.");
53 return Err(error);
54 }
smbcloud-cli/src/cli/mod.rs
+40
@@ -8,9 +8,49 @@ pub struct CommandResult {
8 pub msg: String,
9 }
10
11 +#[derive(clap::ValueEnum, Clone)]
12 +pub enum Environment {
13 + Dev,
14 + Production,
15 +}
16 +
17 +impl std::fmt::Display for Environment {
18 + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19 + write!(f, "{}", self.to_str())
20 + }
21 +}
22 +
23 +impl Environment {
24 + pub fn from_str(env: &str) -> Self {
25 + match env.to_lowercase().as_str() {
26 + "dev" => Environment::Dev,
27 + "production" => Environment::Production,
28 + _ => panic!("Invalid environment: {}", env),
29 + }
30 + }
31 +
32 + pub fn to_str(&self) -> &str {
33 + match self {
34 + Environment::Dev => "dev",
35 + Environment::Production => "production",
36 + }
37 + }
38 +
39 + pub fn smb_dir(&self) -> String {
40 + match self {
41 + Environment::Dev => ".smb-dev".to_string(),
42 + Environment::Production => ".smb".to_string(),
43 + }
44 + }
45 +}
46 +
47 #[derive(Parser)]
48 #[clap(author, version, about)]
49 pub struct Cli {
50 + /// Environment: dev, production
51 + #[arg(short, long, env = "ENVIRONMENT", default_value = "production")]
52 + pub environment: Environment,
53 +
54 /// Log level: trace, debug, info, warn, error, off
55 #[clap(short, long, global = true)]
56 pub log_level: Option<String>,
smbcloud-cli/src/main.rs
+8 -6
@@ -1,7 +1,7 @@
1 use anyhow::{anyhow, Result};
2 use clap::Parser;
3 use console::style;
4 -use smbcloud_cli::cli::CommandResult;
4 +use smbcloud_cli::cli::{CommandResult, Environment};
5 use smbcloud_cli::project::init::process_project_init;
6 use smbcloud_cli::{
7 account::process_account,
@@ -18,12 +18,12 @@ use tracing::subscriber::set_global_default;
18 use tracing_bunyan_formatter::{BunyanFormattingLayer, JsonStorageLayer};
19 use tracing_subscriber::{filter::LevelFilter, prelude::*, EnvFilter};
20
21 -fn setup_logging(level: Option<EnvFilter>) -> Result<()> {
21 +fn setup_logging( env: Environment, level: Option<EnvFilter>) -> Result<()> {
22 // Log in the current directory
23 let log_path = match home::home_dir() {
24 Some(path) => {
25 - create_dir_all(path.join(".smb"))?;
26 - let log_path = [path.to_str().unwrap(), "/.smb/smbcloud-cli.log"].join("");
25 + create_dir_all(path.join(env.smb_dir()))?;
26 + let log_path = [path.to_str().unwrap(), "/", &env.smb_dir(), "/smbcloud-cli.log"].join("");
27 // Create the file if it doesn't exist
28 let _file = OpenOptions::new()
29 .create(true)
@@ -83,6 +83,8 @@ async fn main() {
83 async fn run() -> Result<CommandResult> {
84 let cli = Cli::parse();
85
86 + // println!("Environment: {}", cli.environment);
87 +
88 let log_level_error: Result<CommandResult> = Err(anyhow!(
89 "Invalid log level: {:?}.\n Valid levels are: trace, debug, info, warn, and error.",
90 cli.log_level
@@ -93,9 +95,9 @@ async fn run() -> Result<CommandResult> {
95 Ok(filter) => filter,
96 Err(_) => return log_level_error,
97 };
96 - setup_logging(Some(filter))?;
98 + setup_logging(cli.environment, Some(filter))?;
99 } else {
98 - setup_logging(None)?;
100 + setup_logging(cli.environment, None)?;
101 }
102
103 match cli.command {