Commit 54eb320a authored by Kamil Trzciński's avatar Kamil Trzciński

Refactor shell script generate function: introduce ShellScriptInfo

parent 83195606
......@@ -47,9 +47,15 @@ func (s *ShellScript) String() string {
return helpers.ToYAML(s)
}
type ShellScriptInfo struct {
Shell string
Build *Build
Type ShellType
}
type Shell interface {
GetName() string
GenerateScript(build *Build, shellType ShellType) (*ShellScript, error)
GenerateScript(info ShellScriptInfo) (*ShellScript, error)
IsDefault() bool
}
......@@ -85,13 +91,13 @@ func GetShells() []string {
return names
}
func GenerateShellScript(name string, build *Build, shellType ShellType) (*ShellScript, error) {
shell := GetShell(name)
func GenerateShellScript(info ShellScriptInfo) (*ShellScript, error) {
shell := GetShell(info.Shell)
if shell == nil {
return nil, fmt.Errorf("shell %s not found", name)
return nil, fmt.Errorf("shell %s not found", info.Shell)
}
return shell.GenerateScript(build, shellType)
return shell.GenerateScript(info)
}
func GetDefaultShell() string {
......
......@@ -66,8 +66,10 @@ func init() {
options := executors.ExecutorOptions{
DefaultBuildsDir: "/builds",
SharedBuildsDir: false,
DefaultShell: "bash",
ShellType: common.NormalShell,
Shell: common.ShellScriptInfo{
Shell: "bash",
Type: common.NormalShell,
},
ShowHostname: true,
SupportedOptions: []string{"image", "services"},
}
......
......@@ -68,8 +68,10 @@ func init() {
options := executors.ExecutorOptions{
DefaultBuildsDir: "builds",
SharedBuildsDir: false,
DefaultShell: "bash",
ShellType: common.LoginShell,
Shell: common.ShellScriptInfo{
Shell: "bash",
Type: common.LoginShell,
},
ShowHostname: true,
SupportedOptions: []string{"image", "services"},
}
......
......@@ -16,8 +16,7 @@ import (
type ExecutorOptions struct {
DefaultBuildsDir string
SharedBuildsDir bool
DefaultShell string
ShellType common.ShellType
Shell common.ShellScriptInfo
ShowHostname bool
SupportedOptions []string
}
......@@ -150,8 +149,8 @@ func (e *AbstractExecutor) Errorln(args ...interface{}) {
}
func (e *AbstractExecutor) generateShellScript() error {
shell := helpers.StringOrDefault(e.Config.Shell, e.DefaultShell)
shellScript, err := common.GenerateShellScript(shell, e.Build, e.ShellType)
e.Shell.Shell = helpers.StringOrDefault(e.Config.Shell, e.Shell.Shell)
shellScript, err := common.GenerateShellScript(e.Shell)
if err != nil {
return err
}
......
......@@ -317,8 +317,10 @@ func init() {
options := executors.ExecutorOptions{
DefaultBuildsDir: "builds",
SharedBuildsDir: false,
DefaultShell: "bash",
ShellType: common.LoginShell,
Shell: common.ShellScriptInfo{
Shell: "bash",
Type: common.LoginShell,
},
ShowHostname: true,
}
......
......@@ -92,8 +92,10 @@ func init() {
options := executors.ExecutorOptions{
DefaultBuildsDir: "builds",
SharedBuildsDir: true,
DefaultShell: common.GetDefaultShell(),
ShellType: common.LoginShell,
Shell: common.ShellScriptInfo{
Shell: common.GetDefaultShell(),
Type: common.LoginShell,
},
ShowHostname: false,
}
......
......@@ -67,8 +67,10 @@ func init() {
options := executors.ExecutorOptions{
DefaultBuildsDir: "builds",
SharedBuildsDir: true,
DefaultShell: "bash",
ShellType: common.LoginShell,
Shell: common.ShellScriptInfo{
Shell: "bash",
Type: common.LoginShell,
},
ShowHostname: true,
}
......
......@@ -46,10 +46,11 @@ func (b *BashShell) writeCheckoutCmd(w io.Writer, build *common.Build) {
io.WriteString(w, fmt.Sprintf("git checkout -qf %s\n", build.Sha))
}
func (b *BashShell) GenerateScript(build *common.Build, shellType common.ShellType) (*common.ShellScript, error) {
func (b *BashShell) GenerateScript(info common.ShellScriptInfo) (*common.ShellScript, error) {
var buffer bytes.Buffer
w := bufio.NewWriter(&buffer)
build := info.Build
projectDir := build.FullProjectDir()
projectDir = helpers.ToSlash(projectDir)
gitDir := filepath.Join(projectDir, ".git")
......@@ -97,7 +98,7 @@ func (b *BashShell) GenerateScript(build *common.Build, shellType common.ShellTy
Command: "bash",
}
if shellType == common.LoginShell {
if info.Type == common.LoginShell {
script.Arguments = []string{"--login"}
}
......
......@@ -54,10 +54,11 @@ func (b *CmdShell) writeCheckoutCmd(w io.Writer, build *common.Build) {
b.writeCommandChecked(w, "git checkout -qf \"%s\"", build.Sha)
}
func (b *CmdShell) GenerateScript(build *common.Build, shellType common.ShellType) (*common.ShellScript, error) {
func (b *CmdShell) GenerateScript(info common.ShellScriptInfo) (*common.ShellScript, error) {
var buffer bytes.Buffer
w := bufio.NewWriter(&buffer)
build := info.Build
projectDir := build.FullProjectDir()
projectDir = helpers.ToBackslash(projectDir)
......
......@@ -52,10 +52,11 @@ func (b *PowerShell) writeCheckoutCmd(w io.Writer, build *common.Build) {
b.writeCommandChecked(w, "git checkout -qf \"%s\"", build.Sha)
}
func (b *PowerShell) GenerateScript(build *common.Build, shellType common.ShellType) (*common.ShellScript, error) {
func (b *PowerShell) GenerateScript(info common.ShellScriptInfo) (*common.ShellScript, error) {
var buffer bytes.Buffer
w := bufio.NewWriter(&buffer)
build := info.Build
projectDir := build.FullProjectDir()
projectDir = helpers.ToBackslash(projectDir)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment