github.com/openshift/source-to-image@v1.4.1-0.20240516041539-bf52fc02204e/pkg/build/strategies/onbuild/entrypoint.go (about)

     1  package onbuild
     2  
     3  import (
     4  	"errors"
     5  	"path/filepath"
     6  	"regexp"
     7  
     8  	"github.com/openshift/source-to-image/pkg/util/fs"
     9  	utillog "github.com/openshift/source-to-image/pkg/util/log"
    10  )
    11  
    12  var log = utillog.StderrLog
    13  
    14  var validEntrypoints = []*regexp.Regexp{
    15  	regexp.MustCompile(`^run(\.sh)?$`),
    16  	regexp.MustCompile(`^start(\.sh)?$`),
    17  	regexp.MustCompile(`^exec(\.sh)?$`),
    18  	regexp.MustCompile(`^execute(\.sh)?$`),
    19  }
    20  
    21  // GuessEntrypoint tries to guess the valid entrypoint from the source code
    22  // repository. The valid entrypoints are defined above (run,start,exec,execute)
    23  func GuessEntrypoint(fs fs.FileSystem, sourceDir string) (string, error) {
    24  	files, err := fs.ReadDir(sourceDir)
    25  	if err != nil {
    26  		return "", err
    27  	}
    28  	for _, f := range files {
    29  		if f.IsDir() || !f.Mode().IsRegular() {
    30  			continue
    31  		}
    32  		if isValidEntrypoint(fs, filepath.Join(sourceDir, f.Name())) {
    33  			log.V(2).Infof("Found valid ENTRYPOINT: %s", f.Name())
    34  			return f.Name(), nil
    35  		}
    36  	}
    37  	return "", errors.New("no valid entrypoint specified")
    38  }
    39  
    40  // isValidEntrypoint checks if the given file exists and if it is a regular
    41  // file. Valid ENTRYPOINT must be an executable file, so the executable bit must
    42  // be set.
    43  func isValidEntrypoint(fs fs.FileSystem, path string) bool {
    44  	stat, err := fs.Stat(path)
    45  	if err != nil {
    46  		return false
    47  	}
    48  	found := false
    49  	for _, pattern := range validEntrypoints {
    50  		if pattern.MatchString(stat.Name()) {
    51  			found = true
    52  			break
    53  		}
    54  	}
    55  	if !found {
    56  		return false
    57  	}
    58  	mode := stat.Mode()
    59  	return mode&0111 != 0
    60  }