Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ clean:

.PHONY: test
test:
$(GO) test -v -cover ./...
$(GO) test -cover ./...

.PHONY: coverage
coverage:
Expand Down
8 changes: 3 additions & 5 deletions cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,20 @@ func TestCacheKey(t *testing.T) {
Path: cPath("component.yaml"),
Docker: definitions.Docker{Image: "repo/img:1.2.3"},
Rules: map[string]definitions.Rule{
"test": definitions.Rule{
"test": {
Description: "test it!",
Inputs: []string{"${NAME}_test.go", "go.mod"},
Ignore: []string{"exclude_me.go"},
Outputs: []string{"test_results"},
Command: "go test -v",
},
"build": definitions.Rule{
"build": {
Description: "build it!",
Inputs: []string{"${NAME}.go", "go.mod"},
Ignore: []string{"exclude_me.go"},
Outputs: []string{"foo"},
Command: "go build",
Requires: []definitions.Dependency{
{Rule: "test"},
},
Requires: []definitions.Dependency{{Rule: "test"}},
},
},
Environment: map[string]string{"VOLUME": "11"},
Expand Down
6 changes: 5 additions & 1 deletion cmd/listInputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ func NewListInputsCommand() *cobra.Command {

var rows []interface{}
for _, c := range comps {
for _, r := range c.Rules() {
for _, ruleName := range c.RuleNames() {
r, err := c.Rule(ruleName, nil)
if err != nil {
fatal(err)
}
inputs, err := r.Inputs()
if err != nil {
fatal(err)
Expand Down
6 changes: 5 additions & 1 deletion cmd/listOutputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ func NewListArtifactsCommand() *cobra.Command {
var rows []interface{}
var rowColors []*color.Color
for _, c := range comps {
for _, r := range c.Rules() {
for _, ruleName := range c.RuleNames() {
r, err := c.Rule(ruleName, nil)
if err != nil {
fatal(err)
}
missingOutputs, err := r.MissingOutputs().RelativePaths(projDir)
if err != nil {
fatal(err)
Expand Down
7 changes: 2 additions & 5 deletions cmd/listRules.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ func NewListRulesCommand() *cobra.Command {
Short: "List rules in the project",
Aliases: []string{"r", "rule", "rules"},
Run: func(cmd *cobra.Command, args []string) {

opts := getZimOptions(cmd, args)
proj, err := getProject(opts.Directory)
if err != nil {
Expand All @@ -48,13 +47,12 @@ func NewListRulesCommand() *cobra.Command {
if err != nil {
fatal(err)
}

var rows []interface{}
for _, c := range comps {
for _, r := range c.Rules() {
for _, ruleName := range c.RuleNames() {
rows = append(rows, listRulesViewItem{
Component: c.Name(),
Rule: r.Name(),
Rule: ruleName,
})
}
}
Expand All @@ -66,7 +64,6 @@ func NewListRulesCommand() *cobra.Command {
if err != nil {
fatal(err)
}

for _, tableRow := range table {
fmt.Println(tableRow)
}
Expand Down
13 changes: 11 additions & 2 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,17 @@ func NewRunCommand() *cobra.Command {
// in order of rule dependencies
var schedulerErr error
scheduler := sched.NewGraphScheduler()
for _, rule := range opts.Rules {
rules := components.Rules([]string{rule})
for _, ruleName := range opts.Rules {
var rules []*project.Rule
for _, component := range components {
if component.HasRule(ruleName) {
rule, err := component.Rule(ruleName)
if err != nil {
fatal(err)
}
rules = append(rules, rule)
}
}
if len(rules) == 0 {
return
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/showKey.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ func NewShowKeyCommand() *cobra.Command {
if c == nil {
fatal(fmt.Errorf("Unknown component: %s", componentName))
}
r, found := c.Rule(ruleName)
if !found {
fatal(fmt.Errorf("Unknown rule: %s.%s", componentName, ruleName))
r, err := c.Rule(ruleName, nil)
if err != nil {
fatal(err)
}

ctx := context.Background()
Expand Down
2 changes: 1 addition & 1 deletion definitions/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type Component struct {
Toolchain Toolchain `yaml:"toolchain"`
Rules map[string]Rule `yaml:"rules"`
Exports map[string]Export `yaml:"exports"`
Environment map[string]string `yaml:"environment"`
Environment interface{} `yaml:"environment"`
Path string
}

Expand Down
21 changes: 21 additions & 0 deletions definitions/parameter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package definitions

// Parameter is used to configure a Rule
type Parameter struct {
Name string `yaml:"name"`
Description string `yaml:"description"`
Type string `yaml:"type"`
Mode string `yaml:"mode"`
Default interface{} `yaml:"default"`
}

func mergeParameters(a, b map[string]Parameter) map[string]Parameter {
result := map[string]Parameter{}
for k, v := range a {
result[k] = v
}
for k, v := range b {
result[k] = v
}
return result
}
68 changes: 67 additions & 1 deletion definitions/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,27 @@
package definitions

import (
"fmt"
"io/ioutil"

"github.com/go-yaml/yaml"
)

// Variable definition for creating a shell environment
type Variable struct {
Definition string
Script string
}

// Environment contains a list of variables used to define a shell environment
type Environment struct {
Variables []*Variable
}

// Project defines project configuration in YAML
type Project struct {
Name string `yaml:"name"`
Environment map[string]string `yaml:"environment"`
Environment interface{} `yaml:"environment"`
Components []string `yaml:"components"`
Providers map[string]map[string]interface{} `yaml:"providers"`
}
Expand All @@ -44,3 +56,57 @@ func LoadProjectFromPath(path string) (*Project, error) {
}
return LoadProject(data)
}

// GetEnvironment returns the Environment as defined at the Project level
func (p Project) GetEnvironment() (*Environment, error) {
return GetEnvironment(p.Environment)
}

// GetEnvironment returns one Environment definition from its semi-structured
// YAML form
func GetEnvironment(obj interface{}) (*Environment, error) {

var variables []*Variable

switch e := obj.(type) {

case []interface{}:
for _, item := range e {
if itemStr, ok := item.(string); ok {
variables = append(variables, &Variable{Definition: itemStr})
} else if itemMap, ok := item.(map[interface{}]interface{}); ok {
if len(itemMap) != 1 {
return nil, fmt.Errorf("invalid environment item: %v", itemMap)
}
key, value := getOneKeyValue(itemMap)
keyStr, ok := key.(string)
if !ok {
return nil, fmt.Errorf("environment key must be a string: %v", key)
}
valueMap, ok := value.(map[interface{}]interface{})
if !ok {
return nil, fmt.Errorf("environment value is invalid: %v", value)
}
script, ok := valueMap["run"].(string)
if !ok {
return nil, fmt.Errorf("environment variable does not define a run statement: %v", keyStr)
}
variables = append(variables, &Variable{
Definition: keyStr,
Script: script,
})
} else {
return nil, fmt.Errorf("invalid environment definition: %v", e)
}
}

case map[interface{}]interface{}:
for k, v := range e {
variables = append(variables, &Variable{Definition: fmt.Sprintf("%v=%v", k, v)})
}

default:
return nil, fmt.Errorf("invalid environment definition: %v", e)
}
return &Environment{Variables: variables}, nil
}
32 changes: 32 additions & 0 deletions definitions/project_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package definitions

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestEnvironment(t *testing.T) {
obj := []interface{}{
"foo",
"bar=fizzle biz",
map[interface{}]interface{}{
"ACCOUNT": map[interface{}]interface{}{
"run": "aws sts get-caller-identity",
},
},
}
env, err := GetEnvironment(obj)
require.Nil(t, err)
require.Len(t, env.Variables, 3)

var1 := env.Variables[0]
var2 := env.Variables[1]
var3 := env.Variables[2]

assert.Equal(t, "foo", var1.Definition)
assert.Equal(t, "bar=fizzle biz", var2.Definition)
assert.Equal(t, "ACCOUNT", var3.Definition)
assert.Equal(t, "aws sts get-caller-identity", var3.Script)
}
41 changes: 22 additions & 19 deletions definitions/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ import (

// Dependency between Rules
type Dependency struct {
Component string `yaml:"component"`
Rule string `yaml:"rule"`
Export string `yaml:"export"`
Recurse int `yaml:"recurse"`
Component string `yaml:"component"`
Rule string `yaml:"rule"`
Export string `yaml:"export"`
Parameters map[string]interface{} `yaml:"parameters"`
Recurse int `yaml:"recurse"`
}

// Providers specifies the name of the Provider type to be used for the
Expand Down Expand Up @@ -53,19 +54,20 @@ type Condition struct {

// Rule defines inputs, commands, and outputs for a build step or action
type Rule struct {
Name string `yaml:"name"`
Inputs []string `yaml:"inputs"`
Outputs []string `yaml:"outputs"`
Ignore []string `yaml:"ignore"`
Local bool `yaml:"local"`
Native bool `yaml:"native"`
Requires []Dependency `yaml:"requires"`
Description string `yaml:"description"`
Command string `yaml:"command"`
Commands []interface{} `yaml:"commands"`
Providers Providers `yaml:"providers"`
When Condition `yaml:"when"`
Unless Condition `yaml:"unless"`
Name string `yaml:"name"`
Inputs []string `yaml:"inputs"`
Outputs []string `yaml:"outputs"`
Ignore []string `yaml:"ignore"`
Local bool `yaml:"local"`
Native bool `yaml:"native"`
Requires []Dependency `yaml:"requires"`
Description string `yaml:"description"`
Command string `yaml:"command"`
Commands []interface{} `yaml:"commands"`
Providers Providers `yaml:"providers"`
When Condition `yaml:"when"`
Unless Condition `yaml:"unless"`
Parameters map[string]Parameter `yaml:"parameters"`
}

// GetCommands returns commands unmarshaled from the rule's semi-structured YAML
Expand Down Expand Up @@ -158,8 +160,9 @@ func mergeRule(a, b Rule) Rule {
Inputs: mergeStr(a.Providers.Inputs, b.Providers.Inputs),
Outputs: mergeStr(a.Providers.Outputs, b.Providers.Outputs),
},
When: mergeConditions(a.When, b.When),
Unless: mergeConditions(a.Unless, b.Unless),
When: mergeConditions(a.When, b.When),
Unless: mergeConditions(a.Unless, b.Unless),
Parameters: mergeParameters(a.Parameters, b.Parameters),
}

// Precedence for commands:
Expand Down
Loading