Skip to content

Commit

Permalink
Merge pull request #39 from b4b4r07/fix/38
Browse files Browse the repository at this point in the history
Avoid panic even if state file is empty
  • Loading branch information
b4b4r07 authored Mar 24, 2022
2 parents b75e39b + 394b601 commit 6199c37
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
3 changes: 2 additions & 1 deletion pkg/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ var ReadStateFile = func(filename string) ([]byte, error) {
f, err := os.Open(filename)
if err != nil {
// return empty json contents if state.json does not exist
return []byte(`{}`), nil
return []byte(`{"resources":{}}`), nil
}
defer f.Close()

Expand Down Expand Up @@ -244,6 +244,7 @@ func Keys(resources []Resource) []string {

func Open(path string, resourcers []Resourcer) (*State, error) {
s := State{
Self: Self{Resources: map[ID]Resource{}},
path: path,
packages: map[ID]Resource{},
mu: sync.RWMutex{},
Expand Down
2 changes: 1 addition & 1 deletion pkg/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestOpen(t *testing.T) {
}{
"Empty": {
filename: "empty.json",
state: &State{path: "empty.json"},
state: &State{Self: Self{Resources: map[string]Resource{}}, path: "empty.json"},
},
"Open": {
filename: "state.json",
Expand Down

0 comments on commit 6199c37

Please sign in to comment.