From fe2cca859fd6f48563b2d4cb4b445fb9f70c8feb Mon Sep 17 00:00:00 2001 From: Matt Calhoun Date: Wed, 4 Sep 2024 14:22:50 -0400 Subject: [PATCH] add fix for upload --- internal/exec/describe_affected.go | 2 +- internal/exec/describe_affected_utils.go | 2 +- pkg/pro/api_client.go | 17 +++++++++-------- pkg/utils/json_utils.go | 10 +++++++++- pkg/utils/log_utils.go | 8 +------- 5 files changed, 21 insertions(+), 18 deletions(-) diff --git a/internal/exec/describe_affected.go b/internal/exec/describe_affected.go index 4e3f1a6bc..1205cfdab 100644 --- a/internal/exec/describe_affected.go +++ b/internal/exec/describe_affected.go @@ -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 { diff --git a/internal/exec/describe_affected_utils.go b/internal/exec/describe_affected_utils.go index 218120b66..ad1561098 100644 --- a/internal/exec/describe_affected_utils.go +++ b/internal/exec/describe_affected_utils.go @@ -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 diff --git a/pkg/pro/api_client.go b/pkg/pro/api_client.go index ee6afee31..283e7efbe 100644 --- a/pkg/pro/api_client.go +++ b/pkg/pro/api_client.go @@ -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 @@ -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) } @@ -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 @@ -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 { @@ -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 @@ -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 @@ -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 { @@ -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 diff --git a/pkg/utils/json_utils.go b/pkg/utils/json_utils.go index 52e74742d..f509a4e62 100644 --- a/pkg/utils/json_utils.go +++ b/pkg/utils/json_utils.go @@ -1,6 +1,7 @@ package utils import ( + "bytes" "encoding/json" "os" "strings" @@ -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 } diff --git a/pkg/utils/log_utils.go b/pkg/utils/log_utils.go index 6310c6863..e0014f83d 100644 --- a/pkg/utils/log_utils.go +++ b/pkg/utils/log_utils.go @@ -58,13 +58,7 @@ func LogError(err error) { } // Print stack trace - stackTrace := debug.Stack() - _, stackErr := c.Fprintln(color.Error, "Stack trace:\n"+string(stackTrace)) - if stackErr != nil { - color.Red("Error printing stack trace:") - color.Red("%s\n", stackErr) - } - + debug.PrintStack() } }