github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/repo/fs/migrate.go (about)

     1  package fsrepo
     2  
     3  import (
     4  	"encoding/json"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"github.com/qri-io/qri/repo"
    10  )
    11  
    12  // maybeCreateFlatbufferRefsFile creates a flatbuffer from an existing ds_refs
    13  // json file. repo files used to be stored as json, and we're moving to
    14  // flatbuffers
    15  // TODO (b5) - we should consider keeping both JSON and flatbuffer records for a
    16  // few releases if the json ds_ref.json file exists.
    17  func maybeCreateFlatbufferRefsFile(repoPath string) (migrated bool, err error) {
    18  	fbPath := filepath.Join(repoPath, Filepath(FileRefs))
    19  	if _, err := os.Stat(fbPath); os.IsNotExist(err) {
    20  		jsonPath := filepath.Join(repoPath, Filepath(FileJSONRefs))
    21  		if jsonData, err := ioutil.ReadFile(jsonPath); err == nil {
    22  			jsonRefs := repo.RefList{}
    23  			if err = json.Unmarshal(jsonData, &jsonRefs); err == nil {
    24  				return true, ioutil.WriteFile(fbPath, repo.FlatbufferBytes(jsonRefs), os.ModePerm)
    25  			}
    26  		}
    27  	}
    28  	return false, nil
    29  }