Skip to content

Commit

Permalink
Add a new config list subcommand that prints out all the commands i…
Browse files Browse the repository at this point in the history
…n the config file
  • Loading branch information
autarch committed Oct 8, 2023
1 parent 93411f1 commit a87550d
Show file tree
Hide file tree
Showing 8 changed files with 297 additions and 34 deletions.
117 changes: 117 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ precious-core.workspace = true
anyhow = "1.0.75"
clap = { version = "4.4.2", features = [ "cargo", "derive", "wrap_help" ] }
clean-path = "0.2.1"
comfy-table = "7.0.1"
env_logger = "0.10.0"
fern = { version = ">= 0.5.0, < 0.7.0", features = ["colored"] }
filetime = "0.2.22"
Expand All @@ -45,6 +46,7 @@ precious-core = { version = "0.5.0", path = "./precious-core" }
precious-helpers = { version = "0.5.0", path = "./precious-helpers" }
precious-testhelper = { version = "0.5.0", path = "./precious-testhelper" }
pretty_assertions = "1.4.0"
prettytable = "0.10.0"
rayon = "1.7.0"
regex = "1.9.5"
serde = { version = "1.0.188", features = ["derive"] }
Expand Down
2 changes: 2 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## 0.5.2

- Help output is now line-wrapped based on your terminal width.
- Added a new `precious config list` command that prints a table showing all the commands in your
config file. Requested by Olaf Alders. GH #52.

## 0.5.1 - 2023-03-11

Expand Down
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ You can disable parallel execution by passing `--jobs 1`.

### Subcommands

The `precious` command has two subcommands, `lint` and `tidy`. You must always specify one of these.
These subcommands take the same options.
The `precious` command has three subcommands, `lint`, `tidy`, and `config`. You must always specify
one of these. The `lint` and `tidy` commands take the same options:

#### Selecting Paths to Operate On

Expand Down Expand Up @@ -315,6 +315,29 @@ When `precious` runs it does the following to determine which commands apply to
- If `invoke` is `once`, then the rules are applied to all of the files at once. If any one of
those files matches the include rule, the command will be run.

### The `config` Subcommand

Right now this command only takes one option, which is an additional subcommand, `list`. This prints
a Unicode table describing the commands in your config file.

```
Found config file at: /home/autarch/projects/precious/precious.toml

┌─────────────────────┬──────┬────────────────────────────────────────────────────────┐
│ Name ┆ Type ┆ Runs │
╞═════════════════════╪══════╪════════════════════════════════════════════════════════╡
│ rustfmt ┆ both ┆ rustfmt --edition 2021 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ clippy ┆ lint ┆ cargo clippy --locked --all-targets --all-features │
│ ┆ ┆ --workspace -- -D clippy::all │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ prettier ┆ both ┆ ./node_modules/.bin/prettier --no-config --print-width │
│ ┆ ┆ 100 --prose-wrap always │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ omegasort-gitignore ┆ both ┆ omegasort --sort path --unique │
└─────────────────────┴──────┴────────────────────────────────────────────────────────┘
```
## Configuration Recommendations
Here are some recommendations for how to get the best experience with precious.
Expand Down
1 change: 1 addition & 0 deletions precious-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ version.workspace = true
anyhow.workspace = true
clap.workspace = true
clean-path.workspace = true
comfy-table.workspace = true
fern.workspace = true
globset.workspace = true
ignore.workspace = true
Expand Down
10 changes: 10 additions & 0 deletions precious-core/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ impl LintOrTidyCommandType {
}
}

