(req dto.CommonBackup, backupDir, fileName string)
| 306 | } |
| 307 | |
| 308 | func newComposeBackupContext(req dto.CommonBackup, backupDir, fileName string) (*composeBackupContext, error) { |
| 309 | if req.Name == "" { |
| 310 | return nil, fmt.Errorf("compose name is required") |
| 311 | } |
| 312 | dockerClient, err := dockerUtils.NewDockerClient() |
| 313 | if err != nil { |
| 314 | return nil, err |
| 315 | } |
| 316 | composePath, composeFiles, err := loadComposePathAndFiles(req.Name, dockerClient) |
| 317 | if err != nil { |
| 318 | _ = dockerClient.Close() |
| 319 | return nil, err |
| 320 | } |
| 321 | filePath := path.Join(backupDir, fileName) |
| 322 | tmpDir := path.Join(path.Dir(filePath), strings.TrimSuffix(path.Base(filePath), ".tar.gz")) |
| 323 | ctx := &composeBackupContext{ |
| 324 | req: req, |
| 325 | composeName: req.Name, |
| 326 | composePath: composePath, |
| 327 | composeFiles: composeFiles, |
| 328 | composeDir: path.Dir(composeFiles[0]), |
| 329 | fileOp: files.NewFileOp(), |
| 330 | dockerClient: dockerClient, |
| 331 | backupDir: backupDir, |
| 332 | fileName: fileName, |
| 333 | filePath: filePath, |
| 334 | tmpDir: tmpDir, |
| 335 | meta: composeBackupMeta{ |
| 336 | ComposeName: req.Name, |
| 337 | ComposePath: composePath, |
| 338 | CreatedAt: time.Now().Format(constant.DateTimeLayout), |
| 339 | Files: make([]composeBackupFile, 0), |
| 340 | Containers: make([]string, 0), |
| 341 | }, |
| 342 | } |
| 343 | return ctx, nil |
| 344 | } |
| 345 | |
| 346 | func loadComposePathAndFiles(composeName string, dockerClient *client.Client) (string, []string, error) { |
| 347 | composeRecord, _ := composeRepo.GetRecord(repo.WithByName(composeName)) |
no test coverage detected