Skip to content

Commit 49fc7be

Browse files
committed
feat: move config load-up -> Context:try_build
1 parent ae894a9 commit 49fc7be

File tree

2 files changed

+65
-63
lines changed

2 files changed

+65
-63
lines changed

cli/src/cli.rs

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

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

1415
#[derive(Parser)]
1516
#[command(
@@ -239,7 +240,68 @@ pub struct Context {
239240
}
240241

241242
impl Context{
242-
pub fn try_build(_cli:&Cli, config: &TapeConfig) -> Result<Self> {
243+
pub fn try_build(cli:&Cli) -> Result<Self> {
244+
245+
// 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+
};
304+
243305
let rpc_url = config.solana.rpc_url.to_string();
244306
let commitment_level = config.solana.commitment.to_commitment_config();
245307
let rpc = Arc::new(

cli/src/main.rs

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use env_logger::{self, Env};
1414
use tape_network::store::TapeStore;
1515

1616
use crate::cli::Context;
17-
use crate::config::{TapeConfig, TapeConfigError};
1817

1918

2019
fn main() -> Result<()>{
@@ -40,66 +39,7 @@ async fn run_tape_cli() -> Result<()> {
4039

4140
let cli = Cli::parse();
4241

43-
let config = match TapeConfig::load(&cli.config) {
44-
Ok(config) => config,
45-
Err(e) => match e {
46-
TapeConfigError::ConfigFileNotFound => {
47-
log::print_info("tape.toml not found, creating default configuration...");
48-
match TapeConfig::create_default() {
49-
Ok(config) => {
50-
log::print_info("✓ Default configuration created successfully");
51-
config
52-
},
53-
Err(creation_error) => {
54-
log::print_error(&format!("{}", creation_error));
55-
std::process::exit(1);
56-
}
57-
}
58-
},
59-
60-
TapeConfigError::CustomConfigFileNotFound(path) => {
61-
// This happens when user explicitly provided a path that doesn't exist
62-
log::print_error(&format!("Custom config file not found: {}", path));
63-
log::print_info("Please check the path and try again.");
64-
std::process::exit(1);
65-
},
66-
67-
TapeConfigError::InvalidUrl(msg) => {
68-
log::print_error(&format!("URL Configuration Error: {}", msg));
69-
log::print_info("Please fix the URL in your tape.toml file and try again.");
70-
std::process::exit(1);
71-
},
72-
73-
TapeConfigError::KeypairNotFound(path) => {
74-
log::print_error(&format!("Keypair not found at path: {}", path));
75-
log::print_info("Please ensure the keypair file exists at the specified path in tape.toml");
76-
std::process::exit(1);
77-
},
78-
79-
TapeConfigError::FileReadError(io_err) => {
80-
log::print_error(&format!("Could not read config file: {}", io_err));
81-
std::process::exit(1);
82-
},
83-
84-
TapeConfigError::ParseError(parse_err) => {
85-
log::print_error(&format!("Invalid tape.toml format: {}", parse_err));
86-
log::print_info("Please check your tape.toml file syntax.");
87-
std::process::exit(1);
88-
},
89-
90-
TapeConfigError::HomeDirectoryNotFound => {
91-
log::print_error("Could not determine home directory");
92-
std::process::exit(1);
93-
},
94-
95-
TapeConfigError::DefaultConfigCreationFailed(msg) => {
96-
log::print_error(&format!("Failed to create default config: {}", msg));
97-
std::process::exit(1);
98-
},
99-
}
100-
};
101-
102-
let context = Context::try_build(&cli, &config)?;
42+
let context = Context::try_build(&cli)?;
10343

10444
match cli.command {
10545
Commands::Init {} |

0 commit comments

Comments
 (0)