gitlab.com/jfprevost/gitlab-runner-notlscheck@v11.11.4+incompatible/common/shell.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/sirupsen/logrus"
     7  
     8  	"gitlab.com/gitlab-org/gitlab-runner/helpers"
     9  )
    10  
    11  type ShellConfiguration struct {
    12  	Environment   []string
    13  	DockerCommand []string
    14  	Command       string
    15  	Arguments     []string
    16  	PassFile      bool
    17  	Extension     string
    18  }
    19  
    20  type ShellType int
    21  
    22  const (
    23  	NormalShell ShellType = iota
    24  	LoginShell
    25  )
    26  
    27  func (s *ShellConfiguration) GetCommandWithArguments() []string {
    28  	parts := []string{s.Command}
    29  	for _, arg := range s.Arguments {
    30  		parts = append(parts, arg)
    31  	}
    32  	return parts
    33  }
    34  
    35  func (s *ShellConfiguration) String() string {
    36  	return helpers.ToYAML(s)
    37  }
    38  
    39  type ShellScriptInfo struct {
    40  	Shell           string
    41  	Build           *Build
    42  	Type            ShellType
    43  	User            string
    44  	RunnerCommand   string
    45  	PreCloneScript  string
    46  	PreBuildScript  string
    47  	PostBuildScript string
    48  }
    49  
    50  type Shell interface {
    51  	GetName() string
    52  	GetFeatures(features *FeaturesInfo)
    53  	IsDefault() bool
    54  
    55  	GetConfiguration(info ShellScriptInfo) (*ShellConfiguration, error)
    56  	GenerateScript(buildStage BuildStage, info ShellScriptInfo) (string, error)
    57  }
    58  
    59  var shells map[string]Shell
    60  
    61  func RegisterShell(shell Shell) {
    62  	logrus.Debugln("Registering", shell.GetName(), "shell...")
    63  
    64  	if shells == nil {
    65  		shells = make(map[string]Shell)
    66  	}
    67  	if shells[shell.GetName()] != nil {
    68  		panic("Shell already exist: " + shell.GetName())
    69  	}
    70  	shells[shell.GetName()] = shell
    71  }
    72  
    73  func GetShell(shell string) Shell {
    74  	if shells == nil {
    75  		return nil
    76  	}
    77  
    78  	return shells[shell]
    79  }
    80  
    81  func GetShells() []string {
    82  	names := []string{}
    83  	if shells != nil {
    84  		for name := range shells {
    85  			names = append(names, name)
    86  		}
    87  	}
    88  	return names
    89  }
    90  
    91  func GetShellConfiguration(info ShellScriptInfo) (*ShellConfiguration, error) {
    92  	shell := GetShell(info.Shell)
    93  	if shell == nil {
    94  		return nil, fmt.Errorf("shell %s not found", info.Shell)
    95  	}
    96  
    97  	return shell.GetConfiguration(info)
    98  }
    99  
   100  func GenerateShellScript(buildStage BuildStage, info ShellScriptInfo) (string, error) {
   101  	shell := GetShell(info.Shell)
   102  	if shell == nil {
   103  		return "", fmt.Errorf("shell %s not found", info.Shell)
   104  	}
   105  
   106  	return shell.GenerateScript(buildStage, info)
   107  }
   108  
   109  func GetDefaultShell() string {
   110  	if shells == nil {
   111  		panic("no shells defined")
   112  	}
   113  
   114  	for _, shell := range shells {
   115  		if shell.IsDefault() {
   116  			return shell.GetName()
   117  		}
   118  	}
   119  	panic("no default shell defined")
   120  }