fix: default config paths agnostic

This commit is contained in:
Oleksandr Pravdyvyi 2025-10-30 15:33:58 +02:00
parent 813868f16f
commit 2c21abc2d9
No known key found for this signature in database
GPG Key ID: 9F8955C63C443871
2 changed files with 30 additions and 25 deletions

View File

@ -1,10 +1,7 @@
use base64::{Engine, engine::general_purpose::STANDARD as BASE64};
use nssa_core::account::Nonce;
use rand::{RngCore, rngs::OsRng};
use std::{
path::{Path, PathBuf},
str::FromStr,
};
use std::{path::PathBuf, str::FromStr};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use anyhow::Result;
@ -20,18 +17,29 @@ use crate::{
};
/// Get home dir for wallet. Env var `NSSA_WALLET_HOME_DIR` must be set before execution to succeed.
pub fn get_home() -> Result<PathBuf> {
pub fn get_home_nssa_var() -> Result<PathBuf> {
Ok(PathBuf::from_str(&std::env::var(HOME_DIR_ENV_VAR)?)?)
}
pub fn get_home_default_path_linux() -> PathBuf {
let home = std::env::var("HOME").unwrap();
Path::new(&home).join(".nssa").join("wallet")
/// Get home dir for wallet. Env var `HOME` must be set before execution to succeed.
pub fn get_home_default_path() -> Result<PathBuf> {
std::env::home_dir()
.map(|path| path.join(".nssa").join("wallet"))
.ok_or(anyhow::anyhow!("Failed to get HOME"))
}
/// Fetch config from `NSSA_WALLET_HOME_DIR`
pub async fn fetch_config_default_path_linux() -> Result<WalletConfig> {
let config_home = get_home_default_path_linux();
/// Get home dir for wallet.
pub fn get_home() -> Result<PathBuf> {
if let Ok(home) = get_home_nssa_var() {
Ok(home)
} else {
get_home_default_path()
}
}
/// Fetch config from default home
pub async fn fetch_config() -> Result<WalletConfig> {
let config_home = get_home()?;
let mut config_needs_setup = false;
let config = match tokio::fs::OpenOptions::new()
@ -79,15 +87,7 @@ pub async fn fetch_config_default_path_linux() -> Result<WalletConfig> {
Ok(config)
}
/// Fetch config from `NSSA_WALLET_HOME_DIR`
pub async fn fetch_config() -> Result<WalletConfig> {
let config_home = get_home()?;
let config_contents = tokio::fs::read(config_home.join("wallet_config.json")).await?;
Ok(serde_json::from_slice(&config_contents)?)
}
/// Fetch data stored at `NSSA_WALLET_HOME_DIR/storage.json`
/// Fetch data stored at home
///
/// If file not present, it is considered as empty list of persistent accounts
pub async fn fetch_persistent_storage() -> Result<PersistentStorage> {

View File

@ -28,7 +28,7 @@ use crate::{
token_program::TokenProgramAgnosticSubcommand,
},
config::PersistentStorage,
helperfunctions::{fetch_config_default_path_linux, fetch_persistent_storage},
helperfunctions::fetch_persistent_storage,
};
use crate::{
helperfunctions::{fetch_config, get_home, produce_data_for_storage},
@ -216,7 +216,10 @@ pub enum Command {
/// Check the wallet can connect to the node and builtin local programs
/// match the remote versions
CheckHealth {},
TestCommand {},
/// Command to explicitly setup config and storage
///
/// Does nothing in case if both already present
Setup {},
}
///To execute commands, env var NSSA_WALLET_HOME_DIR must be set into directory with config
@ -246,7 +249,7 @@ pub enum SubcommandReturnValue {
}
pub async fn execute_subcommand(command: Command) -> Result<SubcommandReturnValue> {
let wallet_config = fetch_config_default_path_linux().await?;
let wallet_config = fetch_config().await?;
let mut wallet_core = WalletCore::start_from_config_update_chain(wallet_config).await?;
let subcommand_ret = match command {
@ -301,8 +304,10 @@ pub async fn execute_subcommand(command: Command) -> Result<SubcommandReturnValu
Command::Token(token_subcommand) => {
token_subcommand.handle_subcommand(&mut wallet_core).await?
}
Command::TestCommand {} => {
println!("Hello from test");
Command::Setup {} => {
let path = wallet_core.store_persistent_data().await?;
println!("Stored persistent accounts at {path:#?}");
SubcommandReturnValue::Empty
}