Skip to content

Commit 5fe73cd

Browse files
committed
chore: clean up
1 parent 973777a commit 5fe73cd

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

internal/file/file_manager_service.go

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,6 @@ func (fms *FileManagerService) ConfigApply(ctx context.Context,
308308
if fileErr != nil {
309309
return model.RollbackRequired, fileErr
310310
}
311-
slog.Error("config apply called")
312311
// Update map of current files on disk
313312
fms.UpdateCurrentFilesOnDisk(files.ConvertToMapOfFiles(fileOverview.GetFiles()))
314313
fms.UpdateManifestFile(files.ConvertToMapOfFiles(fileOverview.GetFiles()))
@@ -335,11 +334,11 @@ func (fms *FileManagerService) Rollback(ctx context.Context, instanceID string)
335334
// currentFilesOnDisk needs to be updated after rollback action is performed
336335
delete(fms.currentFilesOnDisk, file.GetFileMeta().GetName())
337336
fms.UpdateManifestFile(fms.currentFilesOnDisk)
337+
338338
continue
339339

340340
case mpi.File_FILE_ACTION_DELETE, mpi.File_FILE_ACTION_UPDATE:
341341
content := fms.rollbackFileContents[file.GetFileMeta().GetName()]
342-
343342
err := fms.fileOperator.Write(ctx, content, file.GetFileMeta())
344343
if err != nil {
345344
return err
@@ -460,22 +459,9 @@ func (fms *FileManagerService) DetermineFileActions(currentFiles, modifiedFiles
460459
fileDiff := make(map[string]*mpi.File) // Files that have changed, key is file name
461460
fileContents := make(map[string][]byte) // contents of the file, key is file name
462461

463-
file, err := os.ReadFile(manifestFilePath)
464-
if err != nil {
465-
fmt.Printf("Failed to read manifest file: %v\n", err)
466-
}
467-
468-
// Parse JSON into a map
469-
var manifestFiles map[string]*mpi.File
470-
err = json.Unmarshal(file, &manifestFiles)
471-
if err != nil {
472-
fmt.Printf("Failed to parse manifest JSON: %v\n", err)
473-
manifestFiles = currentFiles // This is the first time when config apply is happening.
474-
}
475-
476462
// if file is in manifestFiles but not in modified files, file has been deleted
477463
// copy contents, set file action
478-
for fileName, currentFile := range manifestFiles {
464+
for fileName, currentFile := range fms.getManifestFile(currentFiles) {
479465
_, exists := modifiedFiles[fileName]
480466

481467
if !exists {
@@ -532,7 +518,6 @@ func (fms *FileManagerService) UpdateCurrentFilesOnDisk(currentFiles map[string]
532518
for _, file := range currentFiles {
533519
fms.currentFilesOnDisk[file.GetFileMeta().GetName()] = file
534520
}
535-
536521
}
537522

538523
func (fms *FileManagerService) UpdateManifestFile(currentFiles map[string]*mpi.File) {
@@ -541,13 +526,13 @@ func (fms *FileManagerService) UpdateManifestFile(currentFiles map[string]*mpi.F
541526
fmt.Printf("Failed to read manifest file: %v\n", err)
542527
}
543528

544-
slog.Error(string(jsonData))
545-
err = os.MkdirAll(manifestDirPath, 0755) // 0755 allows read/execute for all, write for owner
546-
if err != nil {
529+
// 0755 allows read/execute for all, write for owner
530+
if err := os.MkdirAll(manifestDirPath, 0755); err != nil {
547531
fmt.Printf("Failed to read manifest file: %v\n", err)
548532
}
549533

550-
newFile, err := os.OpenFile(manifestFilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) // 0600 ensures only root can read/write
534+
// 0600 ensures only root can read/write
535+
newFile, err := os.OpenFile(manifestFilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
551536
if err != nil {
552537
fmt.Printf("Failed to read manifest file: %v\n", err)
553538
}
@@ -560,3 +545,23 @@ func (fms *FileManagerService) UpdateManifestFile(currentFiles map[string]*mpi.F
560545

561546
slog.Error("Manifest File updated successfully")
562547
}
548+
549+
func (fms *FileManagerService) getManifestFile(currentFiles map[string]*mpi.File) map[string]*mpi.File {
550+
if _, err := os.Stat(manifestFilePath); err != nil {
551+
return currentFiles // Return current files if manifest file doesn't exist
552+
}
553+
554+
file, err := os.ReadFile(manifestFilePath)
555+
if err != nil {
556+
fmt.Printf("Failed to read manifest file: %v\n", err)
557+
return currentFiles
558+
}
559+
560+
var manifestFiles map[string]*mpi.File
561+
if err = json.Unmarshal(file, &manifestFiles); err != nil {
562+
fmt.Printf("Failed to parse manifest JSON: %v\n", err)
563+
return currentFiles
564+
}
565+
566+
return manifestFiles
567+
}

0 commit comments

Comments
 (0)