Skip to content

Commit 54fcc25

Browse files
committed
[CLI] Limit changelog output to last 5 releases and add --show-all option
1 parent bfeaef7 commit 54fcc25

File tree

8 files changed

+80
-3
lines changed

8 files changed

+80
-3
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,12 @@ Usage:
341341
<img src="assets/cli_valkyrie_changelog.png" width="550" />
342342
</div>
343343

344+
Output example:
345+
346+
<div align="center">
347+
<img src="assets/cli_valkyrie_changelog_output.png" width="550" />
348+
</div>
349+
344350
### Build CLI
345351

346352
Run `./gradlew buildCLI` to build minified version of CLI tool. Artifact will be available in

assets/cli_valkyrie_changelog.png

-300 KB
Loading
230 KB
Loading

gradle/cli.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[versions]
2-
cli-version = "1.0.0"
2+
cli-version = "1.0.1"
33

44
clikt = "5.0.3"
55

tools/cli/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# CLI Changelog
22

3+
## [1.0.1] - 2025-11-20
4+
5+
- Limited changelog output to the last 5 releases
6+
- Added `--show-all` option to display full changelog
7+
38
## [1.0.0] - 2025-11-17
49

510
- Stabilize CLI tool

tools/cli/src/main/kotlin/io/github/composegears/valkyrie/cli/command/ChangelogCommand.kt

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,43 @@ package io.github.composegears.valkyrie.cli.command
33
import com.github.ajalt.clikt.core.CliktCommand
44
import com.github.ajalt.clikt.core.Context
55
import com.github.ajalt.mordant.markdown.Markdown
6+
import io.github.composegears.valkyrie.cli.ext.flagOption
67

78
internal class ChangelogCommand : CliktCommand(name = "changelog") {
89

10+
private val showAll by flagOption(
11+
"--show-all",
12+
help = "Show full release history",
13+
)
14+
915
override fun run() {
10-
echo(Markdown(loadResourceText("CHANGELOG.md"), showHtml = true))
16+
val changelog = loadResourceText("CHANGELOG.md")
17+
18+
val markdownContent = when {
19+
showAll -> changelog
20+
else -> getLatestReleases(changelog)
21+
}
22+
echo(Markdown(markdown = markdownContent))
1123
}
1224

1325
override fun help(context: Context): String = "Print CLI changelog"
26+
27+
private fun getLatestReleases(changelog: String, count: Int = 5): String {
28+
val lines = changelog.lines()
29+
val releaseIndices = lines.indices.filter { index ->
30+
lines[index].trimStart().startsWith("## [")
31+
}
32+
33+
if (releaseIndices.size <= count) {
34+
return changelog
35+
}
36+
37+
val endIndex = releaseIndices[count]
38+
val limitedLines = lines.subList(0, endIndex)
39+
val result = limitedLines.joinToString("\n")
40+
41+
return "$result\n\n---\n\n*Showing $count latest releases. Use \"--show-all\" to see the full changelog.*"
42+
}
1443
}
1544

1645
private fun Any.loadResourceText(name: String): String {

tools/cli/src/main/kotlin/io/github/composegears/valkyrie/cli/ext/Option.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package io.github.composegears.valkyrie.cli.ext
22

33
import com.github.ajalt.clikt.core.CliktCommand
44
import com.github.ajalt.clikt.parameters.options.default
5+
import com.github.ajalt.clikt.parameters.options.flag
56
import com.github.ajalt.clikt.parameters.options.option
67
import com.github.ajalt.clikt.parameters.options.required
78
import com.github.ajalt.clikt.parameters.types.boolean
@@ -40,3 +41,9 @@ internal fun CliktCommand.intOption(
4041
help: String,
4142
default: Int,
4243
) = option(names = names, help = help).int().default(default)
44+
45+
internal fun CliktCommand.flagOption(
46+
vararg names: String,
47+
help: String,
48+
default: Boolean = false,
49+
) = option(names = names, help = help).flag(default = default)

tools/cli/src/test/kotlin/io/github/composegears/valkyrie/cli/command/ChangelogCommandTest.kt

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,46 @@ package io.github.composegears.valkyrie.cli.command
22

33
import assertk.assertThat
44
import assertk.assertions.contains
5+
import assertk.assertions.doesNotContain
6+
import assertk.assertions.isLessThan
57
import assertk.assertions.isNotEmpty
68
import com.github.ajalt.clikt.testing.test
79
import kotlin.test.Test
810

911
class ChangelogCommandTest {
1012

1113
@Test
12-
fun `run should print CHANGELOG content`() {
14+
fun `should print 5 latest releases`() {
1315
val result = ChangelogCommand().test()
1416

1517
assertThat(result.output).contains("CLI Changelog")
18+
assertThat(result.output).contains("Showing 5 latest releases")
19+
assertThat(result.output).contains("Use \"--show-all\" to see the full changelog")
20+
}
21+
22+
@Test
23+
fun `should print full changelog with show-all option`() {
24+
val result = ChangelogCommand().test("--show-all")
25+
26+
assertThat(result.output).contains("CLI Changelog")
27+
assertThat(result.output).doesNotContain("Showing 5 latest releases")
28+
// Should contain all releases including the initial one
29+
assertThat(result.output).contains("Initial release of Valkyrie CLI tool")
30+
}
31+
32+
@Test
33+
fun `limited changelog should be shorter than full changelog`() {
34+
val limitedResult = ChangelogCommand().test()
35+
val fullResult = ChangelogCommand().test("--show-all")
36+
37+
// Limited version should have the "Showing X latest releases" message
38+
assertThat(limitedResult.output).contains("Showing 5 latest releases")
39+
40+
// Full version should not have this message
41+
assertThat(fullResult.output).doesNotContain("Showing 5 latest releases")
42+
43+
// Limited version should be shorter than full version
44+
assertThat(limitedResult.output.length).isLessThan(fullResult.output.length)
1645
}
1746

1847
@Test
@@ -21,5 +50,6 @@ class ChangelogCommandTest {
2150

2251
assertThat(result.output).isNotEmpty()
2352
assertThat(result.output).contains("Print CLI changelog")
53+
assertThat(result.output).contains("--show-all")
2454
}
2555
}

0 commit comments

Comments
 (0)