Files
PandeyandGitHub 09b16ec619 refactor(foundryctl): silent-by-default logs, typed exit codes, ban fmt.Errorf (#118)
## 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
2026-05-17 23:01:00 +05:30

81 lines
3.2 KiB
Go

package railwaytemplatecasting
import (
"context"
"github.com/signoz/foundry/api/v1alpha1"
"github.com/signoz/foundry/api/v1alpha1/installation"
"github.com/signoz/foundry/internal/domain"
"github.com/signoz/foundry/internal/errors"
"github.com/signoz/foundry/internal/molding"
)
var _ molding.MoldingEnricher = (*railwayTemplateMoldingEnricher)(nil)
type railwayTemplateMoldingEnricher struct {
material []domain.StructuredMaterial
}
func newRailwayTemplateMoldingEnricher(config *installation.Casting) (*railwayTemplateMoldingEnricher, error) {
material, err := getRailwayMaterial(config)
if err != nil {
return nil, errors.Wrapf(err, errors.TypeInternal, "failed to get compose yaml material")
}
return &railwayTemplateMoldingEnricher{material: material}, nil
}
// railwayInternalHost returns the Railway private DNS hostname for a service.
// Railway services communicate via SERVICE_NAME.railway.internal within the same project.
func railwayInternalHost(serviceName string) string {
return serviceName + ".railway.internal"
}
func (enricher *railwayTemplateMoldingEnricher) EnrichStatus(ctx context.Context, kind v1alpha1.MoldingKind, config *installation.Casting) error {
name := config.Metadata.Name
if name == "" {
name = "signoz"
}
switch kind {
case v1alpha1.MoldingKindTelemetryStore:
if !config.Spec.TelemetryStore.Spec.IsEnabled() {
return nil
}
svc := name + "-telemetrystore-" + config.Spec.TelemetryStore.Kind.String()
config.Spec.TelemetryStore.Status.Addresses.TCP = []string{domain.MustNewAddress("tcp", railwayInternalHost(svc), 9000).String()}
if config.Spec.TelemetryStore.Status.Extras == nil {
config.Spec.TelemetryStore.Status.Extras = make(map[string]string)
}
config.Spec.TelemetryStore.Status.Extras["service_names"] = svc
config.Spec.TelemetryStore.Status.Extras["_overrides"] = string(enricher.material[1].FmtContents())
case v1alpha1.MoldingKindSignoz:
if !config.Spec.Signoz.Spec.IsEnabled() {
return nil
}
svc := name + "-signoz"
config.Spec.Signoz.Status.Addresses.APIServer = []string{domain.MustNewAddress("tcp", railwayInternalHost(svc), 8080).String()}
config.Spec.Signoz.Status.Addresses.Opamp = []string{domain.MustNewAddress("ws", railwayInternalHost(svc), 4320).String()}
case v1alpha1.MoldingKindTelemetryKeeper:
if !config.Spec.TelemetryKeeper.Spec.IsEnabled() {
return nil
}
svc := name + "-telemetrykeeper-" + config.Spec.TelemetryKeeper.Kind.String()
config.Spec.TelemetryKeeper.Status.Addresses.Client = []string{domain.MustNewAddress("tcp", railwayInternalHost(svc), 9181).String()}
config.Spec.TelemetryKeeper.Status.Addresses.Raft = []string{domain.MustNewAddress("tcp", railwayInternalHost(svc), 9234).String()}
if config.Spec.TelemetryKeeper.Status.Extras == nil {
config.Spec.TelemetryKeeper.Status.Extras = make(map[string]string)
}
config.Spec.TelemetryKeeper.Status.Extras["service_names"] = svc
config.Spec.TelemetryKeeper.Status.Extras["_overrides"] = string(enricher.material[0].FmtContents())
case v1alpha1.MoldingKindIngester:
if !config.Spec.Ingester.Spec.IsEnabled() {
return nil
}
svc := name + "-ingester"
config.Spec.Ingester.Status.Addresses.OTLP = []string{domain.MustNewAddress("tcp", railwayInternalHost(svc), 4318).String()}
}
return nil
}