Skip to content

Commit 8becd1b

Browse files
authored
Merge pull request #62 from apoorvam/master
Add command `validate` to check for parse errors
2 parents 239ec60 + 9506a77 commit 8becd1b

File tree

8 files changed

+483
-23
lines changed

8 files changed

+483
-23
lines changed

Gopkg.lock

Lines changed: 50 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,11 @@
4141
[prune]
4242
go-tests = true
4343
unused-packages = true
44+
45+
[[constraint]]
46+
name = "gopkg.in/go-playground/validator.v9"
47+
version = "9.28.0"
48+
49+
[[constraint]]
50+
name = "github.com/go-playground/universal-translator"
51+
version = "0.16.0"

cmd/validate.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/leopardslab/dunner/pkg/config"
8+
"github.com/spf13/cobra"
9+
"github.com/spf13/viper"
10+
)
11+
12+
func init() {
13+
rootCmd.AddCommand(validateCmd)
14+
}
15+
16+
var validateCmd = &cobra.Command{
17+
Use: "validate",
18+
Short: "Validate the dunner task file `.dunner.yaml`",
19+
Long: "You can validate task file `.dunner.yaml` with this command to see if there are any parse errors",
20+
Run: Validate,
21+
Args: cobra.MinimumNArgs(0),
22+
}
23+
24+
// Validate command invoked from command line, validates the dunner task file. If there are errors, it fails with non-zero exit code.
25+
func Validate(_ *cobra.Command, args []string) {
26+
var dunnerFile = viper.GetString("DunnerTaskFile")
27+
28+
configs, err := config.GetConfigs(dunnerFile)
29+
if err != nil {
30+
log.Fatal(err)
31+
}
32+
33+
errs := configs.Validate()
34+
if len(errs) != 0 {
35+
fmt.Println("Validation failed with following errors:")
36+
for _, err := range errs {
37+
fmt.Println(err.Error())
38+
}
39+
os.Exit(1)
40+
}
41+
fmt.Println("Validation successful!")
42+
}

internal/util/util.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package util
2+
3+
import (
4+
"os"
5+
"path"
6+
"strings"
7+
)
8+
9+
// HomeDir is the environment variable HOME
10+
var HomeDir = os.Getenv("HOME")
11+
12+
// DirExists returns true if the given param is a valid existing directory
13+
func DirExists(dir string) bool {
14+
if strings.HasPrefix(dir, "~") {
15+
dir = path.Join(HomeDir, strings.Trim(dir, "~"))
16+
}
17+
src, err := os.Stat(dir)
18+
if err != nil {
19+
return false
20+
}
21+
return src.IsDir()
22+
}

internal/util/util_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package util
2+
3+
import (
4+
"io/ioutil"
5+
"os"
6+
"testing"
7+
)
8+
9+
func TestDirExistsSuccess(t *testing.T) {
10+
tmpdir, err := ioutil.TempDir("", "TestDir")
11+
if err != nil {
12+
t.Fatal(err)
13+
}
14+
defer os.RemoveAll(tmpdir)
15+
16+
exists := DirExists(tmpdir)
17+
18+
if !exists {
19+
t.Fatalf("Directory exists; but got false")
20+
}
21+
}
22+
23+
func TestDirExistsFail(t *testing.T) {
24+
exists := DirExists("this path is invalid")
25+
26+
if exists {
27+
t.Fatalf("Directory invalid; but got as exists")
28+
}
29+
}
30+
31+
func TestDirExistsFailForFile(t *testing.T) {
32+
tmpfile, err := ioutil.TempFile("", "TestFileExists")
33+
if err != nil {
34+
t.Fatal(err)
35+
}
36+
defer os.RemoveAll(tmpfile.Name())
37+
38+
exists := DirExists(tmpfile.Name())
39+
40+
if exists {
41+
t.Fatalf("Not a directory; but got as true")
42+
}
43+
}
44+
45+
func TestDirExistsIfNotAbsPath(t *testing.T) {
46+
exists := DirExists("~/invalidpathfortesting")
47+
48+
if exists {
49+
t.Fatalf("Not a directory; but got as true")
50+
}
51+
}

0 commit comments

Comments
 (0)