github.com/pluswing/datasync@v1.1.1-0.20240218052257-9077f6fc4ae3/cmd/rm.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 8 "github.com/pluswing/datasync/data" 9 "github.com/pluswing/datasync/file" 10 "github.com/spf13/cobra" 11 ) 12 13 var rmCmd = &cobra.Command{ 14 Use: "rm", 15 Short: "remove dump", 16 Long: `remove dump`, 17 Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), 18 Run: func(cmd *cobra.Command, args []string) { 19 _, err := file.FindCurrentDir() 20 if err != nil { 21 fmt.Println("datasync.yaml not found.\nPlease run `datasync init`") 22 return 23 } 24 25 versionId := args[0] 26 27 ds := file.ReadLocalDataSyncFile() 28 29 newHistories := []data.VersionType{} 30 for _, v := range ds.Histories { 31 if v.Id != versionId { 32 newHistories = append(newHistories, v) 33 } 34 } 35 if len(ds.Histories) == len(newHistories) { 36 fmt.Printf("version not found. %s\n", versionId) 37 return 38 } 39 ds.Histories = newHistories 40 file.WriteLocalDataSyncFile(ds) 41 42 dir, err := file.DataDir() 43 cobra.CheckErr(err) 44 os.Remove(filepath.Join(dir, versionId)) 45 46 fmt.Printf("removed. %s\n", versionId) 47 }, 48 } 49 50 func init() { 51 rootCmd.AddCommand(rmCmd) 52 }