()
| 78 | } |
| 79 | |
| 80 | func (p powershell) installCompletion() error { |
| 81 | if err := dirs.CleanTmpFilesDir(); err != nil { |
| 82 | return fmt.Errorf("failed to clean tmp files dir -> %w", err) |
| 83 | } |
| 84 | |
| 85 | // Use temporary file mode to ensure file operation safety. |
| 86 | tmpDir := dirs.TmpFilesDir |
| 87 | tmpFile := filepath.Join(tmpDir, "celer_completion.ps1") |
| 88 | |
| 89 | // Create and write temporary completion file. |
| 90 | if err := func() error { |
| 91 | file, err := os.Create(tmpFile) |
| 92 | if err != nil { |
| 93 | return fmt.Errorf("failed to create powershell completion file -> %w", err) |
| 94 | } |
| 95 | defer file.Close() |
| 96 | |
| 97 | return p.rootCmd.GenPowerShellCompletion(file) |
| 98 | }(); err != nil { |
| 99 | return fmt.Errorf("failed to generate powershell completion file -> %w", err) |
| 100 | } |
| 101 | |
| 102 | // Wait for the file to be completely released (Windows system may need this). |
| 103 | // Add a small delay or use file lock check here. |
| 104 | if err := p.ensureFileReleased(tmpFile); err != nil { |
| 105 | return err |
| 106 | } |
| 107 | |
| 108 | // Install completion file to `~/Documents/WindowsPowerShell/Modules` |
| 109 | modulesDir := filepath.Join(os.Getenv("USERPROFILE"), "Documents", "WindowsPowerShell", "Modules") |
| 110 | celerRcFile := filepath.Join(modulesDir, "celer", "celer_completion.ps1") |
| 111 | if err := os.MkdirAll(filepath.Dir(celerRcFile), os.ModePerm); err != nil { |
| 112 | return fmt.Errorf("failed to create PowerShell Modules dir -> %w", err) |
| 113 | } |
| 114 | |
| 115 | if err := fileio.MoveFile(tmpFile, celerRcFile); err != nil { |
| 116 | return fmt.Errorf("failed to move PowerShell completion file -> %w", err) |
| 117 | } |
| 118 | |
| 119 | fmt.Printf("[integrate] completion -> %s\n", filepath.Dir(celerRcFile)) |
| 120 | |
| 121 | return nil |
| 122 | } |
| 123 | |
| 124 | // Ensure the file is released helper method. |
| 125 | func (p powershell) ensureFileReleased(filePath string) error { |
no test coverage detected