Skip to content

Commit

Permalink
Add initial config file
Browse files Browse the repository at this point in the history
This will get written to the config file on initialization.
  • Loading branch information
rossmacarthur committed Apr 29, 2020
1 parent 5b63843 commit 75a39b3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/cli.rs
Expand Up @@ -31,7 +31,7 @@ const VERSION_MESSAGE: &str = "Show the version and exit";
group = ArgGroup::with_name("git-reference")
)]
struct Add {
/// A name for this plugin.
/// A unique name for this plugin.
#[structopt(value_name = "NAME")]
name: String,

Expand Down Expand Up @@ -103,7 +103,7 @@ enum RawCommand {
/// Remove a plugin from the config file.
#[structopt(help_message = HELP_MESSAGE)]
Remove {
/// A name for this plugin.
/// A unique name for this plugin.
#[structopt(value_name = "NAME")]
name: String,
},
Expand Down Expand Up @@ -536,7 +536,7 @@ OPTIONS:
--apply <TEMPLATE>... Templates to apply to this plugin
ARGS:
<NAME> A name for this plugin",
<NAME> A unique name for this plugin",
name = crate_name!(),
version = crate_version!()
)
Expand Down
12 changes: 9 additions & 3 deletions src/edit.rs
Expand Up @@ -13,7 +13,7 @@ pub struct Plugin {
}

/// An editable config.
#[derive(Debug, Default)]
#[derive(Debug)]
pub struct Config {
/// The parsed TOML version of the config.
doc: toml_edit::Document,
Expand All @@ -25,6 +25,12 @@ impl From<RawPlugin> for Plugin {
}
}

impl Default for Config {
fn default() -> Self {
Self::from_str(include_str!("plugins.toml")).unwrap()
}
}

impl fmt::Display for Config {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.doc)
Expand Down Expand Up @@ -57,10 +63,10 @@ impl Config {

/// Add a new plugin.
pub fn add(&mut self, name: &str, plugin: Plugin) -> Result<()> {
let content =
let contents =
toml::to_string_pretty(&plugin.inner).expect("failed to serialize plugin as TOML");

let mini = content
let mini = contents
.parse::<toml_edit::Document>()
.expect("failed to parse valid TOML");

Expand Down
2 changes: 1 addition & 1 deletion src/editor.rs
Expand Up @@ -101,7 +101,7 @@ impl Child {
let contents =
fs::read_to_string(&file.path).context("failed to read from temporary file")?;
if contents == original_contents {
bail!("Aborted editing!");
bail!("aborted editing!");
} else {
edit::Config::from_str(&contents)
.context("edited config is invalid, not updating config file")
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Expand Up @@ -38,6 +38,7 @@ use crate::{

/// The main application.
#[derive(Debug)]
#[doc(hidden)]
pub struct Sheldon;

impl Sheldon {
Expand All @@ -48,7 +49,7 @@ impl Sheldon {
"Initialize new config file `{}`?",
&ctx.replace_home(path).display()
)) {
bail!("Aborted initialization!");
bail!("aborted initialization!");
};
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).with_context(s!(
Expand Down Expand Up @@ -92,6 +93,8 @@ impl Sheldon {
}
Err(err) => {
let config = Self::init_config(ctx, path, err)?;
config.to_path(path)?;
header!(ctx, "Initialized", path);
config.to_string()
}
};
Expand Down
18 changes: 18 additions & 0 deletions src/plugins.toml
@@ -0,0 +1,18 @@
# `sheldon` configuration file
# ----------------------------
#
# You can modify this file directly or you can use one of the following
# `sheldon` commands which are provided to assist in editing the config file:
#
# - `sheldon add` to add a new plugin to the config file
# - `sheldon edit` to open up the config file in the default editor
# - `sheldon remove` to remove a plugin from the config file
#
# See the documentation for more https://github.com/rossmacarthur/sheldon#readme

[plugins]

# For example:
#
# [plugins.pure]
# github = "sindresorhus/pure"

0 comments on commit 75a39b3

Please sign in to comment.