Skip to content

Commit

Permalink
Add a command validate to check for parse errors #31 #55
Browse files Browse the repository at this point in the history
Running command `dunner validate` will check for any parse errors and warnings.
If there are any errors, it fails the cmd, if not it finishes by listing any warnings if present.
  • Loading branch information
apoorvam committed Mar 23, 2019
1 parent c513ba6 commit 5b0ceba
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 6 deletions.
12 changes: 10 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ GOCMD=go
GOINSTALL=$(GOCMD) install
GOTEST=$(GOCMD) test
DEP=dep
.PHONY : all install test
.PHONY : all install vet fmt test

all : def

Expand All @@ -18,4 +18,12 @@ test:
@$(GOTEST) -v ./...

clean:
rm -rf *
rm -rf *

vet:
@echo "=== go vet ==="
@go vet ./...

fmt:
@echo "=== go fmt ==="
@go fmt ./...
18 changes: 18 additions & 0 deletions cmd/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

import (
"github.com/leopardslab/Dunner/pkg/config"
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(validateCmd)
}

var validateCmd = &cobra.Command{
Use: "validate",
Short: "Validate the dunner task file `.dunner.yaml`",
Long: "You can validate task file `.dunner.yaml` with this command to see if there are any parse errors",
Run: config.Validate,
Args: cobra.MinimumNArgs(0),
}
25 changes: 25 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,31 @@ type Configs struct {
Tasks map[string][]Task
}

// Validates config and returns a list of errors and warnings. If errors are not critical/only warnings, it returns param `ok` as true, else false
func (configs *Configs) Validate() ([]error, bool) {
var errs []error
var warnings []error
if len(configs.Tasks) == 0 {
warnings = append(warnings, fmt.Errorf("dunner: No tasks defined"))
}

for taskName, tasks := range configs.Tasks {
for _, task := range tasks {
if task.Image == "" {
errs = append(errs, fmt.Errorf(`dunner: [%s] Image repository name cannot be empty`, taskName))
}
if len(task.Command) == 0 {
errs = append(errs, fmt.Errorf("dunner: [%s] Commands not defined for task with image %s", taskName, task.Image))
}
}
}
if len(errs) > 0 {
errs = append(errs, warnings...)
return errs, false
}
return warnings, true
}

// GetConfigs reads and parses tasks from the dunner file
func GetConfigs(filename string) (*Configs, error) {
fileContents, err := ioutil.ReadFile(filename)
Expand Down
47 changes: 47 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,50 @@ test:
}

}

func TestConfigs_Validate(t *testing.T) {
tasks := make(map[string][]Task, 0)
tasks["stats"] = []Task{getSampleTask()}
configs := &Configs{Tasks: tasks}

errs, ok := configs.Validate()

if !ok || len(errs) != 0 {
t.Fatalf("Configs Validation failed, expected to pass")
}
}

func TestConfigs_ValidateWithNoTasks(t *testing.T) {
tasks := make(map[string][]Task, 0)
configs := &Configs{Tasks: tasks}

errs, ok := configs.Validate()

if !ok || len(errs) != 1 {
t.Fatalf("Configs validation failed")
}
if errs[0].Error() != "dunner: No tasks defined" {
t.Fatalf("Configs Validation error message not as expected")
}
}

func TestConfigs_ValidateWithParseErrors(t *testing.T) {
tasks := make(map[string][]Task, 0)
task := Task{Image: "", Command: []string{}}
tasks["stats"] = []Task{task}
configs := &Configs{Tasks: tasks}

errs, ok := configs.Validate()

if ok || len(errs) != 2 {
t.Fatalf("Configs validation failed")
}

if errs[0].Error() != "dunner: [stats] Image repository name cannot be empty" || errs[1].Error() != "dunner: [stats] Commands not defined for task with image " {
t.Fatalf("Configs Validation error message not as expected")
}
}

func getSampleTask() Task {
return Task{Image: "image_name", Command: []string{"node", "--version"}}
}
25 changes: 25 additions & 0 deletions pkg/config/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package config

import (
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func Validate(_ *cobra.Command, args []string) {
var dunnerFile = viper.GetString("DunnerTaskFile")

configs, err := GetConfigs(dunnerFile)
if err != nil {
log.Fatal(err)
}

errs, ok := configs.Validate()
for _, err := range errs {
log.Error(err)
}
if !ok {
os.Exit(1)
}
}
11 changes: 7 additions & 4 deletions pkg/dunner/dunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ func Do(_ *cobra.Command, args []string) {
if err != nil {
log.Fatal(err)
}
errs, ok := configs.Validate()
for _, err := range errs {
log.Error(err)
}
if !ok {
os.Exit(1)
}

execTask(configs, args[0], args[1:])
}
Expand Down Expand Up @@ -91,10 +98,6 @@ func process(configs *config.Configs, s *docker.Step, wg *sync.WaitGroup, args [
log.Fatal(err)
}

if s.Image == "" {
log.Fatalf(`dunner: image repository name cannot be empty`)
}

pout, err := (*s).Exec()
if err != nil {
log.Fatal(err)
Expand Down

0 comments on commit 5b0ceba

Please sign in to comment.