Skip to content

Commit 00755c1

Browse files
authored
Make some command line parameters and options optional (#156)
1 parent f6014ee commit 00755c1

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

cli/src/main/kotlin/com/bazel_diff/cli/GenerateHashesCommand.kt

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ class GenerateHashesCommand : Callable<Int> {
3434

3535
@CommandLine.Option(
3636
names = ["-b", "--bazelPath"],
37-
description = ["Path to Bazel binary"],
37+
description = ["Path to Bazel binary. If not specified, the Bazel binary available in PATH will be used."],
3838
scope = CommandLine.ScopeType.INHERIT,
39-
required = true,
39+
defaultValue = "bazel",
4040
)
4141
lateinit var bazelPath: Path
4242

@@ -80,15 +80,15 @@ class GenerateHashesCommand : Callable<Int> {
8080

8181
@CommandLine.Parameters(
8282
index = "0",
83-
description = ["The filepath to write the resulting JSON of dictionary target => SHA-256 values"]
83+
description = ["The filepath to write the resulting JSON of dictionary target => SHA-256 values. If not specified, the JSON will be written to STDOUT."],
84+
defaultValue = CommandLine.Parameters.NULL_VALUE
8485
)
8586
var outputPath: File? = null
8687

8788
@CommandLine.Spec
8889
lateinit var spec: CommandLine.Model.CommandSpec
8990

9091
override fun call(): Int {
91-
val output = validateOutput(outputPath)
9292
validate(contentHashPath=contentHashPath)
9393

9494
startKoin {
@@ -106,19 +106,12 @@ class GenerateHashesCommand : Callable<Int> {
106106
)
107107
}
108108

109-
return when (GenerateHashesInteractor().execute(seedFilepaths, output)) {
109+
return when (GenerateHashesInteractor().execute(seedFilepaths, outputPath)) {
110110
true -> CommandLine.ExitCode.OK
111111
false -> CommandLine.ExitCode.SOFTWARE
112112
}.also { stopKoin() }
113113
}
114114

115-
private fun validateOutput(output: File?): File {
116-
return output ?: throw CommandLine.ParameterException(
117-
spec.commandLine(),
118-
"No output path specified."
119-
)
120-
}
121-
122115
private fun validate(contentHashPath: File?) {
123116
contentHashPath?.let {
124117
if (!it.canRead()) {

cli/src/main/kotlin/com/bazel_diff/cli/GetImpactedTargetsCommand.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import org.koin.core.context.stopKoin
99
import picocli.CommandLine
1010
import java.io.BufferedWriter
1111
import java.io.File
12+
import java.io.FileDescriptor
1213
import java.io.FileWriter
1314
import java.io.IOException
1415
import java.util.concurrent.Callable
@@ -40,10 +41,9 @@ class GetImpactedTargetsCommand : Callable<Int> {
4041
@CommandLine.Option(
4142
names = ["-o", "--output"],
4243
scope = CommandLine.ScopeType.LOCAL,
43-
description = ["Filepath to write the impacted Bazel targets to, newline separated"],
44-
required = true,
44+
description = ["Filepath to write the impacted Bazel targets to, newline separated. If not specified, the targets will be written to STDOUT."],
4545
)
46-
lateinit var outputPath: File
46+
var outputPath: File? = null
4747

4848
@CommandLine.Spec
4949
lateinit var spec: CommandLine.Model.CommandSpec
@@ -64,7 +64,10 @@ class GetImpactedTargetsCommand : Callable<Int> {
6464
val impactedTargets = CalculateImpactedTargetsInteractor().execute(from, to)
6565

6666
return try {
67-
BufferedWriter(FileWriter(outputPath)).use { writer ->
67+
BufferedWriter(when (val path=outputPath) {
68+
null -> FileWriter(FileDescriptor.out)
69+
else -> FileWriter(path)
70+
}).use { writer ->
6871
impactedTargets.forEach {
6972
writer.write(it)
7073
//Should not be depend on OS

cli/src/main/kotlin/com/bazel_diff/interactor/GenerateHashesInteractor.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import org.koin.core.component.KoinComponent
77
import org.koin.core.component.inject
88
import java.io.BufferedReader
99
import java.io.File
10+
import java.io.FileDescriptor
1011
import java.io.FileReader
1112
import java.io.FileWriter
1213
import java.nio.file.Path
@@ -19,7 +20,7 @@ class GenerateHashesInteractor : KoinComponent {
1920
private val gson: Gson by inject()
2021

2122
@OptIn(ExperimentalTime::class)
22-
fun execute(seedFilepaths: File?, outputPath: File): Boolean {
23+
fun execute(seedFilepaths: File?, outputPath: File?): Boolean {
2324
return try {
2425
val duration = measureTime {
2526
var seedFilepathsSet: Set<Path> = when {
@@ -33,7 +34,10 @@ class GenerateHashesInteractor : KoinComponent {
3334
else -> emptySet()
3435
}
3536
val hashes = buildGraphHasher.hashAllBazelTargetsAndSourcefiles(seedFilepathsSet)
36-
FileWriter(outputPath).use {
37+
when (outputPath) {
38+
null -> FileWriter(FileDescriptor.out)
39+
else -> FileWriter(outputPath)
40+
}.use {
3741
it.write(gson.toJson(hashes))
3842
}
3943
}

0 commit comments

Comments
 (0)