Skip to content

Commit

Permalink
Syntax config path -> config path
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrgn committed Sep 15, 2018
1 parent 48dbf94 commit 6651f7f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 41 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ These are the clients I tried but failed to compile or run:
-o --os <type> Override the operating system [linux, osx, sunos]
-u --update Update the local cache
-c --clear-cache Clear the local cache
--config-path Show config directory path
--config-path Show config file path
--seed-config Create a basic config

Examples:
Expand Down Expand Up @@ -135,7 +135,7 @@ Creating the config file can be done manually or with the help of tldr:

$ tldr --seed-config

The command should print the location where the command created the config file. Alternatively the path can also be queried:
The configuration file path follows OS conventions. It can be queried with the following command:

$ tldr --config-path

Expand Down
46 changes: 23 additions & 23 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use xdg::BaseDirectories;

use error::TealdeerError::{self, ConfigError};

pub const SYNTAX_CONFIG_FILE_NAME: &'static str = "config.toml";
pub const CONFIG_FILE_NAME: &'static str = "config.toml";

fn default_underline() -> bool {
false
Expand Down Expand Up @@ -161,19 +161,19 @@ fn map_io_err_to_config_err(e: IoError) -> TealdeerError {

impl Config {
pub fn load() -> Result<Config, TealdeerError> {
let raw_config = match get_syntax_config_path() {
Ok(syntax_config_file_path) => {
let mut syntax_config_file = fs::File::open(syntax_config_file_path)
let raw_config = match get_config_path() {
Ok(config_file_path) => {
let mut config_file = fs::File::open(config_file_path)
.map_err(map_io_err_to_config_err)?;
let mut contents = String::new();
let _rc = syntax_config_file.read_to_string(&mut contents)
let _rc = config_file.read_to_string(&mut contents)
.map_err(map_io_err_to_config_err)?;

toml::from_str(&contents).map_err(|err| ConfigError(format!("Failed to parse syntax config file: {}", err)))?
toml::from_str(&contents).map_err(|err| ConfigError(format!("Failed to parse config file: {}", err)))?
}
Err(ConfigError(_)) => RawConfig::new(),
Err(_) => {
return Err(ConfigError("Unknown error while looking up syntax config path".into()));
return Err(ConfigError("Unknown error while looking up config path".into()));
}
};

Expand Down Expand Up @@ -206,44 +206,44 @@ pub fn get_config_dir() -> Result<PathBuf, TealdeerError> {
Ok(xdg_dirs.get_config_home())
}

/// Return the path to the syntax config file.
pub fn get_syntax_config_path() -> Result<PathBuf, TealdeerError> {
/// Return the path to the config file.
pub fn get_config_path() -> Result<PathBuf, TealdeerError> {
let config_dir = get_config_dir()?;
let syntax_config_file_path = config_dir.join(SYNTAX_CONFIG_FILE_NAME);
let config_file_path = config_dir.join(CONFIG_FILE_NAME);

if syntax_config_file_path.is_file() {
Ok(syntax_config_file_path)
if config_file_path.is_file() {
Ok(config_file_path)
} else {
Err(ConfigError(format!("{} is not a file path", syntax_config_file_path.to_str().unwrap())))
Err(ConfigError(format!("{} is not a file path", config_file_path.to_str().unwrap())))
}
}

/// Create default syntax config file.
pub fn make_default_syntax_config() -> Result<PathBuf, TealdeerError> {
/// Create default config file.
pub fn make_default_config() -> Result<PathBuf, TealdeerError> {
let config_dir = get_config_dir()?;
if !config_dir.is_dir() {
if let Err(e) = fs::create_dir_all(&config_dir) {
return Err(ConfigError(format!("Could not create config directory: {}", e)));
}
}

let syntax_config_file_path = config_dir.join(SYNTAX_CONFIG_FILE_NAME);
if syntax_config_file_path.is_file() {
let config_file_path = config_dir.join(CONFIG_FILE_NAME);
if config_file_path.is_file() {
return Err(ConfigError(format!(
"A configuration file already exists at {}, no action was taken.",
syntax_config_file_path.to_str().unwrap()
config_file_path.to_str().unwrap()
)));
}

let serialized_syntax_config = toml::to_string(&RawConfig::new())
.map_err(|err| ConfigError(format!("Failed to serialize default syntax config: {}", err)))?;
let serialized_config = toml::to_string(&RawConfig::new())
.map_err(|err| ConfigError(format!("Failed to serialize default config: {}", err)))?;

let mut syntax_config_file = fs::File::create(&syntax_config_file_path)
let mut config_file = fs::File::create(&config_file_path)
.map_err(map_io_err_to_config_err)?;
let _wc = syntax_config_file.write(serialized_syntax_config.as_bytes())
let _wc = config_file.write(serialized_config.as_bytes())
.map_err(map_io_err_to_config_err)?;

Ok(syntax_config_file_path)
Ok(config_file_path)
}

#[test]
Expand Down
18 changes: 9 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ mod error;

use tokenizer::Tokenizer;
use cache::Cache;
use config::{get_config_dir, make_default_syntax_config, Config};
use config::{get_config_path, make_default_config, Config};
use error::TealdeerError::{CacheError, ConfigError, UpdateError};
use formatter::print_lines;
use types::OsType;
Expand All @@ -75,7 +75,7 @@ Options:
-o --os <type> Override the operating system [linux, osx, sunos]
-u --update Update the local cache
-c --clear-cache Clear the local cache
--config-path Show config directory path
--config-path Show config file path
--seed-config Create a basic config
Examples:
Expand Down Expand Up @@ -144,7 +144,7 @@ fn check_cache(args: &Args, cache: &Cache) {
Some(ago) if ago > MAX_CACHE_AGE => {
println!("{}", Color::Red.paint(format!(
"Cache wasn't updated in {} days.\n\
You should probably run `tldr --update` soon.\n",
You should probably run `tldr --update` soon.",
MAX_CACHE_AGE / 24 / 3600
)));
},
Expand Down Expand Up @@ -275,13 +275,13 @@ fn main() {

// Show config file and path and exit
if args.flag_config_path {
match get_config_dir() {
match get_config_path() {
Ok(config_file_path) => {
println!("Config directory path is: {}", config_file_path.to_str().unwrap());
println!("Config path is: {}", config_file_path.to_str().unwrap());
process::exit(0);
},
Err(ConfigError(msg)) => {
eprintln!("Could not look up syntax_config_path: {}", msg);
eprintln!("Could not look up config_path: {}", msg);
process::exit(1);
},
Err(_) => {
Expand All @@ -293,9 +293,9 @@ fn main() {

// Create a basic config and exit
if args.flag_seed_config {
match make_default_syntax_config() {
Ok(syntax_config_file_path) => {
println!("Successfully created seed syntax config file here: {}", syntax_config_file_path.to_str().unwrap());
match make_default_config() {
Ok(config_file_path) => {
println!("Successfully created seed config file here: {}", config_file_path.to_str().unwrap());
process::exit(0);
},
Err(ConfigError(msg)) => {
Expand Down
14 changes: 7 additions & 7 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fn test_setup_seed_config() {
testenv.assert()
.with_args(&["--seed-config"])
.succeeds()
.stdout().contains("Successfully created seed syntax config file")
.stdout().contains("Successfully created seed config file")
.unwrap();
}

Expand Down Expand Up @@ -114,13 +114,13 @@ fn test_correct_rendering_v2() {
fn test_correct_rendering_with_config() {
let testenv = TestEnv::new();

// Setup syntax config file
// TODO should be config::SYNTAX_CONFIG_FILE_NAME
let syntax_file_path = testenv.config_dir.path().join("config.toml");
println!("Syntax config path: {:?}", &syntax_file_path);
// Setup config file
// TODO should be config::CONFIG_FILE_NAME
let config_file_path = testenv.config_dir.path().join("config.toml");
println!("Config path: {:?}", &config_file_path);

let mut syntax_config_file = File::create(&syntax_file_path).unwrap();
syntax_config_file.write(include_str!("config.toml").as_bytes()).unwrap();
let mut config_file = File::create(&config_file_path).unwrap();
config_file.write(include_str!("config.toml").as_bytes()).unwrap();

// Create input file
let file_path = testenv.input_dir.path().join("inkscape-v2.md");
Expand Down

0 comments on commit 6651f7f

Please sign in to comment.