get.porter.sh/porter@v1.3.0/pkg/exec/action.go (about)

     1  package exec
     2  
     3  import (
     4  	"get.porter.sh/porter/pkg/exec/builder"
     5  )
     6  
     7  var _ builder.ExecutableAction = Action{}
     8  var _ builder.BuildableAction = Action{}
     9  
    10  type Action struct {
    11  	Name  string
    12  	Steps []Step // using UnmarshalYAML so that we don't need a custom type per action
    13  }
    14  
    15  // MarshalYAML converts the action back to a YAML representation
    16  // install:
    17  //   exec:
    18  //     ...
    19  //   helm3:
    20  //     ...
    21  func (a Action) MarshalYAML() (interface{}, error) {
    22  	return map[string]interface{}{a.Name: a.Steps}, nil
    23  }
    24  
    25  // MakeSteps builds a slice of Steps for data to be unmarshaled into.
    26  func (a Action) MakeSteps() interface{} {
    27  	return &[]Step{}
    28  }
    29  
    30  // UnmarshalYAML takes any yaml in this form
    31  // ACTION:
    32  // - exec: ...
    33  // and puts the steps into the Action.Steps field
    34  func (a *Action) UnmarshalYAML(unmarshal func(interface{}) error) error {
    35  	results, err := builder.UnmarshalAction(unmarshal, a)
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	for actionName, action := range results {
    41  		a.Name = actionName
    42  		for _, result := range action {
    43  			step := result.(*[]Step)
    44  			a.Steps = append(a.Steps, *step...)
    45  		}
    46  		break // There is only 1 action
    47  	}
    48  	return nil
    49  }
    50  
    51  func (a Action) GetSteps() []builder.ExecutableStep {
    52  	steps := make([]builder.ExecutableStep, len(a.Steps))
    53  	for i := range a.Steps {
    54  		steps[i] = a.Steps[i]
    55  	}
    56  
    57  	return steps
    58  }
    59  
    60  // Actions is a set of actions, and the steps, passed from Porter.
    61  type Actions []Action
    62  
    63  // UnmarshalYAML takes chunks of a porter.yaml file associated with this mixin
    64  // and populates it on the current action set.
    65  // install:
    66  //   exec:
    67  //     ...
    68  //   exec:
    69  //     ...
    70  // upgrade:
    71  //   exec:
    72  //     ...
    73  func (a *Actions) UnmarshalYAML(unmarshal func(interface{}) error) error {
    74  	results, err := builder.UnmarshalAction(unmarshal, Action{})
    75  	if err != nil {
    76  		return err
    77  	}
    78  
    79  	for actionName, action := range results {
    80  		for _, result := range action {
    81  			s := result.(*[]Step)
    82  			*a = append(*a, Action{
    83  				Name:  actionName,
    84  				Steps: *s,
    85  			})
    86  		}
    87  	}
    88  	return nil
    89  }
    90  
    91  var (
    92  	_ builder.HasOrderedArguments = Step{}
    93  	_ builder.ExecutableStep      = Step{}
    94  	_ builder.StepWithOutputs     = Step{}
    95  	_ builder.HasEnvironmentVars  = Step{}
    96  )
    97  
    98  type Step struct {
    99  	Instruction `yaml:"exec"`
   100  }
   101  
   102  type Instruction struct {
   103  	Description     string            `yaml:"description"`
   104  	Command         string            `yaml:"command"`
   105  	WorkingDir      string            `yaml:"dir,omitempty"`
   106  	Arguments       []string          `yaml:"arguments,omitempty"`
   107  	SuffixArguments []string          `yaml:"suffix-arguments,omitempty"`
   108  	Flags           builder.Flags     `yaml:"flags,omitempty"`
   109  	EnvironmentVars map[string]string `yaml:"envs,omitempty"`
   110  	Outputs         []Output          `yaml:"outputs,omitempty"`
   111  	SuppressOutput  bool              `yaml:"suppress-output,omitempty"`
   112  
   113  	// Allow the user to ignore some errors
   114  	builder.IgnoreErrorHandler `yaml:"ignoreError,omitempty"`
   115  }
   116  
   117  func (s Step) GetCommand() string {
   118  	return s.Command
   119  }
   120  
   121  func (s Step) GetArguments() []string {
   122  	return s.Arguments
   123  }
   124  
   125  func (s Step) GetSuffixArguments() []string {
   126  	return s.SuffixArguments
   127  }
   128  
   129  func (s Step) GetFlags() builder.Flags {
   130  	return s.Flags
   131  }
   132  
   133  func (s Step) GetEnvironmentVars() map[string]string {
   134  	return s.EnvironmentVars
   135  }
   136  
   137  func (s Step) SuppressesOutput() bool {
   138  	return s.SuppressOutput
   139  }
   140  
   141  func (s Step) GetOutputs() []builder.Output {
   142  	outputs := make([]builder.Output, len(s.Outputs))
   143  	for i := range s.Outputs {
   144  		outputs[i] = s.Outputs[i]
   145  	}
   146  	return outputs
   147  }
   148  
   149  func (s Step) GetWorkingDir() string {
   150  	return s.WorkingDir
   151  }
   152  
   153  var _ builder.OutputRegex = Output{}
   154  var _ builder.OutputFile = Output{}
   155  var _ builder.OutputJsonPath = Output{}
   156  
   157  type Output struct {
   158  	Name     string `yaml:"name"`
   159  	FilePath string `yaml:"path,omitempty"`
   160  	JsonPath string `yaml:"jsonPath,omitempty"`
   161  	Regex    string `yaml:"regex,omitempty"`
   162  }
   163  
   164  func (o Output) GetName() string {
   165  	return o.Name
   166  }
   167  
   168  func (o Output) GetFilePath() string {
   169  	return o.FilePath
   170  }
   171  
   172  func (o Output) GetJsonPath() string {
   173  	return o.JsonPath
   174  }
   175  
   176  func (o Output) GetRegex() string {
   177  	return o.Regex
   178  }