Skip to content

Commit

Permalink
Fixing inner subfolders looking up sync-all
Browse files Browse the repository at this point in the history
  • Loading branch information
erdemkosk committed Jun 15, 2024
1 parent 458a9ac commit 496e6d2
Showing 1 changed file with 30 additions and 26 deletions.
56 changes: 30 additions & 26 deletions internal/command/syncAll.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,48 +17,52 @@ func (command *SyncAllCommand) Execute(cmd *cobra.Command, args []string) {
currentPath, _ := logic.GetCurrentPathAndFolder(command.path)
envolvePath := logic.GetEnvolveHomePath()

err := filepath.Walk(currentPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Printf("Error accessing path %q: %v\n", path, err)
return err
}
entries, err := os.ReadDir(currentPath)
if err != nil {
log.Printf("Error reading directory %q: %v\n", currentPath, err)
os.Exit(1)
}

for _, entry := range entries {
if entry.IsDir() {
subDirPath := filepath.Join(currentPath, entry.Name())
envFilePath := filepath.Join(subDirPath, ".env")

// Check if .env file exists in the subdirectory
if _, err := os.Stat(envFilePath); os.IsNotExist(err) {
log.Printf(".env file does not exist in directory: %s, skipping...\n", subDirPath)
continue
}

if info.IsDir() && path != currentPath { // Check if it's a directory (excluding current directory)
log.Printf("Syncing .env file in directory: %s\n", path)
log.Printf("Syncing .env file in directory: %s\n", subDirPath)

currentPath, currentFolderName := logic.GetCurrentPathAndFolder(path)
_, currentFolderName := logic.GetCurrentPathAndFolder(subDirPath)
targetPath := filepath.Join(envolvePath, currentFolderName)
currentEnvFilePath := filepath.Join(currentPath, "/.env")
targetEnvFilePath := filepath.Join(targetPath, "/.env")
targetEnvFilePath := filepath.Join(targetPath, ".env")

if _, err := os.Stat(targetEnvFilePath); err == nil {
log.Println("Error: .env file already exists in the current directory!")
return nil
log.Println("Error: .env file already exists in the target directory!")
continue
}

err := logic.CreateFolderIfDoesNotExist(targetPath)

if err != nil {
return nil
log.Printf("Error creating target directory: %v\n", err)
continue
}

copyErr := logic.CopyFile(currentEnvFilePath, targetEnvFilePath)

copyErr := logic.CopyFile(envFilePath, targetEnvFilePath)
if copyErr != nil {
return nil
log.Printf("Error copying .env file: %v\n", copyErr)
continue
}

logic.DeleteFile(currentEnvFilePath)
logic.Symlink(targetEnvFilePath, currentEnvFilePath)

log.Printf("Sync completed successfully for directory: %s\n", path)
}
logic.DeleteFile(envFilePath)

return nil
})
logic.Symlink(targetEnvFilePath, envFilePath)

if err != nil {
log.Printf("Error walking through directory: %v\n", err)
log.Printf("Sync completed successfully for directory: %s\n", subDirPath)
}
}

os.Exit(0)
Expand Down

0 comments on commit 496e6d2

Please sign in to comment.