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