github.com/alloyci/alloy-runner@v1.0.1-0.20180222164613-925503ccafd6/executors/docker/machine/name.go (about)

     1  package machine
     2  
     3  import (
     4  	"crypto/rand"
     5  	"fmt"
     6  	"strings"
     7  	"time"
     8  
     9  	"gitlab.com/gitlab-org/gitlab-runner/common"
    10  )
    11  
    12  func machineFormat(runner string, template string) string {
    13  	if runner != "" {
    14  		return "runner-" + strings.ToLower(runner) + "-" + template
    15  	}
    16  	return template
    17  }
    18  
    19  func machineFilter(config *common.RunnerConfig) string {
    20  	return machineFormat(config.ShortDescription(), config.Machine.MachineName)
    21  }
    22  
    23  func matchesMachineFilter(name, filter string) bool {
    24  	var query string
    25  	if n, _ := fmt.Sscanf(name, filter, &query); n == 1 {
    26  		return true
    27  	}
    28  	return false
    29  }
    30  
    31  func filterMachineList(machines []string, filter string) (newMachines []string) {
    32  	newMachines = make([]string, 0, len(machines))
    33  	for _, machine := range machines {
    34  		if matchesMachineFilter(machine, filter) {
    35  			newMachines = append(newMachines, machine)
    36  		}
    37  	}
    38  	return
    39  }
    40  
    41  func newMachineName(config *common.RunnerConfig) string {
    42  	r := make([]byte, 4)
    43  	rand.Read(r)
    44  	t := time.Now().Unix()
    45  	return fmt.Sprintf(machineFilter(config), fmt.Sprintf("%d-%x", t, r))
    46  }