Skip to content
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
5 changes: 5 additions & 0 deletions internal/model/update.go
Original file line numberDiff line numberDiff line change
Expand Up@@ -58,3 +58,8 @@ type RecordUpdateJobError struct {
ErrorType string `json:"error-type" yaml:"error-type"`
ErrorDetails map[string]any `json:"error-details" yaml:"error-details"`
}

type IncrementMetric struct {
Metric string `json:"metric" yaml:"metric"`
Tags map[string]any `json:"tags" yaml:"tags"`
}
32 changes: 22 additions & 10 deletions internal/server/api.go
Original file line numberDiff line numberDiff line change
Expand Up@@ -118,22 +118,19 @@ func (a *API) ServeHTTP(_ http.ResponseWriter, r *http.Request) {
a.pushError(err)
}

if kind == "increment_metric" {
// Let's just output the metrics data and stop
a.outputRequestData(kind, actual)
return
}

if err := a.pushResult(kind, actual); err != nil {
Comment thread
jakecoffman marked this conversation as resolved.
a.pushError(err)
return
}

if !a.hasExpectations {
if a.writer != nil {
// output the data received to stdout
if err = json.NewEncoder(a.writer).Encode(map[string]any{
"type": kind,
"data": actual.Data,
}); err != nil {
// Fail so the user knows stdout is not working
log.Panicln("Failed to write to stdout: ", err)
}
}
a.outputRequestData(kind, actual)
return
}

Expand DownExpand Up@@ -167,6 +164,19 @@ func (a *API) assertExpectation(kind string, actual *model.UpdateWrapper) {
}
}

func (a *API) outputRequestData(kind string, actual *model.UpdateWrapper) {
if a.writer != nil {
// output the data received to stdout
if err := json.NewEncoder(a.writer).Encode(map[string]any{
"type": kind,
"data": actual.Data,
}); err != nil {
// Fail so the user knows stdout is not working
log.Panicln("Failed to write to stdout: ", err)
}
}
}

func (a *API) pushError(err error) {
escapedError := strings.ReplaceAll(err.Error(), "\n", "")
escapedError = strings.ReplaceAll(escapedError, "\r", "")
Expand DownExpand Up@@ -213,6 +223,8 @@ func decodeWrapper(kind string, data []byte) (actual *model.UpdateWrapper, err e
actual.Data, err = decode[model.RecordPackageManagerVersion](data)
case "record_update_job_error":
actual.Data, err = decode[model.RecordUpdateJobError](data)
case "increment_metric":
actual.Data, err = decode[model.IncrementMetric](data)
default:
return nil, fmt.Errorf("unexpected output type: %s", kind)
}
Expand Down