diff --git a/base/component/component.go b/base/component/component.go index 161b70ad8..260d6658d 100644 --- a/base/component/component.go +++ b/base/component/component.go @@ -13,7 +13,7 @@ import ( type Component interface { Base() *BaseComponent Compare(Component) (bool, error) - WriteTo(dirPath string) (string, error) + WriteTo(dirPath string) (targetFile string, err error) RemoveFrom(dirPath string) error DropDerivedValues() LoadAndFill(*dataset.Dataset) error diff --git a/base/component/kinds.go b/base/component/kinds.go index d072dbe30..40b4c2d9d 100644 --- a/base/component/kinds.go +++ b/base/component/kinds.go @@ -34,7 +34,7 @@ func (fc *FilesysComponent) IsEmpty() bool { } // WriteTo writes the component as a file to the directory -func (fc *FilesysComponent) WriteTo(dirPath string) (string, error) { +func (fc *FilesysComponent) WriteTo(dirPath string) (targetFile string, err error) { return "", fmt.Errorf("cannot write filesys component") } @@ -82,7 +82,7 @@ func (dc *DatasetComponent) Compare(compare Component) (bool, error) { } // WriteTo writes the component as a file to the directory -func (dc *DatasetComponent) WriteTo(dirPath string) (string, error) { +func (dc *DatasetComponent) WriteTo(dirPath string) (targetFile string, err error) { return "", fmt.Errorf("cannot write dataset component") } @@ -154,12 +154,12 @@ func (mc *MetaComponent) Compare(compare Component) (bool, error) { } // WriteTo writes the component as a file to the directory -func (mc *MetaComponent) WriteTo(dirPath string) (string, error) { +func (mc *MetaComponent) WriteTo(dirPath string) (targetFile string, err error) { if mc.DisableSerialization { return "", fmt.Errorf("serialization is disabled") } - if err := mc.LoadAndFill(nil); err != nil { - return "", err + if err = mc.LoadAndFill(nil); err != nil { + return } // Okay to output an empty meta, we do so for `qri init`. if mc.Value != nil { @@ -239,9 +239,9 @@ func (sc *StructureComponent) Compare(compare Component) (bool, error) { } // WriteTo writes the component as a file to the directory -func (sc *StructureComponent) WriteTo(dirPath string) (string, error) { - if err := sc.LoadAndFill(nil); err != nil { - return "", err +func (sc *StructureComponent) WriteTo(dirPath string) (targetFile string, err error) { + if err = sc.LoadAndFill(nil); err != nil { + return } if sc.Value != nil && !sc.Value.IsEmpty() { return writeComponentFile(sc.Value, dirPath, "structure.json") @@ -318,9 +318,9 @@ func (cc *CommitComponent) Compare(compare Component) (bool, error) { } // WriteTo writes the component as a file to the directory -func (cc *CommitComponent) WriteTo(dirPath string) (string, error) { - if err := cc.LoadAndFill(nil); err != nil { - return "", err +func (cc *CommitComponent) WriteTo(dirPath string) (targetFile string, err error) { + if err = cc.LoadAndFill(nil); err != nil { + return } if cc.Value != nil && !cc.Value.IsEmpty() { return writeComponentFile(cc.Value, dirPath, "commit.json") @@ -494,11 +494,11 @@ func (bc *BodyComponent) StructuredData() (interface{}, error) { } // WriteTo writes the component as a file to the directory -func (bc *BodyComponent) WriteTo(dirPath string) (string, error) { +func (bc *BodyComponent) WriteTo(dirPath string) (targetFile string, err error) { if bc.Value == nil { - err := bc.LoadAndFill(nil) + err = bc.LoadAndFill(nil) if err != nil { - return "", err + return } } body := bc.Value @@ -510,7 +510,7 @@ func (bc *BodyComponent) WriteTo(dirPath string) (string, error) { return "", err } bodyFilename := fmt.Sprintf("body.%s", bc.Format) - targetFile := filepath.Join(dirPath, bodyFilename) + targetFile = filepath.Join(dirPath, bodyFilename) return targetFile, ioutil.WriteFile(targetFile, data, os.ModePerm) } @@ -585,14 +585,14 @@ func (rc *ReadmeComponent) Compare(compare Component) (bool, error) { } // WriteTo writes the component as a file to the directory -func (rc *ReadmeComponent) WriteTo(dirPath string) (string, error) { - if err := rc.LoadAndFill(nil); err != nil { - return "", err +func (rc *ReadmeComponent) WriteTo(dirPath string) (targetFile string, err error) { + if err = rc.LoadAndFill(nil); err != nil { + return } if rc.Value != nil && !rc.Value.IsEmpty() { - targetFile := filepath.Join(dirPath, fmt.Sprintf("readme.%s", rc.Format)) - if err := ioutil.WriteFile(targetFile, rc.Value.ScriptBytes, os.ModePerm); err != nil { - return targetFile, err + targetFile = filepath.Join(dirPath, fmt.Sprintf("readme.%s", rc.Format)) + if err = ioutil.WriteFile(targetFile, rc.Value.ScriptBytes, os.ModePerm); err != nil { + return } } return "", nil diff --git a/fsi/fsi.go b/fsi/fsi.go index 90800aa59..57f8796e5 100644 --- a/fsi/fsi.go +++ b/fsi/fsi.go @@ -93,16 +93,16 @@ func (fsi *FSI) LinkedRefs(offset, limit int) ([]repo.DatasetRef, error) { // CreateLink links a working directory to a dataset. Returns the reference alias, and a // rollback function if no error occurs -func (fsi *FSI) CreateLink(dirPath, refStr string) (string, func(), error) { - nullFunc := func() {} +func (fsi *FSI) CreateLink(dirPath, refStr string) (alias string, rollback func(), err error) { + rollback = func() {} ref, err := repo.ParseDatasetRef(refStr) if err != nil { - return "", nullFunc, err + return "", rollback, err } err = repo.CanonicalizeDatasetRef(fsi.repo, &ref) if err != nil && err != repo.ErrNotFound && err != repo.ErrNoHistory { - return ref.String(), nullFunc, err + return ref.String(), rollback, err } if stored, err := fsi.repo.GetRef(ref); err == nil { @@ -110,20 +110,20 @@ func (fsi *FSI) CreateLink(dirPath, refStr string) (string, func(), error) { // There is already a link for this dataset, see if that link still exists. targetPath := filepath.Join(stored.FSIPath, QriRefFilename) if _, err := os.Stat(targetPath); err == nil { - return "", nullFunc, fmt.Errorf("'%s' is already linked to %s", ref.AliasString(), stored.FSIPath) + return "", rollback, fmt.Errorf("'%s' is already linked to %s", ref.AliasString(), stored.FSIPath) } } } ref.FSIPath = dirPath if err = fsi.repo.PutRef(ref); err != nil { - return "", nullFunc, err + return "", rollback, err } // If future steps fail, remove the ref we just put removeRefFunc := func() { - log.Debugf("removing repo.ref \"%s\" during rollback", ref) + log.Debugf("removing repo.ref %q during rollback", ref) if err := fsi.repo.DeleteRef(ref); err != nil { - log.Debugf("error while removing repo.ref %s: \"%s\"", ref, err) + log.Debugf("error while removing repo.ref %q: %s", ref, err) } } diff --git a/fsi/init.go b/fsi/init.go index 85caf30a7..44cdb1d66 100644 --- a/fsi/init.go +++ b/fsi/init.go @@ -41,8 +41,10 @@ func (fsi *FSI) InitDataset(p InitParams) (name string, err error) { log.Debug("did rollback InitDataset due to error") } defer func() { - log.Debug("InitDataset rolling back...") - rollback() + if rollback != nil { + log.Debug("InitDataset rolling back...") + rollback() + } }() if p.Dir == "" { @@ -76,9 +78,9 @@ func (fsi *FSI) InitDataset(p InitParams) (name string, err error) { // steps fail. rollback = concatFunc( func() { - log.Debugf("removing directory \"%s\" during rollback", targetPath) + log.Debugf("removing directory %q during rollback", targetPath) if err := os.Remove(targetPath); err != nil { - log.Debugf("error while removing directory %s: \"%s\"", targetPath, err) + log.Debugf("error while removing directory %q: %s", targetPath, err) } }, rollback) } @@ -174,9 +176,9 @@ func (fsi *FSI) InitDataset(p InitParams) (name string, err error) { // If future steps fail, rollback the components that have been written rollback = concatFunc(func() { if wroteFile != "" { - log.Debugf("removing file \"%s\" during rollback", wroteFile) + log.Debugf("removing file %q during rollback", wroteFile) if err := os.Remove(wroteFile); err != nil { - log.Debugf("error while removing file \"%s\": %s", wroteFile, err) + log.Debugf("error while removing file %q: %s", wroteFile, err) } } }, rollback) @@ -191,9 +193,8 @@ func (fsi *FSI) InitDataset(p InitParams) (name string, err error) { return name, err } - rollback = func() { - // Success, no need to rollback. - } + // Success, no need to rollback. + rollback = nil return name, nil }