From 3903828fa0296f4a267638071f68ff7d72f96614 Mon Sep 17 00:00:00 2001 From: b4b4r07 Date: Thu, 24 Mar 2022 21:24:23 +0900 Subject: [PATCH 1/2] Avoid panic even if state file is empty --- pkg/state/state.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/state/state.go b/pkg/state/state.go index c0fbe44..7a226d0 100644 --- a/pkg/state/state.go +++ b/pkg/state/state.go @@ -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() @@ -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{}, From 394b601baa1d3a79e4eed4dab9ccd95ce288b0b2 Mon Sep 17 00:00:00 2001 From: b4b4r07 Date: Thu, 24 Mar 2022 21:55:34 +0900 Subject: [PATCH 2/2] Add test --- pkg/state/state_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/state/state_test.go b/pkg/state/state_test.go index d1a88e3..8f4899a 100644 --- a/pkg/state/state_test.go +++ b/pkg/state/state_test.go @@ -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",