File tree Expand file tree Collapse file tree 3 files changed +57
-8
lines changed Expand file tree Collapse file tree 3 files changed +57
-8
lines changed Original file line number Diff line number Diff line change 66 "github.com/cmuench/inotify-proxy/internal/util"
77 "github.com/cmuench/inotify-proxy/internal/watcher"
88 "github.com/gookit/color"
9+ "os"
910 "strings"
1011)
1112
@@ -41,7 +42,12 @@ func main() {
4142func loadConfig (c config.Config , includedDirectories []string , profilePtr * string ) []string {
4243 if util .FileExists ("inotify-proxy.yaml" ) {
4344 color .Info .Println ("load config" )
44- c = config .ReadFile ("inotify-proxy.yaml" )
45+ c , err := config .ReadFile ("inotify-proxy.yaml" );
46+
47+ if err != nil {
48+ color .Errorf ("error: Invalid config provided.\n " )
49+ os .Exit (1 )
50+ }
4551
4652 for _ , watch := range c .Watch {
4753 includedDirectories = append (includedDirectories , watch .Dir )
Original file line number Diff line number Diff line change @@ -14,20 +14,30 @@ type Config struct {
1414 Profile string `yaml:"profile"`
1515}
1616
17- func ReadFile (filename string ) Config {
18- yamlData , err := ioutil .ReadFile (filename )
17+ func ReadFile (filename string ) (Config , error ) {
18+ var (
19+ c Config
20+ err error
21+ yamlData []byte
22+ )
23+ yamlData , err = ioutil .ReadFile (filename )
1924
2025 if err != nil {
21- panic ( err )
26+ return c , err
2227 }
2328
29+ c , err = Parse (yamlData )
30+
31+ return c , err
32+ }
33+
34+ func Parse (yamlData []byte ) (Config , error ) {
2435 var c Config
25- err = yaml .Unmarshal (yamlData , & c )
36+ err : = yaml .Unmarshal (yamlData , & c )
2637
2738 if err != nil {
28- panic ( err )
39+ return c , err
2940 }
3041
31- return c
42+ return c , nil
3243}
33-
Original file line number Diff line number Diff line change 1+ package config
2+
3+ import (
4+ "github.com/stretchr/testify/assert"
5+ "testing"
6+ )
7+
8+ func TestParseValidYaml (t * testing.T ) {
9+
10+ validYamlData := `
11+ ---
12+ watch:
13+ - dir: /tmp/watch1
14+ - dir: /tmp/watch2
15+ profile: magento2
16+
17+ `
18+ c , err := Parse ([]byte (validYamlData ))
19+
20+ assert .NoError (t , err , "Config is valid and should not throw an error" )
21+ assert .IsType (t , Config {}, c )
22+ }
23+
24+ func TestParseInvalidYaml (t * testing.T ) {
25+ invalidYamlData := `
26+ ---
27+ watch
28+
29+ `
30+ _ , err := Parse ([]byte (invalidYamlData ))
31+
32+ assert .Error (t , err , "Config is invalid and should throw an error" )
33+ }
You can’t perform that action at this time.
0 commit comments