github.com/diggerhq/digger/libs@v0.0.0-20240604170430-9d61cdf01cc5/orchestrator/utils.go (about)

     1  package orchestrator
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"strings"
     7  )
     8  
     9  func ParseProjectName(comment string) string {
    10  	re := regexp.MustCompile(`-p ([0-9a-zA-Z\-_]+)`)
    11  	match := re.FindStringSubmatch(comment)
    12  	if len(match) > 1 {
    13  		return match[1]
    14  	}
    15  	return ""
    16  }
    17  
    18  type DiggerCommand string
    19  
    20  const DiggerCommandNoop DiggerCommand = "noop"
    21  const DiggerCommandPlan DiggerCommand = "plan"
    22  const DiggerCommandApply DiggerCommand = "apply"
    23  const DiggerCommandLock DiggerCommand = "lock"
    24  const DiggerCommandUnlock DiggerCommand = "unlock"
    25  
    26  func GetCommandFromComment(comment string) (*DiggerCommand, error) {
    27  	supportedCommands := map[string]DiggerCommand{
    28  		"digger noop":   DiggerCommandNoop,
    29  		"digger plan":   DiggerCommandPlan,
    30  		"digger apply":  DiggerCommandApply,
    31  		"digger unlock": DiggerCommandUnlock,
    32  		"digger lock":   DiggerCommandLock,
    33  	}
    34  	diggerCommand := strings.ToLower(comment)
    35  	diggerCommand = strings.TrimSpace(diggerCommand)
    36  	for command, value := range supportedCommands {
    37  		if strings.HasPrefix(diggerCommand, command) {
    38  			return &value, nil
    39  		}
    40  	}
    41  	return nil, fmt.Errorf("Unrecognised command: %v", comment)
    42  }
    43  
    44  func GetCommandFromJob(job Job) (*DiggerCommand, error) {
    45  	supportedCommands := map[string]DiggerCommand{
    46  		"digger noop":   DiggerCommandNoop,
    47  		"digger plan":   DiggerCommandPlan,
    48  		"digger apply":  DiggerCommandApply,
    49  		"digger unlock": DiggerCommandUnlock,
    50  		"digger lock":   DiggerCommandLock,
    51  	}
    52  
    53  	if len(job.Commands) == 0 {
    54  		res := DiggerCommandNoop
    55  		return &res, nil
    56  	}
    57  
    58  	diggerCommands := job.Commands
    59  	for command, value := range supportedCommands {
    60  		for _, diggerCommand := range diggerCommands {
    61  			if strings.HasPrefix(diggerCommand, command) {
    62  				return &value, nil
    63  			}
    64  		}
    65  	}
    66  	return nil, fmt.Errorf("could not figure out command: %v", job.Commands)
    67  }