Skip to content

Commit

Permalink
BUG: fix error with affected stack uploads (#691)
Browse files Browse the repository at this point in the history
* make some logging fixes

* add fix for upload
  • Loading branch information
mcalhoun authored Sep 4, 2024
1 parent b52762e commit 0f0fef3
Show file tree
Hide file tree
Showing 9 changed files with 295 additions and 16 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ require (
github.com/containerd/log v0.1.0 // indirect
github.com/containerd/platforms v0.2.1 // indirect
github.com/containerd/stargz-snapshotter/estargz v0.14.3 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dlclark/regexp2 v1.4.0 // indirect
Expand Down Expand Up @@ -192,6 +193,7 @@ require (
github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/rs/zerolog v1.26.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/ryanuber/go-glob v1.0.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
Expand Down
264 changes: 264 additions & 0 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion internal/exec/describe_affected.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func ExecuteDescribeAffectedCmd(cmd *cobra.Command, args []string) error {
}
}

a.Logger.Trace(fmt.Sprintf("\nAffected components and stacks: \n"))
a.Logger.Trace("\nAffected components and stacks: \n")

err = printOrWriteToFile(a.Format, a.OutputFile, affected)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/exec/describe_affected_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func ExecuteDescribeAffectedWithTargetRefCheckout(
return nil, nil, nil, "", err
}

return affected, localRepoHead, remoteRepoHead, localRepoInfo.LocalRepoPath, nil
return affected, localRepoHead, remoteRepoHead, localRepoInfo.RepoUrl, nil
}

// ExecuteDescribeAffectedWithTargetRepoPath uses `repo-path` to access the target repo, and processes stack configs
Expand Down
2 changes: 1 addition & 1 deletion pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (l *Logger) Info(message string) {
}
}

