Skip to content

Commit

Permalink
..
Browse files Browse the repository at this point in the history
  • Loading branch information
infiniteregrets committed Sep 27, 2024
1 parent 07f689e commit aaf0ff9
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 22 deletions.
125 changes: 122 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ tokio = { version = "*", features = ["full"] }
humantime = "2.1.0"
miette = { version = "7.2.0", features = ["fancy"] }
color-print = "0.3.6"
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
17 changes: 5 additions & 12 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::{
env,
path::{Path, PathBuf},
};
use std::path::{Path, PathBuf};

use config::{Config, FileFormat};
use miette::Diagnostic;
Expand All @@ -12,7 +9,7 @@ use crate::error::S2CliError;

#[derive(Debug, Deserialize, Serialize)]
pub struct S2Config {
pub token: String,
pub auth_token: String,
}

#[cfg(target_os = "windows")]
Expand All @@ -33,24 +30,20 @@ pub fn config_path() -> Result<PathBuf, S2CliError> {
}

pub fn load_config(path: &Path) -> Result<S2Config, S2ConfigError> {
if let Ok(env_token) = env::var("S2_AUTH_TOKEN") {
return Ok(S2Config { token: env_token });
}
Config::builder()
.add_source(config::File::new(
path.to_str().ok_or(S2ConfigError::PathError)?,
FileFormat::Toml,
))
.add_source(config::Environment::with_prefix("S2"))
.build()
.map_err(|_| S2ConfigError::LoadError)?
.try_deserialize::<S2Config>()
.map_err(|_| S2ConfigError::LoadError)
}

pub fn create_config(config_path: &PathBuf, token: String) -> Result<(), S2ConfigError> {
let cfg = S2Config {
token: token.to_string(),
};
pub fn create_config(config_path: &PathBuf, auth_token: String) -> Result<(), S2ConfigError> {
let cfg = S2Config { auth_token };

if let Some(parent) = config_path.parent() {
std::fs::create_dir_all(parent).map_err(|_| S2ConfigError::WriteError)?;
Expand Down
27 changes: 20 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use s2::{
client::{Client, ClientConfig, HostCloud},
types::{BasinMetadata, StorageClass},
};
use tracing_subscriber::{fmt::format::FmtSpan, layer::SubscriberExt, util::SubscriberInitExt};

mod account;
mod basin;
Expand Down Expand Up @@ -60,7 +61,7 @@ enum ConfigActions {
/// Set the authentication token
Set {
#[arg(short, long)]
token: String,
auth_token: String,
},
}

Expand Down Expand Up @@ -123,10 +124,10 @@ enum BasinActions {
},
}

async fn s2_client(token: String) -> Result<Client, S2CliError> {
async fn s2_client(auth_token: String) -> Result<Client, S2CliError> {
let config = ClientConfig::builder()
.host_uri(HostCloud::Local)
.token(token.to_string())
.token(auth_token.to_string())
.connection_timeout(std::time::Duration::from_secs(5))
.build();

Expand All @@ -143,10 +144,21 @@ async fn run() -> Result<(), S2CliError> {
let commands = Cli::parse();
let config_path = config_path()?;

tracing_subscriber::registry()
.with(
tracing_subscriber::fmt::layer()
.pretty()
.with_span_events(FmtSpan::NEW)
.compact()
.with_writer(std::io::stderr),
)
.with(tracing_subscriber::EnvFilter::from_default_env())
.init();

match commands.command {
Commands::Config { action } => match action {
ConfigActions::Set { token } => {
create_config(&config_path, token)?;
ConfigActions::Set { auth_token } => {
create_config(&config_path, auth_token)?;
println!("{}", "✓ Token set successfully".green().bold());
println!(
" Configuration saved to: {}",
Expand All @@ -157,7 +169,8 @@ async fn run() -> Result<(), S2CliError> {

Commands::Account { action } => {
let cfg = config::load_config(&config_path)?;
let account_service = AccountService::new(s2_client(cfg.token).await?);
println!("cfg: {:?}", cfg);
let account_service = AccountService::new(s2_client(cfg.auth_token).await?);
match action {
AccountActions::ListBasins {
prefix,
Expand Down Expand Up @@ -203,7 +216,7 @@ async fn run() -> Result<(), S2CliError> {
}
Commands::Basins { action } => {
let cfg = config::load_config(&config_path)?;
let client = s2_client(cfg.token).await?;
let client = s2_client(cfg.auth_token).await?;
match action {
BasinActions::ListStreams {
basin,
Expand Down

0 comments on commit aaf0ff9

Please sign in to comment.