Skip to content

🔧 feat: Enhance config retrieval with improved error handling and diff#1931

Merged
mkmccarty merged 6 commits intomainfrom
mm-branch-1
Dec 12, 2025
Merged

🔧 feat: Enhance config retrieval with improved error handling and diff#1931
mkmccarty merged 6 commits intomainfrom
mm-branch-1

Conversation

@mkmccarty
Copy link
Copy Markdown
Owner

No description provided.

Copilot AI review requested due to automatic review settings December 12, 2025 04:36
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the config retrieval functionality by extracting a reusable APICall function and enhancing the GetConfigFromAPI function with improved error handling and diff capabilities. The changes enable better debugging through MD5-based change detection and JSON diffing of configuration updates.

Key Changes

  • Extracted APICall as a generic reusable function for making Egg Inc API calls
  • Added MD5 comparison to avoid unnecessary file writes when config hasn't changed
  • Integrated jsondiff library to display configuration differences for debugging purposes

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 8 comments.

File Description
src/ei/ei_api.go Refactored GetConfigFromAPI function and extracted APICall as a reusable function; added MD5-based change detection and JSON diff output for config changes
go.mod Added jsondiff v0.7.0 dependency and updated google.golang.org/genai from v1.38.0 to v1.39.0
go.sum Updated checksums for new jsondiff dependency and its transitive dependencies (tidwall packages), plus updated versions for golang.org/x/exp, golang.org/x/mod, golang.org/x/tools, and google.golang.org/genai

Comment thread src/ei/ei_api.go Outdated
Comment thread src/ei/ei_api.go Outdated
Comment thread src/ei/ei_api.go Outdated
Comment thread src/ei/ei_api.go Outdated
Comment on lines +361 to +378
jsonData, err := json.MarshalIndent(configResponse, "", " ")
// Files are different, if we have an existing file, I want a diff
pod := []byte{}
if existingData, readErr := os.ReadFile("ttbb-data/ei-config.json"); readErr == nil {
pod = existingData
}
if patch, perr := jsondiff.Compare(pod, jsonData); perr == nil {
if b, merr := json.MarshalIndent(patch, "", " "); merr == nil {
_, _ = os.Stdout.Write(b)
} else {
log.Printf("Failed to marshal config diff; proceeding to write file: %v", merr)
}
} else {
log.Printf("Config diff failed; proceeding to write file: %v", perr)
}

defer func() {
if err := response.Body.Close(); err != nil {
// Handle the error appropriately, e.g., logging or taking corrective actions
log.Printf("Failed to close: %v", err)
_ = os.MkdirAll("ttbb-data", os.ModePerm)
if err = os.WriteFile("ttbb-data/ei-config.json", []byte(jsonData), 0644); err != nil {
Copy link

Copilot AI Dec 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The err variable is declared here but is also used later at line 378 without being reassigned. If the marshaling on line 361 fails, err will be non-nil, but this error is never checked. Then at line 378, the err variable is reassigned from WriteFile. This could lead to confusion about which operation actually failed if an error is logged at line 379. Each error should be checked immediately after the operation that produces it.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot open a new pull request to apply changes based on this feedback

Comment thread src/ei/ei_api.go
Comment thread src/ei/ei_api.go
Comment on lines +331 to +341
response := APICall(reqURL, &getConfigRequest)

reqBin, err := proto.Marshal(&getConfigRequest)
if err != nil {
log.Print(err)
return false
}
values := url.Values{}
reqDataEncoded := enc.EncodeToString(reqBin)
values.Set("data", string(reqDataEncoded))
configResponse := &ConfigResponse{}
opts := proto.UnmarshalOptions{
DiscardUnknown: true,
}
err := opts.Unmarshal(response, configResponse)
if err != nil {
log.Print(err)
return false
}
Copy link

Copilot AI Dec 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing nil check for the response from APICall. If APICall returns nil due to any error condition (marshaling, network, decompression, etc.), the subsequent Unmarshal will attempt to process nil data, which could lead to unexpected behavior. Add a nil check before attempting to unmarshal the response.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot open a new pull request to apply changes based on this feedback

Comment thread src/ei/ei_api.go Outdated
}
}
}
jsonData, err := json.MarshalIndent(configResponse, "", " ")
Copy link

Copilot AI Dec 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The jsonData variable is marshaled twice - once inside the MD5 comparison block (line 351) and again here (line 361). This is inefficient and could lead to inconsistencies if marshaling produces different results. Consider reusing the newJSONData from the comparison block or restructuring the logic to marshal only once.

Copilot uses AI. Check for mistakes.
Comment thread src/ei/ei_api.go Outdated
mkmccarty and others added 5 commits December 11, 2025 20:47
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI commented Dec 12, 2025

@mkmccarty I've opened a new pull request, #1932, to work on those changes. Once the pull request is ready, I'll request review from you.

Copy link
Copy Markdown
Contributor

Copilot AI commented Dec 12, 2025

@mkmccarty I've opened a new pull request, #1933, to work on those changes. Once the pull request is ready, I'll request review from you.

@mkmccarty mkmccarty merged commit 577804a into main Dec 12, 2025
9 of 15 checks passed
@mkmccarty mkmccarty deleted the mm-branch-1 branch December 12, 2025 05:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants