github.com/leg100/ots@v0.0.7-0.20210919080622-034055ced4bd/parse_apply_output.go (about)

     1  package ots
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"strconv"
     7  )
     8  
     9  var (
    10  	applyChangesRegex = regexp.MustCompile(`(?m)^Apply complete! Resources: (\d+) added, (\d+) changed, (\d+) destroyed.$`)
    11  )
    12  
    13  type apply struct {
    14  	adds, changes, deletions int
    15  }
    16  
    17  func parseApplyOutput(output string) (*apply, error) {
    18  	matches := applyChangesRegex.FindStringSubmatch(output)
    19  	if matches == nil {
    20  		return nil, fmt.Errorf("regexes unexpectedly did not match apply output")
    21  	}
    22  
    23  	adds, err := strconv.ParseInt(matches[1], 10, 0)
    24  	if err != nil {
    25  		return nil, err
    26  	}
    27  	changes, err := strconv.ParseInt(matches[2], 10, 0)
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  	deletions, err := strconv.ParseInt(matches[3], 10, 0)
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  
    36  	return &apply{
    37  		adds:      int(adds),
    38  		changes:   int(changes),
    39  		deletions: int(deletions),
    40  	}, nil
    41  }