| 243 | } |
| 244 | |
| 245 | func loadSqlFile(file string) (string, error) { |
| 246 | if !strings.HasSuffix(file, ".tar.gz") && !strings.HasSuffix(file, ".zip") { |
| 247 | return file, nil |
| 248 | } |
| 249 | fileName := path.Base(file) |
| 250 | fileDir := path.Dir(file) |
| 251 | fileNameItem := time.Now().Format(constant.DateTimeSlimLayout) |
| 252 | dstDir := fmt.Sprintf("%s/%s", fileDir, fileNameItem) |
| 253 | _ = os.Mkdir(dstDir, constant.DirPerm) |
| 254 | if strings.HasSuffix(fileName, ".tar.gz") { |
| 255 | fileOp := files.NewFileOp() |
| 256 | if err := fileOp.TarGzExtractPro(file, dstDir, ""); err != nil { |
| 257 | _ = os.RemoveAll(dstDir) |
| 258 | return "", err |
| 259 | } |
| 260 | } |
| 261 | if strings.HasSuffix(fileName, ".zip") { |
| 262 | archiver, err := files.NewShellArchiver(files.Zip) |
| 263 | if err != nil { |
| 264 | _ = os.RemoveAll(dstDir) |
| 265 | return "", err |
| 266 | } |
| 267 | if err := archiver.Extract(context.Background(), file, dstDir, ""); err != nil { |
| 268 | _ = os.RemoveAll(dstDir) |
| 269 | return "", err |
| 270 | } |
| 271 | } |
| 272 | global.LOG.Infof("decompress file %s successful, now start to check test.sql is exist", file) |
| 273 | var sqlFiles []string |
| 274 | hasTestSql := false |
| 275 | _ = filepath.Walk(dstDir, func(path string, info os.FileInfo, err error) error { |
| 276 | if err != nil { |
| 277 | return nil |
| 278 | } |
| 279 | if !info.IsDir() && strings.HasSuffix(info.Name(), ".sql") { |
| 280 | sqlFiles = append(sqlFiles, path) |
| 281 | if info.Name() == "test.sql" { |
| 282 | hasTestSql = true |
| 283 | } |
| 284 | } |
| 285 | return nil |
| 286 | }) |
| 287 | if len(sqlFiles) == 1 { |
| 288 | return sqlFiles[0], nil |
| 289 | } |
| 290 | if !hasTestSql { |
| 291 | _ = os.RemoveAll(dstDir) |
| 292 | return "", fmt.Errorf("no such file named test.sql in %s", fileName) |
| 293 | } |
| 294 | return "", nil |
| 295 | } |