github.com/pluswing/datasync@v1.1.1-0.20240218052257-9077f6fc4ae3/cmd/apply.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 "os" 6 7 "github.com/pluswing/datasync/compress" 8 "github.com/pluswing/datasync/data" 9 "github.com/pluswing/datasync/dump/dump_file" 10 "github.com/pluswing/datasync/dump/dump_mysql" 11 "github.com/pluswing/datasync/file" 12 "github.com/spf13/cobra" 13 ) 14 15 var applyCmd = &cobra.Command{ 16 Use: "apply [flags] [version_id]", 17 Short: "apply version", 18 Long: `apply version`, 19 Args: cobra.MatchAll(cobra.RangeArgs(0, 1), cobra.OnlyValidArgs), 20 Run: func(cmd *cobra.Command, args []string) { 21 22 _, err := file.FindCurrentDir() 23 if err != nil { 24 fmt.Println("datasync.yaml not found.\nPlease run `datasync init`") 25 return 26 } 27 28 version, err := file.GetCurrentVersion(args) 29 if err != nil { 30 fmt.Println(err.Error()) 31 return 32 } 33 34 dir, err := file.DataDir() 35 cobra.CheckErr(err) 36 tmpFile := version.FileNameWithDir(dir) 37 38 s, err := os.Stat(tmpFile) 39 if err != nil || s.IsDir() { 40 fmt.Printf("file not found. \nplease run `datasync pull %s`\n", version.Id) 41 } 42 43 tmpDir, err := file.MakeTempDir() 44 cobra.CheckErr(err) 45 defer os.RemoveAll(tmpDir) 46 47 fmt.Println("decompressing...") 48 compress.Decompress(tmpDir, tmpFile) 49 50 for _, target := range setting.Targets { 51 data.DispatchTarget(target, data.TargetFuncTable{ 52 Mysql: func(config data.TargetMysqlType) { 53 dump_mysql.Import(dump_mysql.MysqlDumpFile(tmpDir, config), config) 54 }, 55 File: func(config data.TargetFileType) { 56 dump_file.Expand(tmpDir, config) 57 }, 58 }) 59 } 60 61 fmt.Println("finalizing...") 62 err = file.UpdateVersionFile(version.Id) 63 cobra.CheckErr(err) 64 65 fmt.Printf("apply Succeeded. version_id = %s\n", version.Id) 66 }, 67 } 68 69 func init() { 70 rootCmd.AddCommand(applyCmd) 71 }