impl fmt::Display for LintOrTidyCommandType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
LintOrTidyCommandType::Lint => "lint",
LintOrTidyCommandType::Tidy => "tidy",
LintOrTidyCommandType::Both => "both",
})
}
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub enum Invoke {
#[serde(rename = "per-file")]
Expand Down
52 changes: 28 additions & 24 deletions precious-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,52 +14,52 @@ use thiserror::Error;
#[derive(Clone, Debug, Deserialize)]
pub struct CommandConfig {
#[serde(rename = "type")]
typ: LintOrTidyCommandType,
pub(crate) typ: LintOrTidyCommandType,
#[serde(deserialize_with = "string_or_seq_string")]
include: Vec<String>,
pub(crate) include: Vec<String>,
#[serde(default)]
#[serde(deserialize_with = "string_or_seq_string")]
exclude: Vec<String>,
pub(crate) exclude: Vec<String>,
#[serde(default)]
invoke: Option<Invoke>,
pub(crate) invoke: Option<Invoke>,
#[serde(default)]
#[serde(deserialize_with = "working_dir")]
working_dir: Option<WorkingDir>,
pub(crate) working_dir: Option<WorkingDir>,
#[serde(default)]
path_args: Option<PathArgs>,
pub(crate) path_args: Option<PathArgs>,
#[serde(default)]
run_mode: Option<OldRunMode>,
pub(crate) run_mode: Option<OldRunMode>,
#[serde(default)]
chdir: Option<bool>,
pub(crate) chdir: Option<bool>,
#[serde(deserialize_with = "string_or_seq_string")]
cmd: Vec<String>,
pub(crate) cmd: Vec<String>,
#[serde(default)]
env: HashMap<String, String>,
pub(crate) env: HashMap<String, String>,
#[serde(default)]
#[serde(deserialize_with = "string_or_seq_string")]
lint_flags: Vec<String>,
pub(crate) lint_flags: Vec<String>,
#[serde(default)]
#[serde(deserialize_with = "string_or_seq_string")]
tidy_flags: Vec<String>,
pub(crate) tidy_flags: Vec<String>,
#[serde(default = "empty_string")]
path_flag: String,
pub(crate) path_flag: String,
#[serde(deserialize_with = "u8_or_seq_u8")]
ok_exit_codes: Vec<u8>,
pub(crate) ok_exit_codes: Vec<u8>,
#[serde(default)]
#[serde(deserialize_with = "u8_or_seq_u8")]
lint_failure_exit_codes: Vec<u8>,
pub(crate) lint_failure_exit_codes: Vec<u8>,
#[serde(default)]
expect_stderr: bool,
pub(crate) expect_stderr: bool,
#[serde(default)]
#[serde(deserialize_with = "string_or_seq_string")]
ignore_stderr: Vec<String>,
pub(crate) ignore_stderr: Vec<String>,
#[serde(default)]
#[serde(deserialize_with = "string_or_seq_string")]
labels: Vec<String>,
pub(crate) labels: Vec<String>,
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
pub enum OldRunMode {
pub(crate) enum OldRunMode {
#[serde(rename = "files")]
Files,
#[serde(rename = "dirs")]
Expand All @@ -76,12 +76,12 @@ fn empty_string() -> String {
pub struct Config {
#[serde(default)]
#[serde(deserialize_with = "string_or_seq_string")]
pub exclude: Vec<String>,
pub(crate) exclude: Vec<String>,
commands: IndexMap<String, CommandConfig>,
}

#[derive(Debug, Error, PartialEq, Eq)]
pub enum ConfigError {
pub(crate) enum ConfigError {
#[error("File at {} cannot be read: {error:}", file.display())]
FileCannotBeRead { file: PathBuf, error: String },
#[error(
Expand Down Expand Up @@ -345,7 +345,7 @@ where
const DEFAULT_LABEL: &str = "default";

impl Config {
pub fn new(file: &Path) -> Result<Config> {
pub(crate) fn new(file: &Path) -> Result<Config> {
match fs::read(file) {
Err(e) => Err(ConfigError::FileCannotBeRead {
file: file.to_path_buf(),
Expand All @@ -359,7 +359,7 @@ impl Config {
}
}

pub fn into_tidy_commands(
pub(crate) fn into_tidy_commands(
self,
project_root: &Path,
command: Option<&str>,
Expand All @@ -368,7 +368,7 @@ impl Config {
self.into_commands(project_root, command, label, LintOrTidyCommandType::Tidy)
}

pub fn into_lint_commands(
pub(crate) fn into_lint_commands(
self,
project_root: &Path,
command: Option<&str>,
Expand Down Expand Up @@ -405,6 +405,10 @@ impl Config {

Ok(commands)
}

pub(crate) fn command_info(self) -> Vec<(String, CommandConfig)> {
self.commands.into_iter().collect()
}
}

impl CommandConfig {
Expand Down
Loading

0 comments on commit a87550d

Please sign in to comment.