package main import ( "fmt" "path" "strings" "dev.justinjudd.org/justin/bmap" "github.com/spf13/cobra" ) func main() { var rootCmd = &cobra.Command{ Use: "bmap", Short: "Create block map (bmap) files and copy files.", Long: "Create block map (bmap) files and copy files.", Run: func(cmd *cobra.Command, args []string) { cmd.Help() }, } var output string var noChecksum bool var createCmd = &cobra.Command{ Use: "create image", Short: "Create block map (bmap) file.", Long: "Create block map (bmap) file.", Run: func(cmd *cobra.Command, args []string) { if len(args) != 1 { fmt.Println("Must supply image file") return } create(args[0], output, !noChecksum) }, } createCmd.Flags().StringVarP(&output, "output", "o", "", "output file name") createCmd.Flags().BoolVar(&noChecksum, "no-checksum", false, "don't generate block sum for block ranges") var bmapFile string var nobmap bool var noVerify bool var copyCmd = &cobra.Command{ Use: "copy image dest", Short: "Write an image using bmap .", Long: "Write an image using bmap.", Run: func(cmd *cobra.Command, args []string) { if len(args) != 2 { fmt.Println("Must supply image file and destination file") return } copy(args[0], args[1], bmapFile, nobmap, !noVerify) }, } copyCmd.Flags().StringVar(&bmapFile, "bmap", "", "block map file for the image") copyCmd.Flags().BoolVar(&nobmap, "nobmap", false, "allow copying withut a bmap") copyCmd.Flags().BoolVar(&noVerify, "no-verify", false, "do not verify the checksum of data before writing.") rootCmd.AddCommand(createCmd, copyCmd) var cleanCmd = &cobra.Command{ Use: "clean input", Short: "clean a bmap file.", Long: "clean a bmap file.", Run: func(cmd *cobra.Command, args []string) { if len(args) != 1 { fmt.Println("Must supply bmap file") return } clean(args[0]) }, } rootCmd.AddCommand(cleanCmd) rootCmd.Execute() } func create(image string, output string, noChecksum bool) { b, err := bmap.NewBMap(image, noChecksum) if err != nil { println("Error reading image file:", err.Error()) } err = b.Write(output) if err != nil { println("Error writing bmap file", err.Error()) } } func copy(image string, dest string, bmapFile string, nobmap bool, noVerify bool) { if !nobmap { if len(bmapFile) == 0 { bmapFile = strings.TrimSuffix(image, path.Ext(image)) + ".bmap" } b, err := bmap.LoadFromFile(bmapFile, !noVerify) if err != nil { fmt.Println("Unable to read bmap file: ", bmapFile) return } err = b.Copy(image, dest, noVerify) if err != nil { fmt.Println("Error copying image: ", err.Error()) return } } else { err := bmap.Copy(image, dest) if err != nil { fmt.Println("Error copying image: ", err.Error()) return } } } func clean(input string) { err := bmap.CleanBMap(input) if err != nil { fmt.Println(err.Error()) } }