15
15
package types
16
16
17
17
import (
18
+ "fmt"
18
19
"path/filepath"
19
20
"sort"
20
21
"strings"
35
36
}
36
37
)
37
38
39
+ var paths = map [string ]struct {}{}
40
+
38
41
func (cfg Config ) Validate (c path.ContextPath ) (r report.Report ) {
39
42
systemdPath := "/etc/systemd/system/"
40
43
unitPaths := map [string ]struct {}{}
@@ -76,53 +79,37 @@ func (cfg Config) validateParents(c path.ContextPath) report.Report {
76
79
Path string
77
80
Field string
78
81
}
79
- paths := map [string ]struct {}{}
80
82
r := report.Report {}
81
83
82
84
for i , f := range cfg .Storage .Files {
83
- if _ , exists := paths [f .Path ]; exists {
84
- r .AddOnError (c .Append ("storage" , "files" , i , "path" ), errors .ErrPathConflictsParentDir ) //TODO: should add different error?
85
- return r
86
- }
87
- paths [f .Path ] = struct {}{}
88
- entries = append (entries , struct {
89
- Path string
90
- Field string
91
- }{Path : f .Path , Field : "files" })
85
+ fmt .Println ("File variable value:" , f ) // Added print statement
86
+ r = handlePathConflict (f .Path , "files" , i , c , r , errors .ErrPathAlreadyExists )
87
+ addPathAndEntry (f .Path , "files" , & entries )
92
88
}
93
89
94
90
for i , d := range cfg .Storage .Directories {
95
- if _ , exists := paths [d .Path ]; exists {
96
- r .AddOnError (c .Append ("storage" , "directories" , i , "path" ), errors .ErrPathConflictsParentDir ) //TODO: should add different error?
97
- return r
98
- }
99
- paths [d .Path ] = struct {}{}
100
- entries = append (entries , struct {
101
- Path string
102
- Field string
103
- }{Path : d .Path , Field : "directories" })
91
+ fmt .Println ("Directory variable value:" , d ) // Added print statement
92
+ r = handlePathConflict (d .Path , "directories" , i , c , r , errors .ErrPathAlreadyExists )
93
+ addPathAndEntry (d .Path , "directories" , & entries )
104
94
}
105
95
106
96
for i , l := range cfg .Storage .Links {
107
- if _ , exists := paths [l .Path ]; exists {
108
- r .AddOnError (c .Append ("storage" , "links" , i , "path" ), errors .ErrPathConflictsParentDir ) //TODO: error to already exist path
109
- return r
110
- }
111
- paths [l .Path ] = struct {}{}
112
- entries = append (entries , struct {
113
- Path string
114
- Field string
115
- }{Path : l .Path , Field : "links" })
97
+ fmt .Println ("Link variable value:" , l ) // Added print statement
98
+ r = handlePathConflict (l .Path , "links" , i , c , r , errors .ErrPathAlreadyExists )
99
+ addPathAndEntry (l .Path , "links" , & entries )
116
100
}
117
101
118
102
sort .Slice (entries , func (i , j int ) bool {
119
103
return depth (entries [i ].Path ) < depth (entries [j ].Path )
120
104
})
121
105
122
106
for i , entry := range entries {
107
+ fmt .Println ("Entry variable value:" , entry ) // Added print statement
123
108
if i > 0 && isWithin (entry .Path , entries [i - 1 ].Path ) {
124
109
if entries [i - 1 ].Field != "directories" {
125
- r .AddOnError (c .Append ("storage" , entry .Field , i , "path" ), errors .ErrPathConflictsParentDir ) //TODO: conflict parent directories error
110
+ errorMsg := fmt .Errorf ("invalid entry at path %s: %v" , entry .Path , errors .ErrMissLabeledDir )
111
+ r .AddOnError (c .Append ("storage" , entry .Field , i , "path" ), errorMsg )
112
+ fmt .Println ("Error message:" , errorMsg ) // Added print statement
126
113
return r
127
114
}
128
115
}
@@ -131,16 +118,42 @@ func (cfg Config) validateParents(c path.ContextPath) report.Report {
131
118
return r
132
119
}
133
120
134
- // check the depth
121
+ func handlePathConflict (path , fieldName string , index int , c path.ContextPath , r report.Report , err error ) report.Report {
122
+ fmt .Println ("Path variable value:" , path ) // Added print statement
123
+ if _ , exists := paths [path ]; exists {
124
+ r .AddOnError (c .Append ("storage" , fieldName , index , "path" ), err )
125
+ fmt .Println ("Error:" , err ) // Added print statement
126
+ }
127
+ return r
128
+ }
129
+
130
+ func addPathAndEntry (path , fieldName string , entries * []struct { Path , Field string }) {
131
+ * entries = append (* entries , struct {
132
+ Path string
133
+ Field string
134
+ }{Path : path , Field : fieldName })
135
+ fmt .Println ("Added entry:" , path ) // Added print statement
136
+ }
137
+
135
138
func depth (path string ) uint {
136
139
var count uint
137
- for p := filepath .Clean (path ); p != "/" && p != "." ; count ++ {
138
- p = filepath .Dir (p )
140
+ cleanedPath := filepath .FromSlash (filepath .Clean (path ))
141
+ sep := string (filepath .Separator )
142
+
143
+ volume := filepath .VolumeName (cleanedPath )
144
+ if volume != "" {
145
+ cleanedPath = cleanedPath [len (volume ):]
146
+ }
147
+
148
+ for cleanedPath != sep && cleanedPath != "." {
149
+ cleanedPath = filepath .Dir (cleanedPath )
150
+ count ++
139
151
}
140
152
return count
141
153
}
142
154
143
- // isWithin checks if newPath is within prevPath.
144
155
func isWithin (newPath , prevPath string ) bool {
156
+ fmt .Println ("New path variable value:" , newPath ) // Added print statement
157
+ fmt .Println ("Previous path variable value:" , prevPath ) // Added print statement
145
158
return strings .HasPrefix (newPath , prevPath ) && newPath != prevPath
146
159
}
0 commit comments