-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from apoorvam/master
Add command `validate` to check for parse errors
- Loading branch information
Showing
8 changed files
with
483 additions
and
23 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/leopardslab/dunner/pkg/config" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
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: Validate, | ||
Args: cobra.MinimumNArgs(0), | ||
} | ||
|
||
// Validate command invoked from command line, validates the dunner task file. If there are errors, it fails with non-zero exit code. | ||
func Validate(_ *cobra.Command, args []string) { | ||
var dunnerFile = viper.GetString("DunnerTaskFile") | ||
|
||
configs, err := config.GetConfigs(dunnerFile) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
errs := configs.Validate() | ||
if len(errs) != 0 { | ||
fmt.Println("Validation failed with following errors:") | ||
for _, err := range errs { | ||
fmt.Println(err.Error()) | ||
} | ||
os.Exit(1) | ||
} | ||
fmt.Println("Validation successful!") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package util | ||
|
||
import ( | ||
"os" | ||
"path" | ||
"strings" | ||
) | ||
|
||
// HomeDir is the environment variable HOME | ||
var HomeDir = os.Getenv("HOME") | ||
|
||
// DirExists returns true if the given param is a valid existing directory | ||
func DirExists(dir string) bool { | ||
if strings.HasPrefix(dir, "~") { | ||
dir = path.Join(HomeDir, strings.Trim(dir, "~")) | ||
} | ||
src, err := os.Stat(dir) | ||
if err != nil { | ||
return false | ||
} | ||
return src.IsDir() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package util | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestDirExistsSuccess(t *testing.T) { | ||
tmpdir, err := ioutil.TempDir("", "TestDir") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer os.RemoveAll(tmpdir) | ||
|
||
exists := DirExists(tmpdir) | ||
|
||
if !exists { | ||
t.Fatalf("Directory exists; but got false") | ||
} | ||
} | ||
|
||
func TestDirExistsFail(t *testing.T) { | ||
exists := DirExists("this path is invalid") | ||
|
||
if exists { | ||
t.Fatalf("Directory invalid; but got as exists") | ||
} | ||
} | ||
|
||
func TestDirExistsFailForFile(t *testing.T) { | ||
tmpfile, err := ioutil.TempFile("", "TestFileExists") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer os.RemoveAll(tmpfile.Name()) | ||
|
||
exists := DirExists(tmpfile.Name()) | ||
|
||
if exists { | ||
t.Fatalf("Not a directory; but got as true") | ||
} | ||
} | ||
|
||
func TestDirExistsIfNotAbsPath(t *testing.T) { | ||
exists := DirExists("~/invalidpathfortesting") | ||
|
||
if exists { | ||
t.Fatalf("Not a directory; but got as true") | ||
} | ||
} |
Oops, something went wrong.