github.com/vmware/govmomi@v0.37.1/vim25/progress/prefix.go (about)

     1  /*
     2  Copyright (c) 2014 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package progress
    18  
    19  import "fmt"
    20  
    21  type prefixedReport struct {
    22  	Report
    23  	prefix string
    24  }
    25  
    26  func (r prefixedReport) Detail() string {
    27  	if d := r.Report.Detail(); d != "" {
    28  		return fmt.Sprintf("%s: %s", r.prefix, d)
    29  	}
    30  
    31  	return r.prefix
    32  }
    33  
    34  func prefixLoop(upstream <-chan Report, downstream chan<- Report, prefix string) {
    35  	defer close(downstream)
    36  
    37  	for r := range upstream {
    38  		downstream <- prefixedReport{
    39  			Report: r,
    40  			prefix: prefix,
    41  		}
    42  	}
    43  }
    44  
    45  func Prefix(s Sinker, prefix string) Sinker {
    46  	fn := func() chan<- Report {
    47  		upstream := make(chan Report)
    48  		downstream := s.Sink()
    49  		go prefixLoop(upstream, downstream, prefix)
    50  		return upstream
    51  	}
    52  
    53  	return SinkFunc(fn)
    54  }