( ctx context.Context, client DaemonClient, operationID string, path string, )
| 265 | } |
| 266 | |
| 267 | func downloadSupportBundle( |
| 268 | ctx context.Context, |
| 269 | client DaemonClient, |
| 270 | operationID string, |
| 271 | path string, |
| 272 | ) (err error) { |
| 273 | if strings.TrimSpace(path) == "" { |
| 274 | return errors.New("cli: support bundle output path is required") |
| 275 | } |
| 276 | if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { |
| 277 | return fmt.Errorf("cli: create support bundle output directory: %w", err) |
| 278 | } |
| 279 | if _, err := os.Stat(path); err == nil { |
| 280 | return fmt.Errorf("cli: support bundle output already exists: %s", path) |
| 281 | } else if !errors.Is(err, os.ErrNotExist) { |
| 282 | return fmt.Errorf("cli: inspect support bundle output: %w", err) |
| 283 | } |
| 284 | |
| 285 | tmpPath := path + ".tmp-" + strings.TrimSpace(operationID) |
| 286 | file, err := os.OpenFile(tmpPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o600) |
| 287 | if err != nil { |
| 288 | return fmt.Errorf("cli: create support bundle temp file: %w", err) |
| 289 | } |
| 290 | committed := false |
| 291 | defer func() { |
| 292 | if closeErr := file.Close(); closeErr != nil { |
| 293 | closeErr = fmt.Errorf("cli: close support bundle temp file: %w", closeErr) |
| 294 | if err == nil { |
| 295 | err = closeErr |
| 296 | } else { |
| 297 | err = errors.Join(err, closeErr) |
| 298 | } |
| 299 | } |
| 300 | if !committed { |
| 301 | if removeErr := os.Remove(tmpPath); removeErr != nil && !errors.Is(removeErr, os.ErrNotExist) { |
| 302 | removeErr = fmt.Errorf("cli: remove support bundle temp file: %w", removeErr) |
| 303 | if err == nil { |
| 304 | err = removeErr |
| 305 | } else { |
| 306 | err = errors.Join(err, removeErr) |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | }() |
| 311 | |
| 312 | if err := client.DownloadSupportBundle(ctx, operationID, file); err != nil { |
| 313 | return err |
| 314 | } |
| 315 | if err := file.Sync(); err != nil { |
| 316 | return fmt.Errorf("cli: sync support bundle temp file: %w", err) |
| 317 | } |
| 318 | if err := os.Rename(tmpPath, path); err != nil { |
| 319 | return fmt.Errorf("cli: move support bundle into place: %w", err) |
| 320 | } |
| 321 | committed = true |
| 322 | return nil |
| 323 | } |
| 324 |
no test coverage detected