grim/convey
Clone
Summary
Browse
Changes
Graph
Added tag v0.14.0-alpha1 for changeset 335eaa5698de
2018-02-19, Gary Kramlich
7b5ccc461d0e
Added tag v0.14.0-alpha1 for changeset 335eaa5698de
// Convey
// Copyright 2016-2018 Gary Kramlich <grim@reaperworld.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package
script
import
(
"strings"
"github.com/kballard/go-shellquote"
"bitbucket.org/rw_grim/convey/aws"
"bitbucket.org/rw_grim/convey/docker"
"bitbucket.org/rw_grim/convey/tasks"
)
// ParseFunc defines a function that can parse a shell command into a convey
// task.
type
ParseFunc
func
(
argv
[]
string
)
(
tasks
.
Task
,
error
)
var
(
commands
=
map
[
string
]
ParseFunc
{
"docker"
:
docker
.
ParseCommand
,
"aws"
:
aws
.
ParseCommand
,
}
)
// Parse will process a slice of commands and turn it into the proper tasks for
// convey to run.
func
Parse
(
image
,
shell
string
,
script
[]
string
)
([]
tasks
.
Task
,
error
)
{
newTasks
:=
[]
tasks
.
Task
{}
currentScript
:=
[]
string
{}
for
_
,
cmd
:=
range
script
{
// trim whitespace from the command
clean
:=
strings
.
TrimSpace
(
cmd
)
// handle weirdo commands that are entirely wrapped in $()
if
strings
.
HasPrefix
(
clean
,
"$("
)
&&
strings
.
HasSuffix
(
clean
,
")"
)
{
clean
=
strings
.
TrimPrefix
(
clean
,
"$("
)
clean
=
strings
.
TrimSuffix
(
clean
,
")"
)
}
// handle weirdo commands that are entirely wrapped in `'s
if
strings
.
HasPrefix
(
clean
,
"`"
)
&&
strings
.
HasSuffix
(
clean
,
"`"
)
{
clean
=
strings
.
TrimPrefix
(
clean
,
"`"
)
clean
=
strings
.
TrimSuffix
(
clean
,
"`"
)
}
argv
,
err
:=
shellquote
.
Split
(
clean
)
if
err
!=
nil
{
return
nil
,
err
}
if
len
(
argv
)
==
0
{
continue
}
currentCmd
:=
strings
.
TrimSpace
(
argv
[
0
])
if
parser
,
ok
:=
commands
[
currentCmd
];
ok
{
if
len
(
currentScript
)
>
0
{
newTasks
=
append
(
newTasks
,
&
docker
.
Run
{
Shell
:
shell
,
Script
:
currentScript
,
Image
:
image
,
WorkDir
:
"/workspace"
,
})
// reset our script
currentScript
=
[]
string
{}
}
task
,
err
:=
parser
(
argv
)
if
err
!=
nil
{
return
nil
,
err
}
newTasks
=
append
(
newTasks
,
task
)
}
else
{
currentScript
=
append
(
currentScript
,
cmd
)
}
}
// if we have any unfinished script commands create a task for them
if
len
(
currentScript
)
>
0
{
newTasks
=
append
(
newTasks
,
&
docker
.
Run
{
Shell
:
shell
,
Script
:
currentScript
,
Image
:
image
,
WorkDir
:
"/workspace"
,
})
}
return
newTasks
,
nil
}