github.com/joshdk/godel@v0.0.0-20170529232908-862138a45aee/apps/distgo/cmd/runner.go (about)

     1  // Copyright 2016 Palantir Technologies, Inc.
     2  //
     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  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cmd
    16  
    17  import (
    18  	"fmt"
    19  	"io"
    20  
    21  	"github.com/palantir/godel/apps/distgo/params"
    22  )
    23  
    24  type ProcessFunc func(f func(buildSpec params.ProductBuildSpecWithDeps, stdout io.Writer) error) BuildFunc
    25  type BuildFunc func(buildSpecWithDeps []params.ProductBuildSpecWithDeps, stdout io.Writer) error
    26  
    27  // ProcessSerially returns a BuildFunc that processes each of the provided specs in order using the provided function.
    28  // If the function returns an error for any of the specifications, the function immediately returns that error.
    29  func ProcessSerially(f func(buildSpec params.ProductBuildSpecWithDeps, stdout io.Writer) error) BuildFunc {
    30  	return func(buildSpec []params.ProductBuildSpecWithDeps, stdout io.Writer) error {
    31  		for _, currSpec := range buildSpec {
    32  			if err := f(currSpec, stdout); err != nil {
    33  				return err
    34  			}
    35  		}
    36  		return nil
    37  	}
    38  }
    39  
    40  type SpecErrors struct {
    41  	Errors map[string]error
    42  }
    43  
    44  func (e *SpecErrors) Error() string {
    45  	return fmt.Sprintf("%v", e.Errors)
    46  }
    47  
    48  // ProcessSeriallyBatchErrors returns a BuildFunc that processes each of the provided specs in order using the provided
    49  // function. If the function returns an error for any of the specifications, it is stored, but the function will
    50  // will continue processing the provided specifications. The function return nil if no errors occurred; otherwise, it
    51  // returns a SpecErrors error that contains the individual errors.
    52  func ProcessSeriallyBatchErrors(f func(buildSpec params.ProductBuildSpecWithDeps, stdout io.Writer) error) BuildFunc {
    53  	return func(buildSpec []params.ProductBuildSpecWithDeps, stdout io.Writer) error {
    54  		errors := make(map[string]error)
    55  		for _, currSpec := range buildSpec {
    56  			if err := f(currSpec, stdout); err != nil {
    57  				errors[currSpec.Spec.ProductName] = err
    58  			}
    59  		}
    60  		if len(errors) > 0 {
    61  			return &SpecErrors{Errors: errors}
    62  		}
    63  		return nil
    64  	}
    65  }