grim/convey

Parents 31cbebf0c277
Children 02f6bf176f35
Add support for parsing script's that have full commands in $() and ``.
--- a/aws/ecr-get-login.go Tue Feb 13 22:42:39 2018 -0600
+++ b/aws/ecr-get-login.go Tue Feb 13 23:42:37 2018 -0600
@@ -34,9 +34,11 @@
// Executes the `aws ecr get-login` command and calls docker login with the
// results
func (ecr *ECRGetLogin) Execute(name string, logger *gomol.LogAdapter, env []string, st *state.State) error {
+ fullEnv := environment.Merge(env, st.GetEnv())
+
cmd := command.NewGenerator("aws", "ecr", "get-login")
if ecr.Region != "" {
- region, err := environment.Mapper(ecr.Region, env)
+ region, err := environment.Mapper(ecr.Region, fullEnv)
if err != nil {
return err
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/aws/parser.go Tue Feb 13 23:42:37 2018 -0600
@@ -0,0 +1,57 @@
+// 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 aws
+
+import (
+ "fmt"
+
+ "github.com/alecthomas/kingpin"
+
+ "bitbucket.org/rw_grim/convey/tasks"
+)
+
+// ParseCommand is a super stripped down kingpin parser that will parse aws
+// command line options and return a convey task or an error
+func ParseCommand(argv []string) (tasks.Task, error) {
+ app := kingpin.New("", "")
+
+ ecr := app.Command("ecr", "")
+
+ getLogin := ecr.Command("get-login", "")
+ region := getLogin.Flag("region", "").String()
+ getLogin.Flag("no-include-email", "").Bool()
+
+ cmd, err := app.Parse(argv[1:])
+ if err != nil {
+ return nil, err
+ }
+
+ var task tasks.Task
+
+ switch cmd {
+ case "ecr get-login":
+ task = &ECRGetLogin{
+ Region: *region,
+ }
+ }
+
+ if task != nil {
+ return task, nil
+ }
+
+ return nil, fmt.Errorf("unable to parse aws command line '%v'", argv)
+}
--- a/script/script.go Tue Feb 13 22:42:39 2018 -0600
+++ b/script/script.go Tue Feb 13 23:42:37 2018 -0600
@@ -21,6 +21,7 @@
"github.com/kballard/go-shellquote"
+ "bitbucket.org/rw_grim/convey/aws"
"bitbucket.org/rw_grim/convey/docker"
"bitbucket.org/rw_grim/convey/tasks"
)
@@ -30,6 +31,7 @@
var (
commands = map[string]ParseFunc{
"docker": docker.ParseCommand,
+ "aws": aws.ParseCommand,
}
)
@@ -40,7 +42,22 @@
currentScript := []string{}
for _, cmd := range script {
- argv, err := shellquote.Split(cmd)
+ // 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
}
--- a/script/script_test.go Tue Feb 13 22:42:39 2018 -0600
+++ b/script/script_test.go Tue Feb 13 23:42:37 2018 -0600
@@ -23,6 +23,7 @@
junit "github.com/aphistic/sweet-junit"
. "github.com/onsi/gomega"
+ "bitbucket.org/rw_grim/convey/aws"
"bitbucket.org/rw_grim/convey/docker"
"bitbucket.org/rw_grim/convey/tasks"
)
@@ -58,7 +59,32 @@
Expect(len(parsedTasks)).To(Equal(1))
Expect(parsedTasks[0]).To(BeAssignableToTypeOf(&docker.Run{}))
+}
+func (s *scriptSuite) TestSubShellUnknown(t sweet.T) {
+ commands := []string{"$(true)"}
+
+ parsedTasks, err := Parse("", "", commands)
+
+ Expect(err).To(BeNil())
+ Expect(len(parsedTasks)).To(Equal(1))
+
+ Expect(parsedTasks[0]).To(BeAssignableToTypeOf(&docker.Run{}))
+ run := parsedTasks[0].(*docker.Run)
+ Expect(run.Script).To(BeEquivalentTo([]string{"$(true)"}))
+}
+
+func (s *scriptSuite) TestBackTicksUnknown(t sweet.T) {
+ commands := []string{"`true`"}
+
+ parsedTasks, err := Parse("", "", commands)
+
+ Expect(err).To(BeNil())
+ Expect(len(parsedTasks)).To(Equal(1))
+
+ Expect(parsedTasks[0]).To(BeAssignableToTypeOf(&docker.Run{}))
+ run := parsedTasks[0].(*docker.Run)
+ Expect(run.Script).To(BeEquivalentTo([]string{"`true`"}))
}
func (s *scriptSuite) TestDockerThenScript(t sweet.T) {
@@ -107,3 +133,22 @@
Expect(parsedTasks[1]).To(BeAssignableToTypeOf(&docker.Push{}))
Expect(parsedTasks[2]).To(BeAssignableToTypeOf(&docker.Run{}))
}
+
+func (s *scriptSuite) TestAWSECRGetLogin(t sweet.T) {
+ commands := []string{
+ "aws ecr get-login",
+ "aws ecr get-login --region us-west-2",
+ "$(aws ecr get-login)",
+ "`aws ecr get-login`",
+ }
+
+ parsedTasks, err := Parse("", "", commands)
+
+ Expect(err).To(BeNil())
+ Expect(len(parsedTasks)).To(Equal(4))
+
+ Expect(parsedTasks[0]).To(BeAssignableToTypeOf(&aws.ECRGetLogin{}))
+ Expect(parsedTasks[1]).To(BeAssignableToTypeOf(&aws.ECRGetLogin{}))
+ Expect(parsedTasks[2]).To(BeAssignableToTypeOf(&aws.ECRGetLogin{}))
+ Expect(parsedTasks[3]).To(BeAssignableToTypeOf(&aws.ECRGetLogin{}))
+}