Skip to content

Commit ed872e9

Browse files
committed
Make shell a config file setting
- Setting `shell = "bash"` in the config file will now make the global `match` value default to Bash relevant matches.
1 parent 9951723 commit ed872e9

7 files changed

Lines changed: 152 additions & 57 deletions

File tree

README.md

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
- [Command line interface](#command-line-interface)
5050
- [`lock` command](#lock-command)
5151
- [`source` command](#source-command)
52+
- [`init` command](#init-command)
5253
- [`add` command](#add-command)
5354
- [`edit` command](#edit-command)
5455
- [`remove` command](#remove-command)
@@ -73,6 +74,7 @@
7374
- [Example: symlinking files](#example-symlinking-files)
7475
- [Example: overriding the PATH template](#example-overriding-the-path-template)
7576
- [Configuration: global options](#configuration-global-options)
77+
- [`shell`](#shell)
7678
- [`match`](#match)
7779
- [`apply`](#apply-1)
7880
- [Examples](#examples)
@@ -130,18 +132,27 @@ required plugin sources, generate a lock file, and then output shell source.
130132

131133
By default the config file is located at `~/.sheldon/plugins.toml`. You can
132134
either edit this file directly or use the provided command line interface to add
133-
or remove plugins. To add your first plugin to the config file run the `sheldon
134-
add` command.
135+
or remove plugins. To initialize this file run the following.
136+
137+
```sh
138+
sheldon init --shell bash
139+
```
140+
141+
or if you're using Zsh
142+
143+
```sh
144+
sheldon init
145+
```
146+
147+
To add your first plugin to the config file run the `sheldon add` command.
135148

136149
```sh
137150
sheldon add oh-my-zsh --github "ohmyzsh/ohmyzsh"
138151
```
139152

140153
The first argument given here `oh-my-zsh` is a unique name for the plugin. The
141154
`--github` option specifies that we want **sheldon** to manage a clone of
142-
http://github.com/ohmyzsh/ohmyzsh. If this is the first time you are running
143-
**sheldon**, you will be asked if you want to initialize a new config file at
144-
`~/.sheldon/plugins.toml`.
155+
http://github.com/ohmyzsh/ohmyzsh.
145156

146157
You can then use `sheldon source` to install the configured plugins, generate
147158
the lock file, and print out the shell script to source. Simply add the
@@ -187,6 +198,23 @@ configuration prior to generating the script. The output of this command is
187198
highly configurable. You can define your own [custom
188199
templates](#configuration-templates) to apply to your plugins.
189200

201+
### `init` command
202+
203+
This command initializes a new config file. If a config file exists then this
204+
command is a noop.
205+
206+
For example
207+
208+
```sh
209+
sheldon init
210+
```
211+
212+
Or you can specify the shell.
213+
214+
```sh
215+
sheldon init --shell bash
216+
```
217+
190218
### `add` command
191219

192220
This command adds a new plugin to the config file. It does nothing else but edit
@@ -564,6 +592,17 @@ apply = ["source", "PATH"]
564592

565593
## Configuration: global options
566594

595+
### `shell`
596+
597+
Indicates the shell that you are using **sheldon** with. If this field is set to
598+
`bash` the global [`match`](#match) default configuration will use Bash relevant
599+
defaults. If you are using Zsh you don't need to set this value but you may set
600+
it to `zsh`. For example
601+
602+
```toml
603+
shell = "bash"
604+
```
605+
567606
### `match`
568607

569608
A list of glob patterns to match against a plugin's contents. The first pattern
@@ -583,8 +622,20 @@ match = [
583622
]
584623
```
585624

586-
**Note:** if you are not using [Zsh] then you should probably change this
587-
setting.
625+
If `shell = "bash"` then this defaults to
626+
627+
```toml
628+
match = [
629+
"{{ name }}.plugin.bash",
630+
"{{ name }}.plugin.sh",
631+
"{{ name }}.bash",
632+
"{{ name }}.sh",
633+
"*.plugin.bash",
634+
"*.plugin.sh",
635+
"*.bash",
636+
"*.sh"
637+
]
638+
```
588639

589640
### `apply`
590641

src/config.rs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const GITHUB_HOST: &str = "github.com";
2929
/////////////////////////////////////////////////////////////////////////
3030

3131
/// The type of shell that we are using.
32-
#[derive(Debug, Clone, Copy, PartialEq)]
32+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
3333
pub enum Shell {
3434
Bash,
3535
Zsh,
@@ -170,6 +170,8 @@ pub enum Plugin {
170170
#[derive(Debug, Default, Deserialize)]
171171
#[serde(default)]
172172
pub struct RawConfig {
173+
/// What type of shell is being used.
174+
pub shell: Option<Shell>,
173175
/// Which files to match and use in a plugin's directory.
174176
#[serde(rename = "match")]
175177
matches: Option<Vec<String>>,
@@ -187,6 +189,8 @@ pub struct RawConfig {
187189
/// The user configuration.
188190
#[derive(Debug)]
189191
pub struct Config {
192+
/// What type of shell is being used.
193+
pub shell: Option<Shell>,
190194
/// Which files to match and use in a plugin's directory.
191195
pub matches: Option<Vec<String>>,
192196
/// The default list of template names to apply to each matched file.
@@ -258,6 +262,7 @@ macro_rules! impl_serialize_as_str {
258262
};
259263
}
260264

265+
impl_serialize_as_str!(Shell);
261266
impl_serialize_as_str!(GitProtocol);
262267
impl_serialize_as_str!(GistRepository);
263268
impl_serialize_as_str!(GitHubRepository);
@@ -503,6 +508,7 @@ macro_rules! impl_deserialize_from_str {
503508
};
504509
}
505510

511+
impl_deserialize_from_str!(shell, Shell, "a supported shell type");
506512
impl_deserialize_from_str!(git_protocol, GitProtocol, "a Git protocol type");
507513
impl_deserialize_from_str!(gist_repository, GistRepository, "a Gist identifier");
508514
impl_deserialize_from_str!(github_repository, GitHubRepository, "a GitHub repository");
@@ -775,6 +781,7 @@ impl RawConfig {
775781
/// Normalize a `RawConfig` into a `Config`.
776782
fn normalize(self, mut warnings: &mut Vec<Error>) -> Result<Config> {
777783
let Self {
784+
shell,
778785
matches,
779786
apply,
780787
templates,
@@ -822,6 +829,7 @@ impl RawConfig {
822829
}
823830

824831
Ok(Config {
832+
shell,
825833
matches,
826834
apply,
827835
templates,
@@ -849,9 +857,10 @@ mod tests {
849857
use super::*;
850858
use pretty_assertions::assert_eq;
851859

852-
#[derive(Debug, Deserialize)]
853-
struct TemplateTest {
854-
t: Template,
860+
#[test]
861+
fn shell_to_string() {
862+
assert_eq!(Shell::Bash.to_string(), "bash");
863+
assert_eq!(Shell::Zsh.to_string(), "zsh");
855864
}
856865

857866
#[test]
@@ -884,6 +893,31 @@ mod tests {
884893
assert_eq!(test.to_string(), "rossmacarthur/sheldon-test");
885894
}
886895

896+
#[derive(Debug, Deserialize)]
897+
struct ShellTest {
898+
s: Shell,
899+
}
900+
901+
#[test]
902+
fn shell_deserialize_as_str() {
903+
let test: ShellTest = toml::from_str("s = 'bash'").unwrap();
904+
assert_eq!(test.s, Shell::Bash)
905+
}
906+
907+
#[test]
908+
fn shell_deserialize_invalid() {
909+
let error = toml::from_str::<ShellTest>("s = 'ksh'").unwrap_err();
910+
assert_eq!(
911+
error.to_string(),
912+
"expected one of `bash` or `zsh` for key `s` at line 1 column 5"
913+
)
914+
}
915+
916+
#[derive(Debug, Deserialize)]
917+
struct TemplateTest {
918+
t: Template,
919+
}
920+
887921
#[test]
888922
fn template_deserialize_as_str() {
889923
let test: TemplateTest = toml::from_str("t = 'test'").unwrap();

src/configs/bash.plugins.toml

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/edit.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,20 @@ impl fmt::Display for Config {
3232
}
3333

3434
impl Config {
35-
pub fn default(shell: Shell) -> Self {
35+
/// Returns the default `Config` for the given shell.
36+
pub fn default(shell: Option<Shell>) -> Self {
37+
let config = include_str!("plugins.toml");
3638
match shell {
37-
Shell::Bash => Self::from_str(include_str!("configs/bash.plugins.toml")).unwrap(),
38-
Shell::Zsh => Self::from_str(include_str!("configs/zsh.plugins.toml")).unwrap(),
39+
Some(shell) => {
40+
// FIXME: figure out how to do this with `toml_edit`, it always places
41+
// shell = ... above the header comment.
42+
let config = config.replace(
43+
"\n[plugins]",
44+
&format!("\nshell = \"{}\"\n\n[plugins]", shell),
45+
);
46+
Self::from_str(config).unwrap()
47+
}
48+
None => Self::from_str(config).unwrap(),
3949
}
4050
}
4151

@@ -123,6 +133,21 @@ mod tests {
123133
use std::{io::Write, path::PathBuf};
124134
use url::Url;
125135

136+
#[test]
137+
fn config_default() {
138+
Config::default(None);
139+
}
140+
141+
#[test]
142+
fn config_default_bash() {
143+
Config::default(Some(Shell::Bash));
144+
}
145+
146+
#[test]
147+
fn config_default_zsh() {
148+
Config::default(Some(Shell::Zsh));
149+
}
150+
126151
#[test]
127152
fn config_from_str_invalid() {
128153
Config::from_str("x = \n").unwrap_err();

src/lib.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use anyhow::{bail, Context as ResultExt, Error, Result};
2929

3030
use crate::{
3131
cli::{Command, Opt},
32-
config::{Config, Shell},
32+
config::Config,
3333
context::{Context, EditContext, LockContext, SettingsExt},
3434
edit::Plugin,
3535
lock::LockedConfig,
@@ -52,19 +52,13 @@ impl Sheldon {
5252
bail!("aborted initialization!");
5353
};
5454

55-
let shell = ctx.shell.unwrap_or_else(|| {
56-
casual::prompt("Are you using sheldon with Bash or Zsh? [default: zsh] ")
57-
.default(Shell::default())
58-
.get()
59-
});
60-
6155
if let Some(parent) = path.parent() {
6256
fs::create_dir_all(parent).with_context(s!(
6357
"failed to create directory `{}`",
6458
&ctx.replace_home(parent).display()
6559
))?;
6660
}
67-
Ok(edit::Config::default(shell))
61+
Ok(edit::Config::default(ctx.shell))
6862
} else {
6963
Err(err)
7064
}
@@ -78,7 +72,7 @@ impl Sheldon {
7872
.with_context(s!("failed to check `{}`", path.display()))
7973
{
8074
Ok(_) => {
81-
header!(ctx, "Checked", path);
75+
header!(ctx, "Already initialized", path);
8276
}
8377
Err(err) => {
8478
Self::init_config(ctx, path, err)?.to_path(path)?;

0 commit comments

Comments
 (0)