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 6
6
"github.com/cmuench/inotify-proxy/internal/util"
7
7
"github.com/cmuench/inotify-proxy/internal/watcher"
8
8
"github.com/gookit/color"
9
+ "os"
9
10
"strings"
10
11
)
11
12
@@ -41,7 +42,12 @@ func main() {
41
42
func loadConfig (c config.Config , includedDirectories []string , profilePtr * string ) []string {
42
43
if util .FileExists ("inotify-proxy.yaml" ) {
43
44
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
+ }
45
51
46
52
for _ , watch := range c .Watch {
47
53
includedDirectories = append (includedDirectories , watch .Dir )
Original file line number Diff line number Diff line change @@ -14,20 +14,30 @@ type Config struct {
14
14
Profile string `yaml:"profile"`
15
15
}
16
16
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 )
19
24
20
25
if err != nil {
21
- panic ( err )
26
+ return c , err
22
27
}
23
28
29
+ c , err = Parse (yamlData )
30
+
31
+ return c , err
32
+ }
33
+
34
+ func Parse (yamlData []byte ) (Config , error ) {
24
35
var c Config
25
- err = yaml .Unmarshal (yamlData , & c )
36
+ err : = yaml .Unmarshal (yamlData , & c )
26
37
27
38
if err != nil {
28
- panic ( err )
39
+ return c , err
29
40
}
30
41
31
- return c
42
+ return c , nil
32
43
}
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