forked from stellar/stellar-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feat/sign_and_send' into feat/sign_with_lab
- Loading branch information
Showing
11 changed files
with
393 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
use std::collections::HashMap; | ||
use std::fmt::Debug; | ||
use std::{fs, process}; | ||
|
||
use clap::{command, Parser}; | ||
|
||
use crate::commands::config::network; | ||
use crate::config::{alias, locator}; | ||
|
||
#[derive(Parser, Debug, Clone)] | ||
#[group(skip)] | ||
pub struct Cmd { | ||
#[command(flatten)] | ||
pub config_locator: locator::Args, | ||
} | ||
|
||
#[derive(thiserror::Error, Debug)] | ||
pub enum Error { | ||
#[error(transparent)] | ||
Locator(#[from] locator::Error), | ||
|
||
#[error(transparent)] | ||
Network(#[from] network::Error), | ||
|
||
#[error(transparent)] | ||
PatternError(#[from] glob::PatternError), | ||
|
||
#[error(transparent)] | ||
GlobError(#[from] glob::GlobError), | ||
|
||
#[error(transparent)] | ||
IoError(#[from] std::io::Error), | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
struct AliasEntry { | ||
alias: String, | ||
contract: String, | ||
} | ||
|
||
impl Cmd { | ||
pub fn run(&self) -> Result<(), Error> { | ||
let config_dir = self.config_locator.config_dir()?; | ||
let pattern = config_dir | ||
.join("contract-ids") | ||
.join("*.json") | ||
.to_string_lossy() | ||
.into_owned(); | ||
|
||
let paths = glob::glob(&pattern)?; | ||
let mut found = false; | ||
let mut map: HashMap<String, Vec<AliasEntry>> = HashMap::new(); | ||
|
||
for path in paths { | ||
let path = path?; | ||
|
||
if let Some(alias) = path.file_stem() { | ||
let alias = alias.to_string_lossy().into_owned(); | ||
let content = fs::read_to_string(path)?; | ||
let data: alias::Data = serde_json::from_str(&content).unwrap_or_default(); | ||
|
||
for network_passphrase in data.ids.keys() { | ||
let network_passphrase = network_passphrase.to_string(); | ||
let contract = data | ||
.ids | ||
.get(&network_passphrase) | ||
.map(ToString::to_string) | ||
.unwrap_or_default(); | ||
let entry = AliasEntry { | ||
alias: alias.clone(), | ||
contract, | ||
}; | ||
|
||
let list = map.entry(network_passphrase.clone()).or_default(); | ||
|
||
list.push(entry.clone()); | ||
} | ||
} | ||
} | ||
|
||
for network_passphrase in map.keys() { | ||
if let Some(list) = map.clone().get_mut(network_passphrase) { | ||
println!("ℹ️ Aliases available for network '{network_passphrase}'"); | ||
|
||
list.sort_by(|a, b| a.alias.cmp(&b.alias)); | ||
|
||
for entry in list { | ||
found = true; | ||
println!("{}: {}", entry.alias, entry.contract); | ||
} | ||
|
||
println!(); | ||
} | ||
} | ||
|
||
if !found { | ||
eprintln!("⚠️ No aliases defined for network"); | ||
|
||
process::exit(1); | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
use crate::config::locator; | ||
use chrono::{DateTime, Utc}; | ||
use jsonrpsee_core::Serialize; | ||
use semver::Version; | ||
use serde::Deserialize; | ||
use serde_json; | ||
use std::fs; | ||
|
||
const FILE_NAME: &str = "upgrade_check.json"; | ||
|
||
/// The `UpgradeCheck` struct represents the state of the upgrade check. | ||
/// This state is global and stored in the `upgrade_check.json` file in | ||
/// the global configuration directory. | ||
#[derive(Serialize, Deserialize, Debug, PartialEq)] | ||
pub struct UpgradeCheck { | ||
/// The time of the latest check for a new version of the CLI. | ||
pub latest_check_time: DateTime<Utc>, | ||
/// The latest stable version of the CLI available on crates.io. | ||
pub max_stable_version: Version, | ||
/// The latest version of the CLI available on crates.io, including pre-releases. | ||
pub max_version: Version, | ||
} | ||
|
||
impl Default for UpgradeCheck { | ||
fn default() -> Self { | ||
Self { | ||
latest_check_time: DateTime::<Utc>::UNIX_EPOCH, | ||
max_stable_version: Version::new(0, 0, 0), | ||
max_version: Version::new(0, 0, 0), | ||
} | ||
} | ||
} | ||
|
||
impl UpgradeCheck { | ||
/// Loads the state of the upgrade check from the global configuration directory. | ||
/// If the file doesn't exist, returns a default instance of `UpgradeCheck`. | ||
pub fn load() -> Result<Self, locator::Error> { | ||
let path = locator::global_config_path()?.join(FILE_NAME); | ||
if !path.exists() { | ||
return Ok(Self::default()); | ||
} | ||
let data = fs::read(&path) | ||
.map_err(|error| locator::Error::UpgradeCheckReadFailed { path, error })?; | ||
Ok(serde_json::from_slice(data.as_slice())?) | ||
} | ||
|
||
/// Saves the state of the upgrade check to the `upgrade_check.json` file in the global configuration directory. | ||
pub fn save(&self) -> Result<(), locator::Error> { | ||
let path = locator::global_config_path()?.join(FILE_NAME); | ||
let path = locator::ensure_directory(path)?; | ||
let data = serde_json::to_string(self).map_err(|_| locator::Error::ConfigSerialization)?; | ||
fs::write(&path, data) | ||
.map_err(|error| locator::Error::UpgradeCheckWriteFailed { path, error }) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use std::env; | ||
|
||
#[test] | ||
fn test_upgrade_check_load_save() { | ||
// Set the `XDG_CONFIG_HOME` environment variable to a temporary directory | ||
let temp_dir = tempfile::tempdir().unwrap(); | ||
env::set_var("XDG_CONFIG_HOME", temp_dir.path()); | ||
// Test default loading | ||
let default_check = UpgradeCheck::load().unwrap(); | ||
assert_eq!(default_check, UpgradeCheck::default()); | ||
assert_eq!( | ||
default_check.latest_check_time, | ||
DateTime::<Utc>::from_timestamp_millis(0).unwrap() | ||
); | ||
assert_eq!(default_check.max_stable_version, Version::new(0, 0, 0)); | ||
|
||
// Test saving and loading | ||
let saved_check = UpgradeCheck { | ||
latest_check_time: DateTime::<Utc>::from_timestamp(1_234_567_890, 0).unwrap(), | ||
max_stable_version: Version::new(1, 2, 3), | ||
max_version: Version::parse("1.2.4-rc.1").unwrap(), | ||
}; | ||
saved_check.save().unwrap(); | ||
let loaded_check = UpgradeCheck::load().unwrap(); | ||
assert_eq!(loaded_check, saved_check); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.