mixed.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package progress
  14. import (
  15. "context"
  16. "fmt"
  17. "github.com/docker/cli/cli/streams"
  18. "github.com/docker/compose/v2/pkg/api"
  19. )
  20. // NewMixedWriter creates a Writer which allows to mix output from progress.Writer with a api.LogConsumer
  21. func NewMixedWriter(out *streams.Out, consumer api.LogConsumer, dryRun bool) Writer {
  22. isTerminal := out.IsTerminal()
  23. if Mode != ModeAuto || !isTerminal {
  24. return &plainWriter{
  25. out: out,
  26. done: make(chan bool),
  27. dryRun: dryRun,
  28. }
  29. }
  30. return &mixedWriter{
  31. out: consumer,
  32. done: make(chan bool),
  33. dryRun: dryRun,
  34. }
  35. }
  36. type mixedWriter struct {
  37. done chan bool
  38. dryRun bool
  39. out api.LogConsumer
  40. }
  41. func (p *mixedWriter) Start(ctx context.Context) error {
  42. select {
  43. case <-ctx.Done():
  44. return ctx.Err()
  45. case <-p.done:
  46. return nil
  47. }
  48. }
  49. func (p *mixedWriter) Event(e Event) {
  50. p.out.Status("", fmt.Sprintf("%s %s %s", e.ID, e.Text, SuccessColor(e.StatusText)))
  51. }
  52. func (p *mixedWriter) Events(events []Event) {
  53. for _, e := range events {
  54. p.Event(e)
  55. }
  56. }
  57. func (p *mixedWriter) TailMsgf(msg string, args ...interface{}) {
  58. msg = fmt.Sprintf(msg, args...)
  59. p.out.Status("", WarningColor(msg))
  60. }
  61. func (p *mixedWriter) Stop() {
  62. p.done <- true
  63. }