Skip to content

Commit

Permalink
8-to-9: address self review
Browse files Browse the repository at this point in the history
  • Loading branch information
hsanjuan committed Mar 5, 2020
1 parent a765ac1 commit 6443d71
Showing 1 changed file with 39 additions and 51 deletions.
90 changes: 39 additions & 51 deletions ipfs-8-to-9/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,10 @@ func (m Migration) Reversible() bool {

const keyFilenamePrefix = "key_"

func isEncoded(name string) bool {
if !strings.HasPrefix(name, keyFilenamePrefix) {
return false
}
const keystoreRoot = "keystore"

func isEncoded(name string) bool {
_, err := decode(name)

return err == nil
}

Expand All @@ -52,49 +49,23 @@ func decode(name string) (string, error) {
nameWithoutPrefix := strings.ToUpper(name[len(keyFilenamePrefix):])
data, err := base32.StdEncoding.DecodeString(nameWithoutPrefix)

if err != nil {
return "", err
}

decodedName := string(data[:])

return decodedName, nil
return string(data), err
}

func (m Migration) Apply(opts migrate.Options) error {
log.Verbose = opts.Verbose
log.Log("applying %s repo migration", m.Versions())

keystoreRoot := filepath.Join(opts.Path, "keystore")
fileInfos, err := ioutil.ReadDir(keystoreRoot)
err := m.encodeDecode(
opts,
isEncoded, // skip if already encoded
encode,
)

if err != nil {
return err
}

for _, info := range fileInfos {
if info.IsDir() {
log.Log("skipping ", info.Name(), " as it is directory!")
continue
}

if isEncoded(info.Name()) {
log.Log("skipping ", info.Name(), " as it is already encoded!")
continue
}

log.VLog("migrating key's filename: ", info.Name())
encodedName, err := encode(info.Name())
if err != nil {
return err
}

os.Rename(
filepath.Join(keystoreRoot, info.Name()),
filepath.Join(keystoreRoot, encodedName),
)
}

err = mfsr.RepoPath(opts.Path).WriteVersion("9")
if err != nil {
log.Error("failed to update version file to 9")
Expand All @@ -106,11 +77,8 @@ func (m Migration) Apply(opts migrate.Options) error {
return nil
}

func (m Migration) Revert(opts migrate.Options) error {
log.Verbose = opts.Verbose
log.Log("reverting migration")

keystoreRoot := filepath.Join(opts.Path, "keystore")
func (m Migration) encodeDecode(opts migrate.Options, shouldApplyCodec func(string) bool, codec func(string) (string, error)) error {
keystoreRoot := filepath.Join(opts.Path, keystoreRoot)
fileInfos, err := ioutil.ReadDir(keystoreRoot)

if err != nil {
Expand All @@ -119,25 +87,45 @@ func (m Migration) Revert(opts migrate.Options) error {

for _, info := range fileInfos {
if info.IsDir() {
log.Log("skipping", info.Name(), "as it is directory!")
log.Log("skipping ", info.Name(), " as it is directory!")
continue
}

if !isEncoded(info.Name()) {
log.Log("skipping", info.Name(), "as it is not encoded!")
if shouldApplyCodec(info.Name()) {
log.Log("skipping ", info.Name(), ". Already in expected format!")
continue
}

log.VLog("reverting key's filename:", info.Name())
decodedName, err := decode(info.Name())
log.VLog("Renaming key's filename: ", info.Name())
encodedName, err := codec(info.Name())
if err != nil {
return err
}

os.Rename(
filepath.Join(keystoreRoot, info.Name()),
filepath.Join(keystoreRoot, decodedName),
)
src := filepath.Join(keystoreRoot, info.Name())
dest := filepath.Join(keystoreRoot, encodedName)

if err := os.Rename(src, dest); err != nil {
return err
}
}
return nil
}

func (m Migration) Revert(opts migrate.Options) error {
log.Verbose = opts.Verbose
log.Log("reverting migration")

err := m.encodeDecode(
opts,
func(name string) bool {
return !isEncoded(name) // skip if not encoded
},
decode,
)

if err != nil {
return err
}

err = mfsr.RepoPath(opts.Path).WriteVersion("8")
Expand Down

0 comments on commit 6443d71

Please sign in to comment.