Defining a Pipeline

Powerful Command-Line Applications in Go — by Ricardo Gerardi (63 / 127)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Writing Tests for Goci | TOC | Adding Another Step to the Pipe line 👉

At this point, your tool executes the first step in the CI pipeline: Go Build. In its current state, the information to execute this step is hard-coded into the run function. While this is a valid approach, adding more steps to this tool using the same approach would cause extensive code repetition. We want a tool that’s maintainable and easy to extend, so let’s make a change to the program’s structure to make the code more reusable.

To do that, you’ll refactor the part of the run function that executes the external program into its own function. To make it easier to configure, let’s add a custom type step that represents a pipeline step and associate the method execute to it. You’ll also add a constructor function called newStep to create a new step. By doing this, when you want to add a new step to the pipeline, you instantiate the step type with the appropriate values.

Before you do that, break the code into different files so it will be easier to maintain, as you did in Developing a File System Crawler.

Create a file called step.go in the goci directory and open it in your editor. Include the package definition and the import sections. Use the os/exec package to execute external programs:

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.