Skip to content

Commit

Permalink
docs: upgrade guide changes on the defaultexecute struct
Browse files Browse the repository at this point in the history
  • Loading branch information
apenella committed Jan 19, 2024
1 parent 39d3bcd commit e1a0d38
Showing 1 changed file with 25 additions and 31 deletions.
56 changes: 25 additions & 31 deletions docs/upgrade_guide_to_2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
- [Overview](#overview)
- [Changes on the _Executor_ interface](#changes-on-the-executor-interface)
- [How to adapt your code to the new _Executor_ interface](#how-to-adapt-your-code-to-the-new-executor-interface)
- [How to replace the _command_ argument](#how-to-replace-the-command-argument)
- [How to replace the _resultsFunc_ argument](#how-to-replace-the-resultsfunc-argument)
- [How to replace the _options_ argument](#how-to-replace-the-options-argument)
- [Replacing the _command_ argument](#replacing-the-command-argument)
- [Replacing the _resultsFunc_ argument](#replacing-the-resultsfunc-argument)
- [Replacing the _options_ argument](#replacing-the-options-argument)
- [Changes on the _DefaultExecute_ struct](#changes-on-the-defaultexecute-struct)
- [Removed the _ShowDuration_ attribute](#removed-the-showduration-attribute)
- [Removing the _ShowDuration_ attribute](#removing-the-showduration-attribute)
- [New attribute _Output_ for printing execution results](#new-attribute-output-for-printing-execution-results)
- [New attribute _Exec_ for running external commands](#new-attribute-exec-for-running-external-commands)
- [Packages Reorganization](#packages-reorganization)
Expand All @@ -24,8 +24,10 @@ Go through the following sections to learn about the changes introduced in versi

## Changes on the _Executor_ interface

> That changes only affects your code if you have defined a custom _Executor_ struct.
The `Executor` interface has undergone significant breaking changes. It removes the `command`, `resultsFunc`, and `options` arguments from the `Execute` method.
That changes only affects your code if you have defined a custom _Executor_ struct.


Here is the updated `Executor` interface:

Expand All @@ -39,7 +41,7 @@ type Executor interface {

To align with the updated `Executor` interface, you need to adapt your custom _Executor_ by removing the `command`, `resultsFunc`, and `options` arguments from its `Execute` method. The following points detail how to replace each of these arguments.

#### How to replace the _command_ argument
#### Replacing the _command_ argument

Instead of using the command argument, the new `Executor` utilizes a `Commander` to generate commands for execution. Therefore, the `Executor` should include an attribute of type `Commander`.

Expand All @@ -54,7 +56,7 @@ type Commander interface {
}
```

The `Command` method returns an array of strings representing the command to execute. You should use this array for the component responsible for executing external commands.
The `Command` method returns an array of strings representing the command to execute. You should provide the component responsible for executing external commands with this array.

The `DefaultExecute` in _go-ansible_ utilizes an `Executabler` to execute external commands. The `Executabler` is an interface defined as:

Expand All @@ -66,7 +68,7 @@ type Executabler interface {
}
```

The `os/exec`'s `Cmd` struct implements the `Executabler` interface.
In the `os/exec` package, the `Cmd` struct implements the `Executabler` interface.

The following code showcases how the `DefaultExecute` uses the `Commander` and the `Executabler`.

Expand All @@ -81,9 +83,11 @@ if err != nil {
cmd := e.Exec.CommandContext(ctx, command[0], command[1:]...)
```

The `Cmd` attribute is of type `Commander`, while `Exec` is an `Executabler`.

By incorporating these changes, your code will align with the updated `Executor` interface in _go-ansible v2.x_.

#### How to replace the _resultsFunc_ argument
#### Replacing the _resultsFunc_ argument

The _resultsFunc_ previously managed the results output from command execution. With its removal, a new component within the Executor must assume this responsibility.

Expand All @@ -93,7 +97,7 @@ To handle the results output, _go-ansible_ provides two mechanisms:
Located in the package _github.com/apenella/go-ansible/pkg/execute/result/default_, the `DefaultResults` struct handles Ansible's results in plain text.

- **JSONResults Struct**
Defined in the package github.com/apenella/go-ansible/pkg/execute/json, the JSONResults struct manages Ansible's results in JSON format.
Defined in the package github.com/apenella/go-ansible/pkg/execute/json, the `JSONResults` struct manages Ansible's results in JSON format.

Choose between these mechanisms based on the stdout callback plugin you use.

Expand All @@ -113,39 +117,29 @@ To replace the _resultsFunc_, introduce an attribute of type `ResultsOutputer` i

The `DefaultExecute` includes a default `ResultsOutputer` attribute named `Output`, which uses the `DefaultResults` struct to print execution results.

#### How to replace the _options_ argument
#### Replacing the _options_ argument

By removing the _options_ argument, you will not be able to overwrite the _Executor_ configuration when you executing the command. You must set up the Executor when instanciating the struct.
By removing the options argument, the ability to overwrite the `Executor` struct attributes in the `Execute` method is no longer available. To configure the `Executor`, you must set it up during the instantiation of the struct.

## Changes on the _DefaultExecute_ struct

The `DefaultExecute` struct, functioning as the default executor in the `go-ansible` library, has introduced significant changes in version 2.0.0. The next sections describe how to adapt your code to that component to version v2.0.0:
Changes in the `Executor` interface impact the `DefaultExecute` struct. This struct is a ready-to-go component provided by the _go-ansible_ library for executing Ansible commands. The following sections provide details on adapting your code to this component for version v2.0.0

### Removed the _ShowDuration_ attribute
Refer [here](#changes-on-the-executor-interface) to know more about the changes on the `Executor` interface.

The `DefaultExacute` has removed the attribute `ShowDuration`, as previously announced. Starting from version v2.0.0, to measure the duration of the execution you should utilize the `measure.ExecutiorTimeMeasurement` component.
### Removing the _ShowDuration_ attribute

For guidance on how to use the `ExecutiorTimeMeasurement` decorator component, please refer to the example [ansibleplaybook-time-measurement](https://github.com/apenella/go-ansible/blob/master/examples/ansibleplaybook-time-measurement/ansibleplaybook-time-measurement.go) to know how to use it.
As announced in prior go-ansible versions, the `DefaultExecute` has removed the ShowDuration attribute.

### New attribute _Output_ for printing execution results
Starting from version _v2.0.0_, to measure the duration of the execution, you should use the `ExecutorTimeMeasurement` struct. This struct acts as a decorator over the `Executor` and is available in the `github.com/apenella/go-ansible/pkg/execute/measure` package.

Inside the `DefaultExecute` struct, the `Execute` method has undergone an update that removes the `resultsFunc` attribute. This attribute was previously of type `stdoutcallback.StdoutCallbackResultsFunc` and was responsible for printing the execution's output. Instead, a new attribute named `Output` has been introduced to the struct.

The `Output` attribute is of type `results.ResultsOutputer`, which represents an interface defined as follows:

```go
// OptionsFunc is a function that can be used to configure a ResultsOutputer struct
type OptionsFunc func(ResultsOutputer)
For guidance on how to use the ExecutorTimeMeasurement, please refer to the [ansibleplaybook-time-measurement](https://github.com/apenella/go-ansible/blob/master/examples/ansibleplaybook-time-measurement/ansibleplaybook-time-measurement.go) example.

// ResultsOutputer is the interface that must implements an struct to print the execution results
type ResultsOutputer interface {
Print(ctx context.Context, reader io.Reader, writer io.Writer, options ...OptionsFunc) error
}
```
### New attribute _Output_ for printing execution results

If the `Output` attribute is not explicitly specified within the `DefaultExecute` struct, the default behaviour uses the `DefaultResults` struct as the output mechanism. You can find this struct in the `github.com/apenella/go-ansible/pkg/execute/result/default` package, and it is responsible for handling the printing of execution output.
To align with the new `Executor` interface, the `DefaultExecute` struct now includes the `Output` attribute. This attribute, of type `ResultsOutputer`, manages the output of Ansible commands. The definition for `ResultsOutputer` can be found in the `github.com/apenella/go-ansible/pkg/execute/result` package.

For further details on the `DefaultResults` struct, please refer to the section that discusses the package reorganization at [github.com/apenella/go-ansible/pkg/stdoutcallback](#githubcomapenellago-ansiblepkgstdoutcallback).
When the `Output` attribute is not specified in the `DefaultExecute` struct, it uses the `DefaultResults` struct as the output mechanism. You can find this struct in the `github.com/apenella/go-ansible/pkg/execute/result/default` package.

### New attribute _Exec_ for running external commands

Expand Down

0 comments on commit e1a0d38

Please sign in to comment.