2019-03-20 16:12:50 -07:00
|
|
|
use crate::update_manifest::UpdateManifest;
|
2019-03-15 10:54:54 -07:00
|
|
|
use serde_derive::{Deserialize, Serialize};
|
|
|
|
use solana_sdk::pubkey::Pubkey;
|
|
|
|
use std::fs::{create_dir_all, File};
|
2019-03-20 16:12:50 -07:00
|
|
|
use std::io::{self, Write};
|
|
|
|
use std::path::{Path, PathBuf};
|
2019-03-15 10:54:54 -07:00
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Default, Debug, PartialEq)]
|
|
|
|
pub struct Config {
|
|
|
|
pub json_rpc_url: String,
|
2019-03-20 16:12:50 -07:00
|
|
|
pub update_manifest_pubkey: Pubkey,
|
|
|
|
pub current_update_manifest: Option<UpdateManifest>,
|
|
|
|
pub update_poll_secs: u64,
|
|
|
|
releases_dir: PathBuf,
|
|
|
|
bin_dir: PathBuf,
|
2019-03-15 10:54:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Config {
|
2019-03-20 16:12:50 -07:00
|
|
|
pub fn new(data_dir: &str, json_rpc_url: &str, update_manifest_pubkey: &Pubkey) -> Self {
|
|
|
|
Self {
|
|
|
|
json_rpc_url: json_rpc_url.to_string(),
|
|
|
|
update_manifest_pubkey: *update_manifest_pubkey,
|
|
|
|
current_update_manifest: None,
|
|
|
|
update_poll_secs: 60, // check for updates once a minute
|
|
|
|
releases_dir: PathBuf::from(data_dir).join("releases"),
|
|
|
|
bin_dir: PathBuf::from(data_dir).join("bin"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn _load(config_file: &str) -> Result<Self, io::Error> {
|
2019-03-15 10:54:54 -07:00
|
|
|
let file = File::open(config_file.to_string())?;
|
|
|
|
let config = serde_yaml::from_reader(file)
|
2019-03-20 16:12:50 -07:00
|
|
|
.map_err(|err| io::Error::new(io::ErrorKind::Other, format!("{:?}", err)))?;
|
2019-03-15 10:54:54 -07:00
|
|
|
Ok(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn load(config_file: &str) -> Result<Self, String> {
|
|
|
|
Self::_load(config_file).map_err(|err| format!("Unable to load {}: {:?}", config_file, err))
|
|
|
|
}
|
|
|
|
|
2019-03-20 16:12:50 -07:00
|
|
|
fn _save(&self, config_file: &str) -> Result<(), io::Error> {
|
2019-03-15 10:54:54 -07:00
|
|
|
let serialized = serde_yaml::to_string(self)
|
2019-03-20 16:12:50 -07:00
|
|
|
.map_err(|err| io::Error::new(io::ErrorKind::Other, format!("{:?}", err)))?;
|
2019-03-15 10:54:54 -07:00
|
|
|
|
|
|
|
if let Some(outdir) = Path::new(&config_file).parent() {
|
|
|
|
create_dir_all(outdir)?;
|
|
|
|
}
|
|
|
|
let mut file = File::create(config_file)?;
|
|
|
|
file.write_all(&serialized.into_bytes())?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn save(&self, config_file: &str) -> Result<(), String> {
|
|
|
|
self._save(config_file)
|
|
|
|
.map_err(|err| format!("Unable to save {}: {:?}", config_file, err))
|
|
|
|
}
|
2019-03-20 16:12:50 -07:00
|
|
|
|
|
|
|
pub fn bin_dir(&self) -> &PathBuf {
|
|
|
|
&self.bin_dir
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn release_dir(&self, release_sha256: &str) -> PathBuf {
|
|
|
|
self.releases_dir.join(release_sha256)
|
|
|
|
}
|
2019-03-15 10:54:54 -07:00
|
|
|
}
|