Files
go-task/internal/output/style.go
Jay Anslow 74f5cf8f29 Add support for begin/end messages with grouped output
Fixes #647

This allows CI systems that support grouping (such as with [GitHub Actions's `::group::` command](https://docs.github.com/en/actions/learn-github-actions/workflow-commands-for-github-actions#grouping-log-lines) and [Azure Devops](https://docs.microsoft.com/en-us/azure/devops/pipelines/scripts/logging-commands?view=azure-devops&tabs=bash#formatting-commands)) to collapse all of the logs for a single task, to improve readability of logs

## Example

The following Taskfile

```
# Taskfile.yml
version: 3
output:
  group:
    begin: "::group::{{ .TASK }}"
    end: "::endgroup::"
tasks:
  default:
    cmds:
      - "echo 'Hello, World!'"
```

Results in the following output
```bash
$ task
task: [default] echo 'Hello, World!'
::group::default
Hello, World!
::endgroup::
```

See [this GitHub Actions job](https://github.com/janslow/task/runs/4811059609?check_suite_focus=true) for a full example

<img width="771" alt="image" src="https://user-images.githubusercontent.com/1253367/149429832-6cb0c1b5-0758-442e-9375-c4daa65771bc.png">
<img width="394" alt="image" src="https://user-images.githubusercontent.com/1253367/149429851-1d5d2ab5-9095-4795-9b57-f91750720d40.png">
2022-01-14 00:22:14 +00:00

86 lines
2.0 KiB
Go

package output
import (
"fmt"
)
// Style of the Task output
type Style struct {
// Name of the Style.
Name string `yaml:"-"`
// Group specific style
Group GroupStyle
}
// Build the Output for the requested Style.
func (s *Style) Build() (Output, error) {
switch s.Name {
case "interleaved", "":
return Interleaved{}, s.ensureGroupStyleUnset()
case "group":
return Group{
Begin: s.Group.Begin,
End: s.Group.End,
}, nil
case "prefixed":
return Prefixed{}, s.ensureGroupStyleUnset()
default:
return nil, fmt.Errorf(`task: output style %q not recognized`, s.Name)
}
}
func (s *Style) ensureGroupStyleUnset() error {
if s.Group.IsSet() {
return fmt.Errorf("task: output style %q does not support the group begin/end parameter", s.Name)
}
return nil
}
// IsSet returns true if and only if a custom output style is set.
func (s *Style) IsSet() bool {
return s.Name != ""
}
// UnmarshalYAML implements yaml.Unmarshaler
// It accepts a scalar node representing the Style.Name or a mapping node representing the GroupStyle.
func (s *Style) UnmarshalYAML(unmarshal func(interface{}) error) error {
var name string
if err := unmarshal(&name); err == nil {
return s.UnmarshalText([]byte(name))
}
var tmp struct {
Group *GroupStyle
}
if err := unmarshal(&tmp); err != nil {
return fmt.Errorf("task: output style must be a string or mapping with a \"group\" key: %w", err)
}
if tmp.Group == nil {
return fmt.Errorf("task: output style must have the \"group\" key when in mapping form")
}
*s = Style{
Name: "group",
Group: *tmp.Group,
}
return nil
}
// UnmarshalText implements encoding.TextUnmarshaler
// It accepts the Style.Node
func (s *Style) UnmarshalText(text []byte) error {
tmp := Style{Name: string(text)}
if _, err := tmp.Build(); err != nil {
return err
}
return nil
}
// GroupStyle is the style options specific to the Group style.
type GroupStyle struct{
Begin, End string
}
// IsSet returns true if and only if a custom output style is set.
func (g *GroupStyle) IsSet() bool {
return g != nil && *g != GroupStyle{}
}