grim/convey

Add a new subtask task

2020-03-02, Gary Kramlich
469b76e1bd24
Parents 22eba3fdc82c
Children cba4329ae80a
Add a new subtask task
--- a/convey.yml Mon Mar 02 17:09:03 2020 -0600
+++ b/convey.yml Mon Mar 02 20:42:15 2020 -0600
@@ -25,39 +25,38 @@
image: ${GO_IMAGE}
workdir: ${CONVEY_WORKSPACE}
command: go test ./...
- build-linux:
+ build:
type: docker/run
image: ${GO_IMAGE}
environment:
+ - GOARCH=amd64
+ workdir: ${CONVEY_WORKSPACE}
+ command: go build -o convey-${CONVEY_VERSION}-${GOOS}-${GOARCH}${SUFFIX}
+ build-linux:
+ type: convey/subtask
+ base: build
+ environment:
- CGO_ENABLED=0
- GOOS=linux
- - GOARCH=amd64
- workdir: ${CONVEY_WORKSPACE}
- command: go build -o convey-${CONVEY_VERSION}-${GOOS}-${GOARCH}
+ - SUFFIX=
build-windows:
- type: docker/run
- image: golang:1.12-buster
+ type: convey/subtask
+ base: build
environment:
- GOOS=windows
- - GOARCH=amd64
- workdir: ${CONVEY_WORKSPACE}
- command: go build -o convey-${CONVEY_VERSION}-${GOOS}-${GOARCH}.exe
+ - SUFFIX=.exe
build-darwin:
- type: docker/run
- image: ${GO_IMAGE}
+ type: convey/subtask
+ base: build
environment:
- GOOS=darwin
- - GOARCH=amd64
- workdir: ${CONVEY_WORKSPACE}
- command: go build -o convey-${CONVEY_VERSION}-${GOOS}-${GOARCH}
+ - SUFFIX=
build-freebsd:
- type: docker/run
- image: ${GO_IMAGE}
+ type: convey/subtask
+ base: build
environment:
- GOOS=freebsd
- - GOARCH=amd64
- workdir: ${CONVEY_WORKSPACE}
- command: go build -o convey-${CONVEY_VERSION}-${GOOS}-${GOARCH}
+ - SUFFIX=
plans:
default:
--- a/docker/run.go Mon Mar 02 17:09:03 2020 -0600
+++ b/docker/run.go Mon Mar 02 20:42:15 2020 -0600
@@ -68,6 +68,10 @@
return nil
}
+func (r *Run) ReadEnvironment() []string {
+ return []string(r.Environment)
+}
+
// writeScript will write a shell script to the given tempfile for the given commands
func (r *Run) writeScript(name string, rt *runtime.Runtime, fullEnv *environment.Environment) (string, string, string, error) {
// create our task directory if it doesn't exist already
--- a/environment/environment.go Mon Mar 02 17:09:03 2020 -0600
+++ b/environment/environment.go Mon Mar 02 20:42:15 2020 -0600
@@ -24,6 +24,10 @@
"sync"
)
+type EnvironmentReader interface {
+ ReadEnvironment() []string
+}
+
type Environment struct {
mutex sync.Mutex
--- a/loaders/convey/tasks.go Mon Mar 02 17:09:03 2020 -0600
+++ b/loaders/convey/tasks.go Mon Mar 02 20:42:15 2020 -0600
@@ -65,7 +65,7 @@
}
task = newTask
} else {
- return nil, fmt.Errorf("task '%s' not found", rawType)
+ return nil, fmt.Errorf("task type %q not found", rawType)
}
}
@@ -94,5 +94,16 @@
realTasks[name] = realTask
}
+ // now resolve any subtasks
+ for name, task := range realTasks {
+ if subtask, ok := task.(*tasks.SubTask); ok {
+ if parent, found := realTasks[subtask.Base]; found {
+ subtask.Parent = parent
+ } else {
+ return nil, fmt.Errorf("failed to find parent task %q for task %q", subtask.Base, name)
+ }
+ }
+ }
+
return realTasks, nil
}
--- a/tasks/README.md Mon Mar 02 17:09:03 2020 -0600
+++ b/tasks/README.md Mon Mar 02 20:42:15 2020 -0600
@@ -55,3 +55,37 @@
clean:
type: convey/noop
+----
+
+### convey/subtask Task
+
+A subtask lets reuse existing tasks by setting the environment.
+
+As of right now, a subtask can **NOT** override an environment variable in it's
+parent task, but you can mimic that but having all subtasks setting the
+environment variable.
+
+### Attributes
+
+| Name | Required | Default | Description |
+| -------- | -------- | ------- | ----------- |
+| base | Yes | | The name of the parent task |
+
+### Example
+
+ build:
+ type: docker/run
+ command: go build -o foo-${GOOS}-${GOARCH}
+ environment:
+ - GOARCH=amd64
+ linux:
+ type: convey/subtask
+ base: build
+ environment:
+ - GOOS=linux
+ windows:
+ type: convey/subtask
+ base: build
+ environment:
+ - GOOS=windows
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tasks/subtask.go Mon Mar 02 20:42:15 2020 -0600
@@ -0,0 +1,58 @@
+// Convey
+// Copyright 2016-2020 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 tasks
+
+import (
+ "fmt"
+
+ log "github.com/sirupsen/logrus"
+
+ "hg.sr.ht/~grim/convey/environment"
+ "hg.sr.ht/~grim/convey/runtime"
+ "hg.sr.ht/~grim/convey/yaml"
+)
+
+type SubTask struct {
+ Base string `yaml:"base"`
+ Parent Task
+ Environment yaml.StringOrSlice `yaml:"environment"`
+}
+
+func (t *SubTask) New() Task {
+ return &SubTask{}
+}
+
+func (t *SubTask) Valid() error {
+ if t.Base == "" {
+ return fmt.Errorf("no base task specified")
+ }
+
+ return nil
+}
+
+func (t *SubTask) Execute(name string, logger *log.Entry, env *environment.Environment, rt *runtime.Runtime) error {
+ fullEnv := env.Copy()
+
+ _ = t.Parent.(environment.EnvironmentReader)
+ if e, ok := t.Parent.(environment.EnvironmentReader); ok {
+ fullEnv.MergeSlice(e.ReadEnvironment())
+ }
+
+ fullEnv.MergeSlice(t.Environment).Merge(rt.Environment)
+
+ return t.Parent.Execute(name, logger, fullEnv, rt)
+}
--- a/tasks/tasks.go Mon Mar 02 17:09:03 2020 -0600
+++ b/tasks/tasks.go Mon Mar 02 20:42:15 2020 -0600
@@ -27,11 +27,12 @@
var (
// Tasks is the list of tasks defined in this package.
Tasks = map[string]Task{
- "clean": &Clean{},
- "export": &Export{},
- "fail": &Fail{},
- "import": &Import{},
- "noop": &Noop{},
+ "clean": &Clean{},
+ "export": &Export{},
+ "fail": &Fail{},
+ "import": &Import{},
+ "noop": &Noop{},
+ "subtask": &SubTask{},
}
)