github.com/pluswing/datasync@v1.1.1-0.20240218052257-9077f6fc4ae3/cmd/import.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/pluswing/datasync/data"
     7  	"github.com/pluswing/datasync/dump/dump_mysql"
     8  	"github.com/pluswing/datasync/file"
     9  	"github.com/spf13/cobra"
    10  )
    11  
    12  var database string
    13  
    14  var importCmd = &cobra.Command{
    15  	Use:   "import",
    16  	Short: "import mysql dump file",
    17  	Long:  `import mysql dump file`,
    18  	Args:  cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
    19  	Run: func(cmd *cobra.Command, args []string) {
    20  
    21  		_, err := file.FindCurrentDir()
    22  		if err != nil {
    23  			fmt.Println("datasync.yaml not found.\nPlease run `datasync init`")
    24  			return
    25  		}
    26  
    27  		dumpFile := args[0]
    28  
    29  		for _, target := range setting.Targets {
    30  			data.DispatchTarget(target, data.TargetFuncTable{
    31  				Mysql: func(config data.TargetMysqlType) {
    32  					if database == config.Database {
    33  						dump_mysql.Import(dumpFile, config)
    34  					}
    35  				},
    36  				File: func(config data.TargetFileType) {
    37  					// no support
    38  				},
    39  			})
    40  		}
    41  	},
    42  }
    43  
    44  func init() {
    45  	rootCmd.AddCommand(importCmd)
    46  
    47  	importCmd.Flags().StringVarP(&database, "database", "d", "", "database name")
    48  }