github.com/konsorten/ktn-build-info@v1.0.11/ver/run.go (about)

     1  package ver
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	log "github.com/sirupsen/logrus"
     8  )
     9  
    10  func RunCurrentDirectory() error {
    11  	// read actions
    12  	actions, err := ReadVersionInfoActions()
    13  
    14  	if err != nil {
    15  		return err
    16  	}
    17  
    18  	if actions == nil {
    19  		return fmt.Errorf("No %v found", VersionInfoYamlFilename)
    20  	}
    21  
    22  	// nothing to do?
    23  	if len(actions.Inputs) <= 0 {
    24  		return fmt.Errorf("No input actions found")
    25  	}
    26  
    27  	if len(actions.Outputs) <= 0 {
    28  		return fmt.Errorf("No output actions found")
    29  	}
    30  
    31  	// processing inputs
    32  	result := MakeVersionInformation()
    33  
    34  	for _, i := range actions.Inputs {
    35  		log.Debugf("Processing input: %v [%v]", i.Name, i.Parameters)
    36  
    37  		// resolve spec
    38  		spec := GetInputSpec(i.Name)
    39  
    40  		if spec == nil {
    41  			return fmt.Errorf("Failed to resolve input action: %v", i.Name)
    42  		}
    43  
    44  		// run the action
    45  		err := spec.Action(result, i.Parameters)
    46  
    47  		if err != nil {
    48  			return fmt.Errorf("Failed to run input action %v: %v", i.Name, err)
    49  		}
    50  	}
    51  
    52  	// show information
    53  	log.Infof("Version: %v", result)
    54  
    55  	// validate
    56  	if ok, errors := result.IsValid(); !ok {
    57  		return fmt.Errorf("Build information is invalid: %v", strings.Join(errors, "; "))
    58  	}
    59  
    60  	// processing outputs
    61  	for _, o := range actions.Outputs {
    62  		log.Debugf("Processing output: %v [%v]", o.Name, o.Parameters)
    63  
    64  		// resolve spec
    65  		spec := GetOutputSpec(o.Name)
    66  
    67  		if spec == nil {
    68  			return fmt.Errorf("Failed to resolve output action: %v", o.Name)
    69  		}
    70  
    71  		// run the action
    72  		err := spec.Action(result, o.Parameters)
    73  
    74  		if err != nil {
    75  			return fmt.Errorf("Failed to run output action %v [%v]: %v", o.Name, o.Parameters, err)
    76  		}
    77  	}
    78  
    79  	return nil
    80  }