github.com/pluswing/datasync@v1.1.1-0.20240218052257-9077f6fc4ae3/cmd/pull.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 6 "github.com/pluswing/datasync/data" 7 "github.com/pluswing/datasync/file" 8 "github.com/pluswing/datasync/storage" 9 "github.com/spf13/cobra" 10 ) 11 12 var pullCmd = &cobra.Command{ 13 Use: "pull [flags] [version_id]", 14 Short: "pull remote version", 15 Long: `pull remote version`, 16 Args: cobra.MatchAll(cobra.RangeArgs(0, 1), cobra.OnlyValidArgs), 17 Run: func(cmd *cobra.Command, args []string) { 18 _, err := file.FindCurrentDir() 19 if err != nil { 20 fmt.Println("datasync.yaml not found.\nPlease run `datasync init`") 21 return 22 } 23 24 version, err := file.GetCurrentVersion(args) 25 if err != nil { 26 fmt.Println(err.Error()) 27 return 28 } 29 30 var tmpFile string 31 data.DispatchStorage(setting.Storage, data.StorageFuncTable{ 32 Gcs: func(config data.StorageGcsType) { 33 tmpFile = storage.Download(version.FileName(), config) 34 }, 35 }) 36 37 dir, err := file.DataDir() 38 cobra.CheckErr(err) 39 40 err = file.MoveFile(tmpFile, version.FileNameWithDir(dir)) 41 cobra.CheckErr(err) 42 43 fmt.Printf("pull Succeeded. version_id = %s\n", version.Id) 44 }, 45 } 46 47 func init() { 48 rootCmd.AddCommand(pullCmd) 49 }