func (l *Logger) Warning(cliConfig schema.CliConfiguration, message string) {
func (l *Logger) Warning(message string) {
if l.LogLevel == LogLevelTrace ||
l.LogLevel == LogLevelDebug ||
l.LogLevel == LogLevelInfo ||
Expand Down
2 changes: 1 addition & 1 deletion pkg/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func TestLogger_Warning(t *testing.T) {
logger, _ := NewLogger(LogLevelWarning, "/dev/stdout")

output := captureOutput(func() {
logger.Warning(schema.CliConfiguration{}, "Warning message")
logger.Warning("Warning message")
})

assert.Contains(t, output, "Warning message")
Expand Down
17 changes: 9 additions & 8 deletions pkg/pro/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

cfg "github.com/cloudposse/atmos/pkg/config"
"github.com/cloudposse/atmos/pkg/logger"
"github.com/cloudposse/atmos/pkg/utils"
)

// AtmosProAPIClient represents the client to interact with the AtmosPro API
Expand Down Expand Up @@ -69,12 +70,12 @@ func getAuthenticatedRequest(c *AtmosProAPIClient, method, url string, body io.R
func (c *AtmosProAPIClient) UploadAffectedStacks(dto AffectedStacksUploadRequest) error {
url := fmt.Sprintf("%s/%s/affected-stacks", c.BaseURL, c.BaseAPIEndpoint)

data, err := json.Marshal(dto)
data, err := utils.ConvertToJSON(dto)
if err != nil {
return fmt.Errorf("failed to marshal payload: %w", err)
}

req, err := getAuthenticatedRequest(c, "POST", url, bytes.NewBuffer(data))
req, err := getAuthenticatedRequest(c, "POST", url, bytes.NewBuffer([]byte(data)))
if err != nil {
return fmt.Errorf("failed to create authenticated request: %w", err)
}
Expand Down Expand Up @@ -118,7 +119,7 @@ func (c *AtmosProAPIClient) LockStack(dto LockStackRequest) (LockStackResponse,

body, err := io.ReadAll(resp.Body)
if err != nil {
return LockStackResponse{}, fmt.Errorf("Error reading response body: %s", err)
return LockStackResponse{}, fmt.Errorf("error reading response body: %s", err)
}

// Create an instance of the struct
Expand All @@ -127,7 +128,7 @@ func (c *AtmosProAPIClient) LockStack(dto LockStackRequest) (LockStackResponse,
// Unmarshal the JSON response into the struct
err = json.Unmarshal(body, &responseData)
if err != nil {
return LockStackResponse{}, fmt.Errorf("Error unmarshaling JSON: %s", err)
return LockStackResponse{}, fmt.Errorf("error unmarshaling JSON: %s", err)
}

if !responseData.Success {
Expand All @@ -136,7 +137,7 @@ func (c *AtmosProAPIClient) LockStack(dto LockStackRequest) (LockStackResponse,
context += fmt.Sprintf(" %s: %v\n", key, value)
}

return LockStackResponse{}, fmt.Errorf("An error occurred while attempting to lock stack.\n\nError: %s\nContext:\n%s", responseData.ErrorMessage, context)
return LockStackResponse{}, fmt.Errorf("an error occurred while attempting to lock stack.\n\nError: %s\nContext:\n%s", responseData.ErrorMessage, context)
}

return responseData, nil
Expand Down Expand Up @@ -165,7 +166,7 @@ func (c *AtmosProAPIClient) UnlockStack(dto UnlockStackRequest) (UnlockStackResp

body, err := io.ReadAll(resp.Body)
if err != nil {
return UnlockStackResponse{}, fmt.Errorf("Error reading response body: %s", err)
return UnlockStackResponse{}, fmt.Errorf("error reading response body: %s", err)
}

// Create an instance of the struct
Expand All @@ -175,7 +176,7 @@ func (c *AtmosProAPIClient) UnlockStack(dto UnlockStackRequest) (UnlockStackResp
err = json.Unmarshal(body, &responseData)

if err != nil {
return UnlockStackResponse{}, fmt.Errorf("Error unmarshaling JSON: %s", err)
return UnlockStackResponse{}, fmt.Errorf("error unmarshaling JSON: %s", err)
}

if !responseData.Success {
Expand All @@ -184,7 +185,7 @@ func (c *AtmosProAPIClient) UnlockStack(dto UnlockStackRequest) (UnlockStackResp
context += fmt.Sprintf(" %s: %v\n", key, value)
}

return UnlockStackResponse{}, fmt.Errorf("An error occurred while attempting to lock stack.\n\nError: %s\nContext:\n%s", responseData.ErrorMessage, context)
return UnlockStackResponse{}, fmt.Errorf("an error occurred while attempting to unlock stack.\n\nError: %s\nContext:\n%s", responseData.ErrorMessage, context)
}

return responseData, nil
Expand Down
10 changes: 9 additions & 1 deletion pkg/utils/json_utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"bytes"
"encoding/json"
"os"
"strings"
Expand All @@ -16,7 +17,14 @@ func PrintAsJSON(data any) error {
if err != nil {
return err
}
PrintMessage(j)

var prettyJSON bytes.Buffer
err = json.Indent(&prettyJSON, []byte(j), "", " ")
if err != nil {
return err
}

PrintMessage(prettyJSON.String())
return nil
}

Expand Down
10 changes: 7 additions & 3 deletions pkg/utils/log_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"os/exec"
"runtime/debug"

"github.com/fatih/color"

Expand Down Expand Up @@ -48,13 +49,16 @@ func LogErrorAndExit(err error) {
func LogError(err error) {
if err != nil {
c := color.New(color.FgRed)
_, err2 := c.Fprintln(color.Error, err.Error()+"\n")
if err2 != nil {
_, printErr := c.Fprintln(color.Error, err.Error()+"\n")
if printErr != nil {
color.Red("Error logging the error:")
color.Red("%s\n", err2)
color.Red("%s\n", printErr)
color.Red("Original error:")
color.Red("%s\n", err)
}

// Print stack trace
debug.PrintStack()
}
}

Expand Down

0 comments on commit 0f0fef3

Please sign in to comment.