1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use account_address::*;
use aptos_crypto::ed25519::{Ed25519PrivateKey, Ed25519PublicKey};
use serde::{Deserialize, Serialize};
use std::{
collections::HashMap,
path::{Path, PathBuf},
};
use utils::{from_yaml, open_file, read_from_file, write_to_user_only_file};
pub mod error;
pub mod utils;
pub use error::*;
#[derive(Debug, Serialize, Deserialize)]
pub struct CliConfig {
pub profiles: Option<HashMap<String, ProfileConfig>>,
}
const CONFIG_FILE: &str = "config.yaml";
const LEGACY_CONFIG_FILE: &str = "config.yml";
const CONFIG_FOLDER: &str = ".aptos";
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct ProfileConfig {
pub private_key: Option<Ed25519PrivateKey>,
pub public_key: Option<Ed25519PublicKey>,
pub account: Option<AccountAddress>,
pub rest_url: Option<String>,
pub faucet_url: Option<String>,
}
impl Default for CliConfig {
fn default() -> Self {
CliConfig {
profiles: Some(HashMap::new()),
}
}
}
impl CliConfig {
pub fn config_exists() -> bool {
if let Ok(folder) = Self::aptos_folder() {
let config_file = folder.join(CONFIG_FILE);
let old_config_file = folder.join(LEGACY_CONFIG_FILE);
config_file.exists() || old_config_file.exists()
} else {
false
}
}
pub fn load() -> CliTypedResult<Self> {
let folder = Self::aptos_folder()?;
let config_file = folder.join(CONFIG_FILE);
let old_config_file = folder.join(LEGACY_CONFIG_FILE);
if config_file.exists() {
from_yaml(
&String::from_utf8(read_from_file(config_file.as_path())?)
.map_err(CliError::from)?,
)
} else if old_config_file.exists() {
from_yaml(
&String::from_utf8(read_from_file(old_config_file.as_path())?)
.map_err(CliError::from)?,
)
} else {
Err(CliError::ConfigNotFoundError(format!(
"{}",
config_file.display()
)))
}
}
pub fn load_profile(profile: &str) -> CliTypedResult<Option<ProfileConfig>> {
let mut config = Self::load()?;
Ok(config.remove_profile(profile))
}
pub fn remove_profile(&mut self, profile: &str) -> Option<ProfileConfig> {
if let Some(ref mut profiles) = self.profiles {
profiles.remove(&profile.to_string())
} else {
None
}
}
pub fn save(&self) -> CliTypedResult<()> {
let aptos_folder = Self::aptos_folder()?;
if !aptos_folder.exists() {
std::fs::create_dir(&aptos_folder).map_err(|err| {
CliError::CommandArgumentError(format!(
"Unable to create {} directory {}",
aptos_folder.display(),
err
))
})?;
}
let config_file = aptos_folder.join(CONFIG_FILE);
let config_bytes = self.try_to_string()?;
write_to_user_only_file(&config_file, CONFIG_FILE, config_bytes.as_bytes())?;
let legacy_config_file = aptos_folder.join(LEGACY_CONFIG_FILE);
if legacy_config_file.exists() {
eprintln!("Removing legacy config file {}", LEGACY_CONFIG_FILE);
let _ = std::fs::remove_file(legacy_config_file);
}
Ok(())
}
fn aptos_folder() -> CliTypedResult<PathBuf> {
std::env::current_dir()
.map_err(|err| {
CliError::UnexpectedError(format!("Unable to get current directory {}", err))
})
.map(|dir| dir.join(CONFIG_FOLDER))
}
pub fn load_from_path(path: &Path) -> CliTypedResult<CliConfig> {
Ok(serde_yaml::from_reader(open_file(path)?)?)
}
pub fn load_from_root(root: &Path) -> CliTypedResult<CliConfig> {
Self::load_from_path(&root.join(CONFIG_FOLDER).join(CONFIG_FILE))
}
pub fn try_to_string(&self) -> CliTypedResult<String> {
serde_yaml::to_string(&self)
.map_err(|err| CliError::UnexpectedError(format!("Failed to serialize config {}", err)))
}
}