Skip to content

Commit

Permalink
redirect command output to console based on CLI options
Browse files Browse the repository at this point in the history
  • Loading branch information
thiyagu06 committed Jan 31, 2022
1 parent eea4e82 commit f2d6173
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ You can then execute your native executable with: `./build/command-runner-1.0.0-
## TODO

- [x] ability to run specific step in the stage
- [ ] redirect command output to console based on YAML property.
- [x] redirect command output to console based on CLI options.
- [ ] ability to specify regex to verify command output
- [ ] add tests
- [ ] GitHub action to create release for native executables
Expand Down
3 changes: 0 additions & 3 deletions pipeline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ name: "Backend Developer"
description: "Install Pipeline required for backed developers"
steps:
setup:
- name: "check gh version"
command: "gh --version"
abortIfFailed: false
- name: "check docker version"
command: "docker --version"
abortIfFailed: true
Expand Down
11 changes: 10 additions & 1 deletion src/main/kotlin/com/thiyagu06/runner/commands/OptionsCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.thiyagu06.runner.commands
import com.thiyagu06.runner.Stage
import com.thiyagu06.runner.exception.StepNotFoundException
import com.thiyagu06.runner.model.Command
import com.thiyagu06.runner.model.RunnerGlobalSettings
import com.thiyagu06.runner.model.Pipeline
import com.thiyagu06.runner.reporter.ConsoleReporter
import com.thiyagu06.runner.service.InitializerService
Expand All @@ -19,14 +20,22 @@ open class OptionsCommand {
@Option(names = ["--step", "-s"], required = false, description = ["run the specific step from the pipeline yaml"])
var stepName: String? = null

@Option(
names = ["--no-printOutput", "--no-p"],
required = false,
description = ["whether to print the output of each step to console"],
negatable = true
)
var printOutput: Boolean = true

private val canExecuteCommand: (Command, String) -> Boolean = { command, compareTo -> compareTo == command.name }

fun run(stage: Stage) {
InitializerService.initGlobalDirectory()
val pipelineYaml = PipelineManager.toPipeline(pipeline)
val commandsToRun = getCommands(pipelineYaml, stage)
ConsoleReporter.header("Running pipeline: ${pipelineYaml.name} for stage: $stage, description: ${pipelineYaml.description}")
StepsExecutor.runSteps(commandsToRun)
StepsExecutor.runSteps(commandsToRun, RunnerGlobalSettings(printOutput))
}

private fun getCommands(pipeline: Pipeline, stage: Stage): List<Command> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.thiyagu06.runner.model

data class RunnerGlobalSettings(val printStepResult: Boolean)
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ object StepExecutionTracker {
executionStatuses.addIfAbsent(stepsResult)
}

fun onSuccess(stepName: String, duration: Duration, commandOutput: String) {
fun onSuccess(stepName: String, duration: Duration, commandOutput: String, shouldPrint: Boolean) {
val summary = StepExecutionResult(stepName, duration, SUCCESS, commandOutput)
if (shouldPrint) ConsoleReporter.info("successfully executed step: $stepName output: $commandOutput")
track(summary)
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/kotlin/com/thiyagu06/runner/service/StepsExecutor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package com.thiyagu06.runner.service
import com.thiyagu06.runner.model.Command
import com.thiyagu06.runner.model.CommandExecutionResult.Failure
import com.thiyagu06.runner.model.CommandExecutionResult.Success
import com.thiyagu06.runner.model.RunnerGlobalSettings
import java.time.Clock
import java.time.Duration
import java.time.Instant

object StepsExecutor {

fun runSteps(commands: List<Command>) {
fun runSteps(commands: List<Command>, settings: RunnerGlobalSettings) {
var lastFailedCommand: Command? = null
for (command in commands) {
if (lastFailedCommand?.abortIfFailed == true) {
Expand All @@ -20,7 +21,7 @@ object StepsExecutor {
val result = CommandExecutor.execute(command.command)
val executionTime = Duration.between(startTime, Instant.now(Clock.systemDefaultZone()))
when (result) {
is Success -> StepExecutionTracker.onSuccess(command.name, executionTime, result.commandOutput)
is Success -> StepExecutionTracker.onSuccess(command.name, executionTime, result.commandOutput, settings.printStepResult)
is Failure -> {
lastFailedCommand = command
StepExecutionTracker.onFailure(command.name, executionTime, result.commandOutput)
Expand Down

0 comments on commit f2d6173

Please sign in to comment.