Skip to content

Commit d6a8e20

Browse files
committed
feat(clean): add subcommand clean for easy cleanup
1 parent 407a811 commit d6a8e20

3 files changed

Lines changed: 94 additions & 31 deletions

File tree

‎SPECIFICATION.md‎

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# SettingSpec Specification
22

3-
**Version:** 0.5.1
3+
**Version:** 0.6
44
**Status:** Released
55
**Authors:** Arijit Basu and SettingSpec Contributors
66
**Repository:** <https://github.com/sayanarijit/settingspec>
@@ -721,15 +721,44 @@ All commands support:
721721
-V, --version Show the version.
722722
```
723723

724-
### 7.2 `export`
724+
### 7.2 `init`
725+
726+
Creates a starter `settingspec.toml` in the current directory.
727+
728+
```bash
729+
settingspec init [OPTIONS]
730+
```
731+
732+
### 7.3 `check`
733+
734+
Checks the configuration without creating files.
735+
736+
```bash
737+
settingspec check [OPTIONS]
738+
```
739+
740+
It checks:
741+
742+
- TOML syntax.
743+
- Reserved keywords in setting names and profiles.
744+
- Profile completeness.
745+
- Required environment variables for active profile.
746+
- Decryption identities for encrypted environment files used by the active profile.
747+
748+
Exit codes:
749+
750+
- `0` — validation passed.
751+
- Non-zero — validation failed.
752+
753+
### 7.4 `export`
725754

726755
Generates the configured export files and/or writes resolved settings to standard output.
727756

728757
```bash
729758
settingspec export [OPTIONS]
730759
```
731760

732-
### 7.3 `run`
761+
### 7.5 `run`
733762

734763
Runs another command with the resolved configuration available to it.
735764

@@ -750,28 +779,7 @@ settingspec run [OPTIONS] -- <COMMAND> [ARGS...]
750779
- With `export.keep = true`, generated files remain.
751780
8. SettingSpec exits with the same status code as the child command.
752781

753-
### 7.4 `check`
754-
755-
Checks the configuration without creating files.
756-
757-
```bash
758-
settingspec check [OPTIONS]
759-
```
760-
761-
It checks:
762-
763-
- TOML syntax.
764-
- Reserved keywords in setting names and profiles.
765-
- Profile completeness.
766-
- Required environment variables for active profile.
767-
- Decryption identities for encrypted environment files used by the active profile.
768-
769-
Exit codes:
770-
771-
- `0` — validation passed.
772-
- Non-zero — validation failed.
773-
774-
### 7.5 `watch`
782+
### 7.6 `watch`
775783

776784
Runs SettingSpec as a long-running service and regenerates the configured export files whenever `settingspec.toml` changes.
777785

@@ -781,16 +789,16 @@ Upon exit, files are cleaned up as per declared `spec.export.keep` behavior.
781789
settingspec watch [OPTIONS]
782790
```
783791

784-
### 7.6 `init`
792+
If `settingspec.toml` already exists, `init` does nothing and does not overwrite it.
785793

786-
Creates a starter `settingspec.toml` in the current directory.
794+
### 7.7 `clean`
795+
796+
Deletes the export files defined in `spec.export.file`. Keeps the gitignore entries.
787797

788798
```bash
789-
settingspec init [OPTIONS]
799+
settingspec clean [OPTIONS]
790800
```
791801

792-
If `settingspec.toml` already exists, `init` does nothing and does not overwrite it.
793-
794802
---
795803

796804
## 8. Security

‎src/cli.rs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ pub enum Commands {
5757
},
5858
/// Watches settingspec.toml and regenerates exports whenever it changes
5959
Watch,
60+
61+
/// Deletes all generated export files
62+
Clean,
6063
}
6164

6265
pub const INIT_CONFIG: &str = r#"[spec]
@@ -169,6 +172,7 @@ pub fn run_cli() -> Result<()> {
169172
execute_run(&ctx, &command)
170173
}
171174
Commands::Watch => run_watch(),
175+
Commands::Clean => clean_exported_files(),
172176
}
173177
}
174178

@@ -278,6 +282,17 @@ fn perform_export(ctx: &ExecutionContext) -> Result<Vec<PathBuf>> {
278282
Ok(generated_files)
279283
}
280284

285+
fn clean_exported_files() -> Result<()> {
286+
let ctx = load_and_resolve()?;
287+
288+
let _guard = CleanupGuard {
289+
keep: false,
290+
paths: ctx.doc.spec.export.file.keys().map(PathBuf::from).collect(),
291+
};
292+
293+
Ok(())
294+
}
295+
281296
fn execute_run(ctx: &ExecutionContext, command_args: &[String]) -> Result<()> {
282297
if command_args.is_empty() {
283298
return Err(SettingSpecError::InvalidToml(

‎tests/test_cli_commands.rs‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ fn test_cli_help_and_version() {
1414
.stdout(predicate::str::contains("export"))
1515
.stdout(predicate::str::contains("check"))
1616
.stdout(predicate::str::contains("watch"))
17-
.stdout(predicate::str::contains("run"));
17+
.stdout(predicate::str::contains("run"))
18+
.stdout(predicate::str::contains("clean"));
1819

1920
let mut cmd_ver = Command::cargo_bin("settingspec").unwrap();
2021
cmd_ver
@@ -1046,3 +1047,42 @@ key.default.val = "from_child"
10461047
let content = std::fs::read_to_string(sub.child("settings.toml").path()).unwrap();
10471048
assert!(content.contains("key = \"from_child\""));
10481049
}
1050+
1051+
#[test]
1052+
fn test_cli_clean_exported_files() {
1053+
let temp = assert_fs::TempDir::new().unwrap();
1054+
let config = temp.child("settingspec.toml");
1055+
config
1056+
.write_str(
1057+
r#"
1058+
[spec.export.file]
1059+
"settings.toml" = true
1060+
[settings]
1061+
key.default.val = "val"
1062+
"#,
1063+
)
1064+
.unwrap();
1065+
1066+
// First export to create the file
1067+
let mut export_cmd = Command::cargo_bin("settingspec").unwrap();
1068+
export_cmd
1069+
.current_dir(temp.path())
1070+
.arg("export")
1071+
.assert()
1072+
.success();
1073+
1074+
temp.child("settings.toml")
1075+
.assert(predicate::path::exists());
1076+
1077+
// Now run clean command to remove exported files
1078+
let mut clean_cmd = Command::cargo_bin("settingspec").unwrap();
1079+
clean_cmd
1080+
.current_dir(temp.path())
1081+
.arg("clean")
1082+
.assert()
1083+
.success();
1084+
1085+
// Verify exported file is removed
1086+
temp.child("settings.toml")
1087+
.assert(predicate::path::missing());
1088+
}

0 commit comments

Comments
 (0)