Repository: knope-dev/knope (rust)

Issue:
Feature request: add "get-version" workflow to default config
It's often useful to get the current version of a project without actually modifying it. Would it make sense for the default generated config file to include this feature?

```
[[workflows]]
name = "get-version"

[[workflows.steps]]
type = "Command"
command = "echo \"$version\""

[workflows.steps.variables]
"$version" = "Version"
```

Pull request:
Add get-version command to default configuration
Adds the `get-version` command to the default configuration:

```toml
[[workflows]]
name = "get-version"
help_text = "Get the current version of the project"

[[workflows.steps]]
type = "Command"
command = "echo \"$version\""

[workflows.steps.variables]
"$version" = "Version"
```

Closes #885

Patch:
diff --git a/.changeset/add_get_version_default_workflow.md b/.changeset/add_get_version_default_workflow.md
new file mode 100644
index 0000000..1ec2da3
--- /dev/null
+++ b/.changeset/add_get_version_default_workflow.md
@@ -0,0 +1,15 @@
+---
+knope: minor
+---
+
+# Add `get-version` default workflow
+
+For **single-package repositories** with no custom workflows defined,
+there is now a [default workflow](https://knope.tech/reference/default-config/#workflows) called `get-version` that
+prints out the current package version.
+
+If you want similar functionality for multi-package repositories, please add your ideas to [issue #988](https://github.com/knope-dev/knope/issues/988).
+
+Thanks to @BatmanAoD for the suggestion and @alex-way for the implementation!
+
+PR #994 closed #885.
diff --git a/crates/knope/src/config/mod.rs b/crates/knope/src/config/mod.rs
index dbdca12..8289dfa 100644
--- a/crates/knope/src/config/mod.rs
+++ b/crates/knope/src/config/mod.rs
@@ -333,7 +333,8 @@ fn generate_workflows(has_forge: bool, packages: &[Package]) -> Vec<Workflow> {
         ]
     };
     release_steps.insert(0, Step::PrepareRelease(PrepareRelease::default()));
-    vec![
+
+    let mut workflows = vec![
         Workflow {
             name: String::from("release"),
             help_text: None,
@@ -344,7 +345,25 @@ fn generate_workflows(has_forge: bool, packages: &[Package]) -> Vec<Workflow> {
             help_text: None,
             steps: vec![Step::CreateChangeFile],
         },
-    ]
+    ];
+
+    if packages.len() == 1 {
+        let mut get_version_variables = IndexMap::new();
+        get_version_variables.insert(String::from("$version"), Variable::Version);
+
+        let get_version_steps = vec![Step::Command {
+            command: String::from("echo \"$version\""),
+            variables: Some(get_version_variables),
+            shell: None,
+        }];
+
+        workflows.push(Workflow {
+            name: String::from("get-version"),
+            help_text: Some(String::from("Get the current version of the project")),
+            steps: get_version_steps,
+        });
+    }
+    workflows
 }
 
 #[cfg(test)]
diff --git a/docs/src/content/docs/reference/default-config.md b/docs/src/content/docs/reference/default-config.md
index 7a6b4cf..a4ef81e 100644
--- a/docs/src/content/docs/reference/default-config.md
+++ b/docs/src/content/docs/reference/default-config.md
@@ -74,7 +74,7 @@ scopes = ["something-else"]
 When there are no workflows defined in a `knope.toml` file, Knope will use the default workflows.
 Some pieces will differ depending on the configured packages and forges:
 
-```toml title="knope.toml" {"Does not use a $version variable when there are multiple packages": 9-13} {"Moves git push down here and pushes tags if no forges are configured": 18}
+```toml title="knope.toml" {"Does not use a $version variable when there are multiple packages": 11-13} {"Moves git push down here and pushes tags if no forges are configured": 22} {"Omits "get-version" when there are multiple packages": 30-40}
 [[workflows]]
 name = "release"
 
@@ -85,13 +85,14 @@ type = "PrepareRelease"
 type = "Command"
 command = "git commit -m \"chore: prepare release $version\""
 
-[[workflows.steps]]
-type = "Command"
-command = "git push"
 
 [workflows.steps.variables]
 "$version" = "Version"
 
+[[workflows.steps]]
+type = "Command"
+command = "git push"
+
 [[workflows.steps]]
 type = "Release"
 
@@ -102,6 +103,18 @@ name = "document-change"
 
 [[workflows.steps]]
 type = "CreateChangeFile"
+
+
+[[workflows]]
+name = "get-version"
+help_text = "Get the current version of the project"
+
+[[workflows.steps]]
+type = "Command"
+command = "echo \"$version\""
+
+[[workflows.steps.variables]]
+"$version" = "Version"
 ```
 
 ## Forges


Test patch:
diff --git a/crates/knope/tests/default_workflows/get_version/mod.rs b/crates/knope/tests/default_workflows/get_version/mod.rs
new file mode 100644
index 0000000..e12403a
--- /dev/null
+++ b/crates/knope/tests/default_workflows/get_version/mod.rs
@@ -0,0 +1,2 @@
+mod multi_package;
+mod single_package;
diff --git a/crates/knope/tests/default_workflows/get_version/multi_package/in/Cargo.toml b/crates/knope/tests/default_workflows/get_version/multi_package/in/Cargo.toml
new file mode 100644
index 0000000..b7e9607
--- /dev/null
+++ b/crates/knope/tests/default_workflows/get_version/multi_package/in/Cargo.toml
@@ -0,0 +1,3 @@
+[package]
+name = "single-package-get-version"
+version = "1.23.45"
diff --git a/crates/knope/tests/default_workflows/get_version/multi_package/in/knope.toml b/crates/knope/tests/default_workflows/get_version/multi_package/in/knope.toml
new file mode 100644
index 0000000..8dbaada
--- /dev/null
+++ b/crates/knope/tests/default_workflows/get_version/multi_package/in/knope.toml
@@ -0,0 +1,5 @@
+[packages.rust]
+versioned_files = ["Cargo.toml"]
+
+[packages.js]
+versioned_files = ["package.json"]
diff --git a/crates/knope/tests/default_workflows/get_version/multi_package/in/package.json b/crates/knope/tests/default_workflows/get_version/multi_package/in/package.json
new file mode 100644
index 0000000..1c8dac0
--- /dev/null
+++ b/crates/knope/tests/default_workflows/get_version/multi_package/in/package.json
@@ -0,0 +1,3 @@
+{
+  "version": "13.24.54"
+}
\ No newline at end of file
diff --git a/crates/knope/tests/default_workflows/get_version/multi_package/mod.rs b/crates/knope/tests/default_workflows/get_version/multi_package/mod.rs
new file mode 100644
index 0000000..0a657b8
--- /dev/null
+++ b/crates/knope/tests/default_workflows/get_version/multi_package/mod.rs
@@ -0,0 +1,6 @@
+use crate::helpers::TestCase;
+
+#[test]
+fn get_version() {
+    TestCase::new(file!()).run("get-version");
+}
diff --git a/crates/knope/tests/default_workflows/get_version/multi_package/stderr.log b/crates/knope/tests/default_workflows/get_version/multi_package/stderr.log
new file mode 100644
index 0000000..4d7f1db
--- /dev/null
+++ b/crates/knope/tests/default_workflows/get_version/multi_package/stderr.log
@@ -0,0 +1,5 @@
+error: unrecognized subcommand 'get-version'
+
+Usage: knope[EXE] [OPTIONS] [COMMAND]
+
+For more information, try '--help'.
diff --git a/crates/knope/tests/default_workflows/get_version/single_package/in/Cargo.toml b/crates/knope/tests/default_workflows/get_version/single_package/in/Cargo.toml
new file mode 100644
index 0000000..b7e9607
--- /dev/null
+++ b/crates/knope/tests/default_workflows/get_version/single_package/in/Cargo.toml
@@ -0,0 +1,3 @@
+[package]
+name = "single-package-get-version"
+version = "1.23.45"
diff --git a/crates/knope/tests/default_workflows/get_version/single_package/mod.rs b/crates/knope/tests/default_workflows/get_version/single_package/mod.rs
new file mode 100644
index 0000000..0a657b8
--- /dev/null
+++ b/crates/knope/tests/default_workflows/get_version/single_package/mod.rs
@@ -0,0 +1,6 @@
+use crate::helpers::TestCase;
+
+#[test]
+fn get_version() {
+    TestCase::new(file!()).run("get-version");
+}
diff --git a/crates/knope/tests/default_workflows/get_version/single_package/stdout.log b/crates/knope/tests/default_workflows/get_version/single_package/stdout.log
new file mode 100644
index 0000000..c7e09b5
--- /dev/null
+++ b/crates/knope/tests/default_workflows/get_version/single_package/stdout.log
@@ -0,0 +1,1 @@
+1.23.45
diff --git a/crates/knope/tests/default_workflows/mod.rs b/crates/knope/tests/default_workflows/mod.rs
index 9f3a308..14958db 100644
--- a/crates/knope/tests/default_workflows/mod.rs
+++ b/crates/knope/tests/default_workflows/mod.rs
@@ -1,6 +1,3 @@
-mod cargo_workspace;
 mod document_change;
-mod gitea;
-mod github;
-mod no_forge;
+mod get_version;
 mod release;
diff --git a/crates/knope/tests/default_workflows/cargo_workspace/in/Cargo.toml b/crates/knope/tests/default_workflows/release/cargo_workspace/in/Cargo.toml
similarity index 100%
rename from crates/knope/tests/default_workflows/cargo_workspace/in/Cargo.toml
rename to crates/knope/tests/default_workflows/release/cargo_workspace/in/Cargo.toml
diff --git a/crates/knope/tests/default_workflows/cargo_workspace/in/first/Cargo.toml b/crates/knope/tests/default_workflows/release/cargo_workspace/in/first/Cargo.toml
similarity index 100%
rename from crates/knope/tests/default_workflows/cargo_workspace/in/first/Cargo.toml
rename to crates/knope/tests/default_workflows/release/cargo_workspace/in/first/Cargo.toml
diff --git a/crates/knope/tests/default_workflows/cargo_workspace/in/knope.toml b/crates/knope/tests/default_workflows/release/cargo_workspace/in/knope.toml
similarity index 100%
rename from crates/knope/tests/default_workflows/cargo_workspace/in/knope.toml
rename to crates/knope/tests/default_workflows/release/cargo_workspace/in/knope.toml
diff --git a/crates/knope/tests/default_workflows/cargo_workspace/in/second/Cargo.toml b/crates/knope/tests/default_workflows/release/cargo_workspace/in/second/Cargo.toml
similarity index 100%
rename from crates/knope/tests/default_workflows/cargo_workspace/in/second/Cargo.toml
rename to crates/knope/tests/default_workflows/release/cargo_workspace/in/second/Cargo.toml
diff --git a/crates/knope/tests/default_workflows/cargo_workspace/mod.rs b/crates/knope/tests/default_workflows/release/cargo_workspace/mod.rs
similarity index 100%
rename from crates/knope/tests/default_workflows/cargo_workspace/mod.rs
rename to crates/knope/tests/default_workflows/release/cargo_workspace/mod.rs
diff --git a/crates/knope/tests/default_workflows/cargo_workspace/stdout.log b/crates/knope/tests/default_workflows/release/cargo_workspace/stdout.log
similarity index 100%
rename from crates/knope/tests/default_workflows/cargo_workspace/stdout.log
rename to crates/knope/tests/default_workflows/release/cargo_workspace/stdout.log
diff --git a/crates/knope/tests/default_workflows/gitea/in/Cargo.toml b/crates/knope/tests/default_workflows/release/gitea/in/Cargo.toml
similarity index 100%
rename from crates/knope/tests/default_workflows/gitea/in/Cargo.toml
rename to crates/knope/tests/default_workflows/release/gitea/in/Cargo.toml
diff --git a/crates/knope/tests/default_workflows/gitea/in/knope.toml b/crates/knope/tests/default_workflows/release/gitea/in/knope.toml
similarity index 100%
rename from crates/knope/tests/default_workflows/gitea/in/knope.toml
rename to crates/knope/tests/default_workflows/release/gitea/in/knope.toml
diff --git a/crates/knope/tests/default_workflows/gitea/mod.rs b/crates/knope/tests/default_workflows/release/gitea/mod.rs
similarity index 100%
rename from crates/knope/tests/default_workflows/gitea/mod.rs
rename to crates/knope/tests/default_workflows/release/gitea/mod.rs
diff --git a/crates/knope/tests/default_workflows/gitea/stdout.log b/crates/knope/tests/default_workflows/release/gitea/stdout.log
similarity index 100%
rename from crates/knope/tests/default_workflows/gitea/stdout.log
rename to crates/knope/tests/default_workflows/release/gitea/stdout.log
diff --git a/crates/knope/tests/default_workflows/github/in/Cargo.toml b/crates/knope/tests/default_workflows/release/github/in/Cargo.toml
similarity index 100%
rename from crates/knope/tests/default_workflows/github/in/Cargo.toml
rename to crates/knope/tests/default_workflows/release/github/in/Cargo.toml
diff --git a/crates/knope/tests/default_workflows/github/in/knope.toml b/crates/knope/tests/default_workflows/release/github/in/knope.toml
similarity index 100%
rename from crates/knope/tests/default_workflows/github/in/knope.toml
rename to crates/knope/tests/default_workflows/release/github/in/knope.toml
diff --git a/crates/knope/tests/default_workflows/github/mod.rs b/crates/knope/tests/default_workflows/release/github/mod.rs
similarity index 100%
rename from crates/knope/tests/default_workflows/github/mod.rs
rename to crates/knope/tests/default_workflows/release/github/mod.rs
diff --git a/crates/knope/tests/default_workflows/github/stdout.log b/crates/knope/tests/default_workflows/release/github/stdout.log
similarity index 100%
rename from crates/knope/tests/default_workflows/github/stdout.log
rename to crates/knope/tests/default_workflows/release/github/stdout.log
diff --git a/crates/knope/tests/default_workflows/release/help_multi_package/mod.rs b/crates/knope/tests/default_workflows/release/help_multi_package/mod.rs
index c891603..be4244d 100644
--- a/crates/knope/tests/default_workflows/release/help_multi_package/mod.rs
+++ b/crates/knope/tests/default_workflows/release/help_multi_package/mod.rs
@@ -1,4 +1,6 @@
+use crate::helpers::TestCase;
+
 #[test]
 fn help() {
     TestCase::new(file!()).run("release --help");
-}
\ No newline at end of file
+}
diff --git a/crates/knope/tests/default_workflows/release/help_multi_package/stdout.log b/crates/knope/tests/default_workflows/release/help_multi_package/stdout.log
index 2eeaa6f..1539b1e 100644
--- a/crates/knope/tests/default_workflows/release/help_multi_package/stdout.log
+++ b/crates/knope/tests/default_workflows/release/help_multi_package/stdout.log
@@ -1,4 +1,4 @@
-Usage: knope release [OPTIONS]
+Usage: knope[EXE] release [OPTIONS]
 
 Options:
       --dry-run
diff --git a/crates/knope/tests/default_workflows/release/help_single_package/mod.rs b/crates/knope/tests/default_workflows/release/help_single_package/mod.rs
index c891603..be4244d 100644
--- a/crates/knope/tests/default_workflows/release/help_single_package/mod.rs
+++ b/crates/knope/tests/default_workflows/release/help_single_package/mod.rs
@@ -1,4 +1,6 @@
+use crate::helpers::TestCase;
+
 #[test]
 fn help() {
     TestCase::new(file!()).run("release --help");
-}
\ No newline at end of file
+}
diff --git a/crates/knope/tests/default_workflows/release/help_single_package/stdout.log b/crates/knope/tests/default_workflows/release/help_single_package/stdout.log
index 0623b1f..fb5dee3 100644
--- a/crates/knope/tests/default_workflows/release/help_single_package/stdout.log
+++ b/crates/knope/tests/default_workflows/release/help_single_package/stdout.log
@@ -1,4 +1,4 @@
-Usage: knope release [OPTIONS]
+Usage: knope[EXE] release [OPTIONS]
 
 Options:
       --dry-run
diff --git a/crates/knope/tests/default_workflows/release/mod.rs b/crates/knope/tests/default_workflows/release/mod.rs
index 8b13789..5254c28 100644
--- a/crates/knope/tests/default_workflows/release/mod.rs
+++ b/crates/knope/tests/default_workflows/release/mod.rs
@@ -1,1 +1,6 @@
-
+mod cargo_workspace;
+mod gitea;
+mod github;
+mod help_multi_package;
+mod help_single_package;
+mod no_forge;
diff --git a/crates/knope/tests/default_workflows/no_forge/in/Cargo.toml b/crates/knope/tests/default_workflows/release/no_forge/in/Cargo.toml
similarity index 100%
rename from crates/knope/tests/default_workflows/no_forge/in/Cargo.toml
rename to crates/knope/tests/default_workflows/release/no_forge/in/Cargo.toml
diff --git a/crates/knope/tests/default_workflows/no_forge/in/knope.toml b/crates/knope/tests/default_workflows/release/no_forge/in/knope.toml
similarity index 100%
rename from crates/knope/tests/default_workflows/no_forge/in/knope.toml
rename to crates/knope/tests/default_workflows/release/no_forge/in/knope.toml
diff --git a/crates/knope/tests/default_workflows/no_forge/mod.rs b/crates/knope/tests/default_workflows/release/no_forge/mod.rs
similarity index 100%
rename from crates/knope/tests/default_workflows/no_forge/mod.rs
rename to crates/knope/tests/default_workflows/release/no_forge/mod.rs
diff --git a/crates/knope/tests/default_workflows/no_forge/stdout.log b/crates/knope/tests/default_workflows/release/no_forge/stdout.log
similarity index 100%
rename from crates/knope/tests/default_workflows/no_forge/stdout.log
rename to crates/knope/tests/default_workflows/release/no_forge/stdout.log
diff --git a/crates/knope/tests/generate/packages/out/knope.toml b/crates/knope/tests/generate/packages/out/knope.toml
index eedad81..5a8c282 100644
--- a/crates/knope/tests/generate/packages/out/knope.toml
+++ b/crates/knope/tests/generate/packages/out/knope.toml
@@ -30,3 +30,14 @@ name = "document-change"
 
 [[workflows.steps]]
 type = "CreateChangeFile"
+
+[[workflows]]
+name = "get-version"
+help_text = "Get the current version of the project"
+
+[[workflows.steps]]
+type = "Command"
+command = "echo \"$version\""
+
+[workflows.steps.variables]
+"$version" = "Version"
diff --git a/crates/knope/tests/generate/packages_with_changelog/out/knope.toml b/crates/knope/tests/generate/packages_with_changelog/out/knope.toml
index 4342ab8..0fa7c85 100644
--- a/crates/knope/tests/generate/packages_with_changelog/out/knope.toml
+++ b/crates/knope/tests/generate/packages_with_changelog/out/knope.toml
@@ -31,3 +31,14 @@ name = "document-change"
 
 [[workflows.steps]]
 type = "CreateChangeFile"
+
+[[workflows]]
+name = "get-version"
+help_text = "Get the current version of the project"
+
+[[workflows.steps]]
+type = "Command"
+command = "echo \"$version\""
+
+[workflows.steps.variables]
+"$version" = "Version"