Skip to content

Commit 6db09f8

Browse files
committed
feat: reduce match block of config loadup
1 parent 596886a commit 6db09f8

File tree

2 files changed

+13
-61
lines changed

2 files changed

+13
-61
lines changed

cli/src/cli.rs

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ use std::path::PathBuf;
99
use std::sync::Arc;
1010

1111
use crate::keypair::{get_keypair_path, get_payer};
12-
use crate::config::{TapeConfig, TapeConfigError};
13-
use crate::log;
12+
use crate::config::TapeConfig;
1413

1514
#[derive(Parser)]
1615
#[command(
@@ -243,64 +242,7 @@ impl Context{
243242
pub fn try_build(cli:&Cli) -> Result<Self> {
244243

245244
// loading up configs
246-
let config = match TapeConfig::load(&cli.config) {
247-
Ok(config) => config,
248-
Err(e) => match e {
249-
TapeConfigError::ConfigFileNotFound => {
250-
log::print_info("tape.toml not found, creating default configuration...");
251-
match TapeConfig::create_default() {
252-
Ok(config) => {
253-
log::print_info("✓ Default configuration created successfully");
254-
config
255-
},
256-
Err(creation_error) => {
257-
log::print_error(&format!("{}", creation_error));
258-
std::process::exit(1);
259-
}
260-
}
261-
},
262-
263-
TapeConfigError::CustomConfigFileNotFound(path) => {
264-
// This happens when user explicitly provided a path that doesn't exist
265-
log::print_error(&format!("Custom config file not found: {}", path));
266-
log::print_info("Please check the path and try again.");
267-
std::process::exit(1);
268-
},
269-
270-
TapeConfigError::InvalidUrl(msg) => {
271-
log::print_error(&format!("URL Configuration Error: {}", msg));
272-
log::print_info("Please fix the URL in your tape.toml file and try again.");
273-
std::process::exit(1);
274-
},
275-
276-
TapeConfigError::KeypairNotFound(path) => {
277-
log::print_error(&format!("Keypair not found at path: {}", path));
278-
log::print_info("Please ensure the keypair file exists at the specified path in tape.toml");
279-
std::process::exit(1);
280-
},
281-
282-
TapeConfigError::FileReadError(io_err) => {
283-
log::print_error(&format!("Could not read config file: {}", io_err));
284-
std::process::exit(1);
285-
},
286-
287-
TapeConfigError::ParseError(parse_err) => {
288-
log::print_error(&format!("Invalid tape.toml format: {}", parse_err));
289-
log::print_info("Please check your tape.toml file syntax.");
290-
std::process::exit(1);
291-
},
292-
293-
TapeConfigError::HomeDirectoryNotFound => {
294-
log::print_error("Could not determine home directory");
295-
std::process::exit(1);
296-
},
297-
298-
TapeConfigError::DefaultConfigCreationFailed(msg) => {
299-
log::print_error(&format!("Failed to create default config: {}", msg));
300-
std::process::exit(1);
301-
},
302-
}
303-
};
245+
let config = TapeConfig::load(&cli.config)?;
304246

305247
let rpc_url = config.solana.rpc_url.to_string();
306248
let commitment_level = config.solana.commitment.to_commitment_config();

cli/src/config.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::fs;
33
use std::path::{Path, PathBuf};
44
use solana_sdk::commitment_config::CommitmentConfig;
55
use std::fmt;
6+
use crate::log::*;
67

78
#[derive(Debug, Serialize, Deserialize, Clone)]
89
pub struct TapeConfig {
@@ -117,7 +118,16 @@ impl TapeConfig {
117118
},
118119
None => {
119120
let default_path = get_default_config_path()?;
120-
Self::load_from_path(default_path)
121+
match Self::load_from_path(&default_path) {
122+
Ok(config) => Ok(config),
123+
Err(TapeConfigError::ConfigFileNotFound) => {
124+
print_info("tape.toml not found, creating default configuration...");
125+
let config = Self::create_default()?;
126+
print_info("✓ Default configuration created successfully");
127+
Ok(config)
128+
},
129+
Err(e) => Err(e),
130+
}
121131
}
122132
}
123133
}

0 commit comments

Comments
 (0)