tee.go 1015 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. Copyright (c) 2014 VMware, Inc. All Rights Reserved.
  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. // Tee works like Unix tee; it forwards all progress reports it receives to the
  15. // specified sinks
  16. func Tee(s1, s2 Sinker) Sinker {
  17. fn := func() chan<- Report {
  18. d1 := s1.Sink()
  19. d2 := s2.Sink()
  20. u := make(chan Report)
  21. go tee(u, d1, d2)
  22. return u
  23. }
  24. return SinkFunc(fn)
  25. }
  26. func tee(u <-chan Report, d1, d2 chan<- Report) {
  27. defer close(d1)
  28. defer close(d2)
  29. for r := range u {
  30. d1 <- r
  31. d2 <- r
  32. }
  33. }