1515package types
1616
1717import (
18+ "fmt"
1819 "path/filepath"
1920 "sort"
2021 "strings"
3536 }
3637)
3738
39+ var paths = map [string ]struct {}{}
40+
3841func (cfg Config ) Validate (c path.ContextPath ) (r report.Report ) {
3942 systemdPath := "/etc/systemd/system/"
4043 unitPaths := map [string ]struct {}{}
@@ -76,53 +79,37 @@ func (cfg Config) validateParents(c path.ContextPath) report.Report {
7679 Path string
7780 Field string
7881 }
79- paths := map [string ]struct {}{}
8082 r := report.Report {}
8183
8284 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 )
9288 }
9389
9490 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 )
10494 }
10595
10696 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 )
116100 }
117101
118102 sort .Slice (entries , func (i , j int ) bool {
119103 return depth (entries [i ].Path ) < depth (entries [j ].Path )
120104 })
121105
122106 for i , entry := range entries {
107+ fmt .Println ("Entry variable value:" , entry ) // Added print statement
123108 if i > 0 && isWithin (entry .Path , entries [i - 1 ].Path ) {
124109 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
126113 return r
127114 }
128115 }
@@ -131,16 +118,42 @@ func (cfg Config) validateParents(c path.ContextPath) report.Report {
131118 return r
132119}
133120
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+
135138func depth (path string ) uint {
136139 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 ++
139151 }
140152 return count
141153}
142154
143- // isWithin checks if newPath is within prevPath.
144155func 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
145158 return strings .HasPrefix (newPath , prevPath ) && newPath != prevPath
146159}
0 commit comments