Skip to content

Commit

Permalink
feat: allow S2_CELL to be set
Browse files Browse the repository at this point in the history
  • Loading branch information
infiniteregrets committed Nov 7, 2024
1 parent 961b3b4 commit 74229ac
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
11 changes: 6 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ use thiserror::Error;

use crate::error::S2CliError;

#[derive(Debug, Deserialize, Serialize)]
#[derive(Debug, Deserialize, Serialize, Default)]
pub struct S2Config {
pub auth_token: String,
pub cell: String,
#[serde(skip_serializing)]
pub basin_zone: Option<String>,
}

#[cfg(target_os = "windows")]
Expand Down Expand Up @@ -41,14 +44,12 @@ pub fn load_config(path: &Path) -> Result<S2Config, S2ConfigError> {
Ok(builder.build()?.try_deserialize::<S2Config>()?)
}

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

pub fn create_config(config_path: &PathBuf, config: S2Config) -> Result<(), S2ConfigError> {
if let Some(parent) = config_path.parent() {
std::fs::create_dir_all(parent).map_err(S2ConfigError::WriteError)?;
}

let toml = toml::to_string(&cfg).unwrap();
let toml = toml::to_string(&config).unwrap();
std::fs::write(config_path, toml).map_err(S2ConfigError::WriteError)?;

Ok(())
Expand Down
43 changes: 30 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use std::path::PathBuf;

use account::AccountService;
use basin::BasinService;
use clap::{builder::styling, Parser, Subcommand};
use clap::{builder::styling, ArgGroup, Parser, Subcommand};
use colored::*;
use config::{config_path, create_config};
use config::{config_path, create_config, load_config, S2Config};
use error::S2CliError;
use stream::{RecordStream, StreamService, StreamServiceError};
use streamstore::{
Expand Down Expand Up @@ -85,11 +85,22 @@ enum Commands {

#[derive(Subcommand, Debug)]
enum ConfigActions {
/// Set the authentication token to be reused in subsequent commands.
/// Alternatively, use the S2_AUTH_TOKEN environment variable.
/// Set s2 configuration options.
#[command(group(
ArgGroup::new("config")
.required(true)
.args(["auth_token", "cell"]),
))]
Set {
#[arg(short, long)]
auth_token: String,
/// Set the authentication token to be reused in subsequent commands.
/// Alternatively, use the S2_AUTH_TOKEN environment variable.
#[arg(short, long, group = "config")]
auth_token: Option<String>,

/// Set the cell endpoint to be reused in subsequent commands.
/// Alternatively, use the S2_CELL environment variable.
#[arg(short, long, group = "config")]
cell: Option<String>,
},
}

Expand Down Expand Up @@ -296,13 +307,19 @@ async fn run() -> Result<(), S2CliError> {

match commands.command {
Commands::Config { action } => match action {
ConfigActions::Set { auth_token } => {
create_config(&config_path, auth_token)?;
eprintln!("{}", "✓ Token set successfully".green().bold());
eprintln!(
" Configuration saved to: {}",
config_path.display().to_string().cyan()
);
ConfigActions::Set { auth_token, cell } => {
let existing_config =
load_config(&config_path).unwrap_or_else(|_| S2Config::default());

let new_config = S2Config {
auth_token: auth_token.unwrap_or(existing_config.auth_token),
cell: cell.unwrap_or(existing_config.cell),
basin_zone: existing_config.basin_zone,
};

create_config(&config_path, new_config)?;
eprintln!("{}", "✓ Confg set successfully".green().bold());
eprintln!(" Saved to: {}", config_path.display().to_string().cyan());
}
},

Expand Down

0 comments on commit 74229ac

Please sign in to comment.