file-store/cmd/ee.go

63 lines
1.9 KiB
Go
Raw Permalink Normal View History

2022-08-24 02:54:01 +00:00
package cmd
import (
"github.com/spf13/cobra"
sf "git.keganmyers.com/terribleplan/file-store/pkg/storefile"
)
var (
ee_Size int32
ee_Shards uint16
ee_Parity uint16
ee_Name string
ee_Overwrite bool
)
func init() {
root.AddCommand(ee)
ee_encode.Flags().Int32Var(&ee_Size, "size", 10*1024*1024, "Size (in bytes) of each shard (default: 10485760, 10 MiB)")
ee_encode.Flags().Uint16Var(&ee_Shards, "shards", 10, "Number of shards to generate for file (default: 10)")
ee_encode.Flags().Uint16Var(&ee_Parity, "parity", 4, "Number of parity records to generate for file (default: 4)")
ee_encode.Flags().StringVar(&ee_Name, "name", "", "Name to store with the file (default: file name, without path)")
ee.AddCommand(ee_encode)
ee_decode.Flags().StringVar(&ee_Name, "output", "", "Path to store with the file (default: path in meta.json, relative to cwd)")
ee_decode.Flags().BoolVar(&ee_Overwrite, "overwrite", false, "Whether to overwrite any existing output file (default: false)")
ee.AddCommand(ee_decode)
}
var ee = &cobra.Command{
Use: "ee",
Short: "Do things with erasure encoding",
}
var ee_encode = &cobra.Command{
Use: "encode [file] [output-folder]",
Short: "Erasure encode a file",
Long: `Erasure encode a file.
--shards and --parity controls output size and redundancy.
Output size is file_size * (shards + parity / shards).
Up to 'parity' shards can be lost while maintaining the ability to read/recover the file.`,
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
if err := sf.StoreFile(args[0], args[1], ee_Size, ee_Shards, ee_Parity, ee_Name); err != nil {
panic(err)
}
},
}
var ee_decode = &cobra.Command{
Use: "decode [encoded-folder]",
Short: "Erasure decode a file",
Long: `Erasure decode a file.`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if err := sf.ReadFile(args[0], ee_Name, ee_Overwrite); err != nil {
panic(err)
}
},
}