Skip to content

Commit

Permalink
Fix bug when state.json does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
babarot committed Mar 21, 2022
1 parent 6a74aab commit 704c778
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cmd/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (m *metaCmd) init() error {

s, err := state.Open(filepath.Join(root, "state.json"), resourcers)
if err != nil {
return errors.Wrap(err, "faield to open state file")
return errors.Wrap(err, "failed to open state file")
}
m.state = s

Expand Down
15 changes: 2 additions & 13 deletions pkg/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"os"
"path/filepath"
"sync"
"syscall"

"github.com/google/go-cmp/cmp"
)
Expand Down Expand Up @@ -87,7 +86,8 @@ var exists = func(path string) bool {
var ReadStateFile = func(filename string) ([]byte, error) {
f, err := os.Open(filename)
if err != nil {
return nil, pathError(err)
// return empty json contents if state.json does not exist
return []byte(`{}`), nil
}
defer f.Close()

Expand All @@ -103,17 +103,6 @@ var SaveStateFile = func(filename string) (io.Writer, error) {
return os.Create(filename)
}

func pathError(err error) error {
var pathError *os.PathError
if errors.As(err, &pathError) && errors.Is(pathError.Err, syscall.ENOTDIR) {
if p := findRegularFile(pathError.Path); p != "" {
return fmt.Errorf("remove or rename regular file `%s` (must be a directory)", p)
}

}
return err
}

func findRegularFile(p string) string {
for {
if s, err := os.Stat(p); err == nil && s.Mode().IsRegular() {
Expand Down

0 comments on commit 704c778

Please sign in to comment.