Skip to content

Commit 89b68cc

Browse files
committed
Fix include directive parsing to handle relative paths
1 parent 8d7f74c commit 89b68cc

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

internal/datasource/config/nginx_config_parser.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func (ncp *NginxConfigParser) createNginxConfigContext(
142142
func(ctx context.Context, parent, directive *crossplane.Directive) error {
143143
switch directive.Directive {
144144
case "include":
145-
include := ncp.parseIncludeDirective(directive)
145+
include := ncp.parseIncludeDirective(directive, &conf)
146146

147147
nginxConfigContext.Includes = append(nginxConfigContext.Includes, include)
148148
case "log_format":
@@ -267,12 +267,15 @@ func (ncp *NginxConfigParser) findLocalSysLogServers(sysLogServer string) string
267267
return ""
268268
}
269269

270-
func (ncp *NginxConfigParser) parseIncludeDirective(directive *crossplane.Directive) string {
270+
func (ncp *NginxConfigParser) parseIncludeDirective(
271+
directive *crossplane.Directive,
272+
configFile *crossplane.Config,
273+
) string {
271274
var include string
272275
if filepath.IsAbs(directive.Args[0]) {
273276
include = directive.Args[0]
274277
} else {
275-
include = filepath.Join(filepath.Dir(directive.File), directive.Args[0])
278+
include = filepath.Join(filepath.Dir(configFile.File), directive.Args[0])
276279
}
277280

278281
return include

internal/watcher/file/file_watcher_service.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,12 @@ func (fws *FileWatcherService) addWatchers(ctx context.Context) {
138138
}
139139

140140
if !slices.Contains(fws.watcher.WatchList(), directory) {
141-
fws.addWatcher(ctx, directory)
142-
fws.filesChanged.Store(true)
141+
err := fws.addWatcher(ctx, directory)
142+
if err != nil {
143+
slog.DebugContext(ctx, "Failed to add file watcher", "directory", directory, "error", err)
144+
} else {
145+
fws.filesChanged.Store(true)
146+
}
143147
}
144148
}
145149
}
@@ -208,7 +212,7 @@ func (fws *FileWatcherService) checkForUpdates(ctx context.Context, ch chan<- Fi
208212
}
209213
}
210214

211-
func (fws *FileWatcherService) addWatcher(ctx context.Context, directory string) {
215+
func (fws *FileWatcherService) addWatcher(ctx context.Context, directory string) error {
212216
slog.DebugContext(ctx, "Checking if file watcher needs to be added", "directory", directory)
213217

214218
if _, err := os.Stat(directory); errors.Is(err, os.ErrNotExist) {
@@ -220,9 +224,7 @@ func (fws *FileWatcherService) addWatcher(ctx context.Context, directory string)
220224

221225
slog.DebugContext(ctx, "Adding watcher", "directory", directory)
222226

223-
if err := fws.watcher.Add(directory); err != nil {
224-
slog.WarnContext(ctx, "Failed to add file watcher", "directory", directory, "error", err)
225-
}
227+
return fws.watcher.Add(directory)
226228
}
227229

228230
func (fws *FileWatcherService) removeWatcher(ctx context.Context, path string) {

internal/watcher/file/file_watcher_service_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func TestFileWatcherService_addWatcher(t *testing.T) {
6262
require.NoError(t, err)
6363
defer os.Remove(testDirectory)
6464

65-
fileWatcherService.addWatcher(ctx, testDirectory)
65+
require.NoError(t, fileWatcherService.addWatcher(ctx, testDirectory))
6666

6767
directoriesBeingWatched := fileWatcherService.watcher.WatchList()
6868
assert.Len(t, directoriesBeingWatched, 1)
@@ -79,7 +79,7 @@ func TestFileWatcherService_addWatcher_Error(t *testing.T) {
7979
tempDir := t.TempDir()
8080
testDirectory := path.Join(tempDir, "test_dir")
8181

82-
fileWatcherService.addWatcher(ctx, testDirectory)
82+
require.Error(t, fileWatcherService.addWatcher(ctx, testDirectory))
8383

8484
directoriesBeingWatched := fileWatcherService.watcher.WatchList()
8585
assert.Empty(t, directoriesBeingWatched)

0 commit comments

Comments
 (0)