Skip to main content

spl_forge/common/
init.rs

1use crate::{commands::config::ConfigData, common::paths::path};
2use crate::common::theme;
3use anyhow::Result;
4use solana_sdk::signer::{keypair::Keypair, Signer};
5use std::path::PathBuf;
6
7pub fn ensure_config_exists() -> Result<()> {
8    let config_path = path()?;
9    if !config_path.exists() {
10        run_first_time_setup(&config_path)?;
11    }
12    Ok(())
13}
14
15fn run_first_time_setup(config_path: &PathBuf) -> Result<()> {
16    println!();
17    println!("{}", theme::heading("Welcome to spl-forge! Performing first-time setup..."));
18    println!();
19
20    let config = ConfigData::default_values();
21    let keypair = Keypair::new();
22    let keypair_path = PathBuf::from(&config.keypair_path);
23
24    if let Some(parent) = keypair_path.parent() {
25        std::fs::create_dir_all(parent)?;
26    }
27
28    solana_sdk::signer::keypair::write_keypair_file(
29        &keypair,
30        &keypair_path,
31    )
32    .map_err(|e| anyhow::anyhow!("Failed to write new keypair file: {}", e))?;
33
34    println!(
35        "A new keypair has been created for you at: {}",
36        theme::label(&keypair_path.to_string_lossy())
37    );
38    println!(
39        "   Your new public key is: {}",
40        theme::success(&keypair.pubkey().to_string())
41    );
42
43    config.save()?;
44    println!(
45        "Default configuration file created at: {}",
46        theme::label(&config_path.to_string_lossy())
47    );
48
49    println!();
50    println!("{}", theme::tip("Tip: To use your existing Solana CLI wallet, run:"));
51    println!("{}", theme::command("spl-forge config set --keypair solana-cli"));
52    println!();
53
54    Ok(())
55}