Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid panic even if state file is empty #39

Merged
merged 2 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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