main.go 1.76 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
package main

import (
	"fmt"
	"os"
	"strconv"

	"github.com/ipfs/go-ds-flatfs"
)

// To convert from the old format to a new format with a different
// sharding function use:
//   flatfs upgrade blocks 5
//   flatfs create blocks-new v1/next-to-last/2
//   flatfs move blocks blocks-new
//   rmdir blocks
//   mv blocks-new blocks
// to do the reverse
//   flatfs create blocks-new v1/prefix/5
//   flatfs move blocks blocks-new
//   rmdir blocks
//   mv blocks-new blocks
//   flatfs downgrade blocks

func usage() {
	fmt.Fprintf(os.Stderr, "usage: %s create DIR SHARDFUN | upgrade DIR PREFIXLEN | downgrade DIR | move OLDDIR NEWDIR\n", os.Args[0])
	os.Exit(1)
}

func fail(err error) {
	fmt.Fprintf(os.Stderr, "%v\n", err)
	os.Exit(1)
}

func main() {
	if len(os.Args) < 2 {
		usage()
	}

	switch os.Args[1] {
	case "create":
		if len(os.Args) != 4 {
			usage()
		}
		dir := os.Args[2]
		funStr := os.Args[3]
		if funStr[0] != '/' {
			if funStr[0] != 'v' { // and version if not provided
				funStr = "v1/" + funStr
			}
			funStr = flatfs.PREFIX + funStr
		}
		fun, err := flatfs.ParseShardFunc(funStr)
		if err != nil {
			fail(err)
		}
		err = flatfs.Create(dir, fun)
		if err != nil {
			fail(err)
		}
	case "upgrade":
		if len(os.Args) != 4 {
			usage()
		}
		dir := os.Args[2]
		prefixLen, err := strconv.Atoi(os.Args[3])
		if err != nil {
			fail(err)
		}
		err = flatfs.UpgradeV0toV1(dir, prefixLen)
		if err != nil {
			fail(err)
		}
	case "downgrade":
		if len(os.Args) != 3 {
			usage()
		}
		dir := os.Args[2]
		err := flatfs.DowngradeV1toV0(dir)
		if err != nil {
			fail(err)
		}
	case "move":
		if len(os.Args) != 4 {
			usage()
		}
		oldDir := os.Args[2]
		newDir := os.Args[3]
		err := flatfs.Move(oldDir, newDir, os.Stderr)
		if err != nil {
			fail(err)
		}
	default:
		usage()
	}
}