github.com/beauknowssoftware/makehcl@v0.0.0-20200322000747-1b9bb1e1c008/internal/parse/paraseCommand.go (about)

     1  package parse
     2  
     3  import (
     4  	"github.com/pkg/errors"
     5  
     6  	"github.com/beauknowssoftware/makehcl/internal/definition"
     7  	"github.com/hashicorp/hcl/v2"
     8  )
     9  
    10  const (
    11  	commandAttributeName      = "command"
    12  	dependenciesAttributeName = "dependencies"
    13  	environmentAttributeName  = "environment"
    14  )
    15  
    16  var (
    17  	commandSchema = hcl.BodySchema{
    18  		Attributes: []hcl.AttributeSchema{
    19  			{
    20  				Name:     commandAttributeName,
    21  				Required: false,
    22  			},
    23  			{
    24  				Name:     dependenciesAttributeName,
    25  				Required: false,
    26  			},
    27  			{
    28  				Name:     environmentAttributeName,
    29  				Required: false,
    30  			},
    31  		},
    32  	}
    33  )
    34  
    35  func fillCommand(name string, body hcl.Body, ctx *hcl.EvalContext) (*definition.Command, error) {
    36  	con, diag := body.Content(&commandSchema)
    37  	if diag.HasErrors() {
    38  		return nil, diag
    39  	}
    40  
    41  	var c definition.Command
    42  	c.Name = name
    43  
    44  	for name, attr := range con.Attributes {
    45  		switch name {
    46  		case environmentAttributeName:
    47  			environment, err := evaluateStringMap(attr.Expr, ctx)
    48  			if err != nil {
    49  				err = errors.Wrapf(err, "failed to evaluate %v", environmentAttributeName)
    50  				return nil, err
    51  			}
    52  
    53  			c.Environment = environment
    54  		case commandAttributeName:
    55  			command, err := evaluateString(attr.Expr, ctx)
    56  			if err != nil {
    57  				err = errors.Wrapf(err, "failed to evaluate %v", commandAttributeName)
    58  				return nil, err
    59  			}
    60  
    61  			c.Command = command
    62  		case dependenciesAttributeName:
    63  			dependencies, err := evaluateStringArray(attr.Expr, ctx)
    64  			if err != nil {
    65  				err = errors.Wrapf(err, "failed to evaluate %v", dependenciesAttributeName)
    66  				return nil, err
    67  			}
    68  
    69  			c.Dependencies = dependencies
    70  		}
    71  	}
    72  
    73  	return &c, nil
    74  }
    75  
    76  func constructCommand(blk *hcl.Block, ctx *hcl.EvalContext) (*definition.Command, error) {
    77  	return fillCommand(blk.Labels[0], blk.Body, ctx)
    78  }