mirror of
https://github.com/hanzoai/o11y-foundry.git
synced 2026-11-03 02:08:34 +00:00
## Summary Reshape foundryctl's output contract so it composes with agents and shell pipelines. Three changes that fall out of the same observation: **stdout, stderr, and exit codes should be a stable contract — today they're conflated.** - **Logger to stderr, silent by default.** `instrumentation.NewLogger` writes to `os.Stderr` (was `os.Stdout`) and uses `slog.DiscardHandler` unless `--debug` is set. Matches helm/aws CLI convention. stdout is now reserved for command results. - **Typed exit codes.** `internal/errors.typ` carries a `code int` field; `errors.ExitCode(err)` walks the wrap chain via `errors.As` and returns a deterministic code: `InvalidInput=2, NotFound=3, Unsupported=4, Internal=5, Fatal=6`, untyped=1, nil=0. Compact 1-6 scheme rather than sysexits.h because sysexits would collapse `Internal` and `Fatal` into the same `EX_SOFTWARE (70)`, losing a distinction we care about (Fatal = recovered panic; Internal = expected-but-failed path). `cmd/foundryctl/main.go` now exits with `errors.ExitCode(err)`. - **`fmt.Errorf` forbidden by lint.** New forbidigo rules in `.golangci.yml` ban `fmt.Errorf` and `fmt.Print*` / `print` / `println`. 168 call sites across 38 files converted to `errors.Newf` / `errors.Wrapf` with contextually-chosen types. Narrow exception for `internal/errors/error_test.go` so the ExitCode wrap-chain test stays authentic. Sites of note for review: - `internal/errors/type.go` — exit code mapping table (one source of truth) - `internal/errors/error.go` — `ExitCode(err)` helper - `internal/instrumentation/logger.go` — `slog.DiscardHandler` default - `.golangci.yml` — forbidigo config + the single narrow test exclusion ## Test plan - [x] `go build ./...` - [x] `go test ./...` (all packages pass; new `TestExitCode` covers 9 cases: nil, untyped, each typed, fmt.Errorf-wrapped, foundry-Wrapf-wrapped) - [x] `golangci-lint run ./...` — 0 issues - [x] Manual: `foundryctl forge` on a missing file exits non-zero and emits nothing on stdout (logs hidden); `--debug` emits JSON on stderr only - [x] Manual: end-to-end forge + cast against the docker/compose flavor still produces a healthy stack
78 lines
2.2 KiB
Go
78 lines
2.2 KiB
Go
package jsonpatch
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
|
|
jsonpatchv5 "github.com/evanphx/json-patch/v5"
|
|
"github.com/signoz/foundry/api/v1alpha1"
|
|
"github.com/signoz/foundry/internal/domain"
|
|
"github.com/signoz/foundry/internal/errors"
|
|
"github.com/signoz/foundry/internal/patch"
|
|
)
|
|
|
|
var _ patch.Patch = (*jsonPatch)(nil)
|
|
|
|
type jsonPatch struct{}
|
|
|
|
func New() patch.Patch {
|
|
return &jsonPatch{}
|
|
}
|
|
|
|
func (p *jsonPatch) Apply(ctx context.Context, materials []domain.Material, pe v1alpha1.PatchEntry) ([]domain.Material, error) {
|
|
patchDoc, err := json.Marshal(pe.Operations)
|
|
if err != nil {
|
|
return nil, errors.Wrapf(err, errors.TypeInternal, "failed to marshal patch operations for target %q", pe.Target)
|
|
}
|
|
|
|
result := make([]domain.Material, len(materials))
|
|
copy(result, materials)
|
|
|
|
matched := false
|
|
for i, mat := range result {
|
|
ok, err := patch.MatchTarget(pe.Target, mat.Path())
|
|
if err != nil {
|
|
return nil, errors.Wrapf(err, errors.TypeInvalidInput, "invalid glob pattern %q", pe.Target)
|
|
}
|
|
if !ok {
|
|
continue
|
|
}
|
|
|
|
structured, ok := mat.(domain.StructuredMaterial)
|
|
if !ok {
|
|
return nil, errors.Newf(errors.TypeUnsupported, "json patch on blob material %q is not supported", mat.Path())
|
|
}
|
|
|
|
if structured.HasMultipleDocuments() {
|
|
return nil, errors.Newf(errors.TypeUnsupported, "json patch on multi-doc yaml material %q is not supported", mat.Path())
|
|
}
|
|
|
|
matched = true
|
|
patched, err := applyToMaterial(structured, patchDoc)
|
|
if err != nil {
|
|
return nil, errors.Wrapf(err, errors.TypeInternal, "failed to apply patch to %q", mat.Path())
|
|
}
|
|
result[i] = patched
|
|
}
|
|
|
|
if !matched {
|
|
return nil, errors.Newf(errors.TypeNotFound, "patch target %q did not match any generated material", pe.Target)
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func applyToMaterial(mat domain.StructuredMaterial, patchDoc []byte) (domain.StructuredMaterial, error) {
|
|
decoded, err := jsonpatchv5.DecodePatch(patchDoc)
|
|
if err != nil {
|
|
return nil, errors.Wrapf(err, errors.TypeInvalidInput, "failed to decode json patch")
|
|
}
|
|
|
|
patched, err := decoded.Apply(mat.JSONContents())
|
|
if err != nil {
|
|
return nil, errors.Wrapf(err, errors.TypeInternal, "failed to apply json patch")
|
|
}
|
|
|
|
return mat.CloneWithJSONContents(patched), nil
|
|
}
|