Skip to content

Commit

Permalink
fix(fsi): Cleanup quote usage, name return values
Browse files Browse the repository at this point in the history
  • Loading branch information
dustmop committed Nov 14, 2019
1 parent bc3eb42 commit 1fa2f2b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 39 deletions.
2 changes: 1 addition & 1 deletion base/component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 21 additions & 21 deletions base/component/kinds.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down Expand Up @@ -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")
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand All @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions fsi/fsi.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,37 +93,37 @@ 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 {
if stored.FSIPath != "" {
// 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)
}
}

Expand Down
19 changes: 10 additions & 9 deletions fsi/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand All @@ -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
}

Expand Down

0 comments on commit 1fa2f2b

Please sign in to comment.