plain.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. "io"
  18. )
  19. type plainWriter struct {
  20. out io.Writer
  21. done chan bool
  22. dryRun bool
  23. }
  24. func (p *plainWriter) Start(ctx context.Context) error {
  25. select {
  26. case <-ctx.Done():
  27. return ctx.Err()
  28. case <-p.done:
  29. return nil
  30. }
  31. }
  32. func (p *plainWriter) Event(e Event) {
  33. prefix := ""
  34. if p.dryRun {
  35. prefix = "DRY RUN MODE - "
  36. }
  37. fmt.Fprintln(p.out, prefix, e.ID, e.Text, e.StatusText)
  38. }
  39. func (p *plainWriter) Events(events []Event) {
  40. for _, e := range events {
  41. p.Event(e)
  42. }
  43. }
  44. func (p *plainWriter) TailMsgf(m string, args ...interface{}) {
  45. prefix := ""
  46. if p.dryRun {
  47. prefix = DRYRUN_PREFIX
  48. }
  49. fmt.Fprintln(p.out, append([]interface{}{prefix, m}, args...)...)
  50. }
  51. func (p *plainWriter) Stop() {
  52. p.done <- true
  53. }