grim/convey
Clone
Summary
Browse
Changes
Graph
remove the helper function for running plans
2021-12-23, Gary Kramlich
eff7fbacd5ee
remove the helper function for running plans
package
script
import
(
"os"
"path/filepath"
gort
"runtime"
"strings"
log
"github.com/sirupsen/logrus"
"keep.imfreedom.org/grim/convey/environment"
"keep.imfreedom.org/grim/convey/exec"
"keep.imfreedom.org/grim/convey/runtime"
"keep.imfreedom.org/grim/convey/yaml"
)
type
Shell
struct
{
Commands
yaml
.
StringOrSlice
`yaml:"commands"`
Environment
yaml
.
StringOrSlice
`yaml:"environment"`
Filename
string
`yaml:"filename"`
Shell
string
`yaml:"shell"`
}
func
(
s
*
Shell
)
Execute
(
name
string
,
logger
*
log
.
Entry
,
stageEnv
environment
.
Environment
,
rt
*
runtime
.
Runtime
)
error
{
// Create a new environment based on the stage's environment. Then merge
// the task's environment overriding anything from the stage. Finally merge
// the runtime environment which holds the environment from the command line.
env
:=
stageEnv
.
Copy
().
MergeSlice
(
s
.
Environment
).
Merge
(
rt
.
Environment
)
filename
:=
filepath
.
Join
(
rt
.
ConfigPath
,
s
.
Filename
)
if
!
strings
.
HasPrefix
(
filename
,
rt
.
ConfigPath
)
{
return
ErrScriptOutsideOfWorkspace
}
if
filename
==
""
{
file
,
err
:=
sliceToFile
(
s
.
Commands
,
rt
.
ConfigPath
,
env
)
if
err
!=
nil
{
return
err
}
filename
=
file
defer
os
.
Remove
(
filename
)
}
shell
,
err
:=
findInterpreter
(
s
.
Shell
)
if
err
!=
nil
{
return
err
}
if
shell
==
""
{
return
ErrFailedToDetermineShell
}
return
exec
.
Run
(
name
,
[]
string
{
shell
,
filename
},
rt
.
Timeout
)
}
func
(
s
*
Shell
)
New
()
runtime
.
Task
{
return
&
Shell
{}
}
func
(
s
*
Shell
)
Valid
()
error
{
if
gort
.
GOOS
==
"windows"
{
return
ErrNotSupportedOnWindows
}
if
s
.
Shell
==
""
{
s
.
Shell
=
"/bin/sh"
}
if
len
(
s
.
Commands
)
==
0
&&
s
.
Filename
==
""
{
return
ErrNoCommandsOrFilename
}
if
len
(
s
.
Commands
)
>
0
&&
s
.
Filename
!=
""
{
return
ErrOnlyFilenameOrCommands
}
return
nil
}
func
(
s
*
Shell
)
Deprecated
()
error
{
return
nil
}