nushell/src/object/config.rs

66 lines
1.9 KiB
Rust
Raw Normal View History

use crate::errors::ShellError;
use crate::prelude::*;
use app_dirs::*;
use indexmap::IndexMap;
use log::trace;
use serde_derive::{Deserialize, Serialize};
use std::fs::{self, OpenOptions};
use std::io;
use std::path::Path;
const APP_INFO: AppInfo = AppInfo {
name: "nu",
author: "nu shell developers",
};
#[derive(Deserialize, Serialize)]
struct Config {
#[serde(flatten)]
2019-08-01 03:58:42 +02:00
extra: IndexMap<String, Tagged<Value>>,
}
2019-08-01 03:58:42 +02:00
crate fn write_config(config: &IndexMap<String, Tagged<Value>>) -> Result<(), ShellError> {
let location = app_root(AppDataType::UserConfig, &APP_INFO)
.map_err(|err| ShellError::string(&format!("Couldn't open config file:\n{}", err)))?;
let filename = location.join("config.toml");
touch(&filename)?;
2019-07-08 18:44:53 +02:00
let contents = toml::to_string(&Config {
2019-07-09 06:31:26 +02:00
extra: config.iter().map(|(k, v)| (k.clone(), v.clone())).collect(),
2019-07-08 18:44:53 +02:00
})?;
fs::write(&filename, &contents)?;
Ok(())
}
2019-08-01 03:58:42 +02:00
crate fn config(span: impl Into<Span>) -> Result<IndexMap<String, Tagged<Value>>, ShellError> {
2019-07-08 18:44:53 +02:00
let span = span.into();
let location = app_root(AppDataType::UserConfig, &APP_INFO)
.map_err(|err| ShellError::string(&format!("Couldn't open config file:\n{}", err)))?;
let filename = location.join("config.toml");
touch(&filename)?;
trace!("config file = {}", filename.display());
let contents = fs::read_to_string(filename)
2019-08-01 03:58:42 +02:00
.map(|v| v.tagged(span))
.map_err(|err| ShellError::string(&format!("Couldn't read config file:\n{}", err)))?;
let parsed: Config = toml::from_str(&contents)
.map_err(|err| ShellError::string(&format!("Couldn't parse config file:\n{}", err)))?;
Ok(parsed.extra)
}
// A simple implementation of `% touch path` (ignores existing files)
fn touch(path: &Path) -> io::Result<()> {
match OpenOptions::new().create(true).write(true).open(path) {
Ok(_) => Ok(()),
Err(e) => Err(e),
}
}