Skip to content

Replace os.Exit in binoculars library with error return #4779

@dejanzele

Description

@dejanzele

Is your feature request related to a problem? Please describe.

StartUp() in internal/binoculars/server.go calls os.Exit(-1) on initialization errors (lines 28 and 34) instead of returning an error. This means you can't unit test the function (the test process would just die), and it prevents the caller from doing any cleanup.

// Current code (internal/binoculars/server.go)
func StartUp(config *configuration.BinocularsConfig) (func(), *sync.WaitGroup) {
    // ...
    kubernetesClientProvider, err := cluster.NewKubernetesClientProvider(...)
    if err != nil {
        log.Errorf("Failed to connect to kubernetes because %s", err)
        os.Exit(-1)
    }

    authServices, err := auth.ConfigureAuth(config.Auth)
    if err != nil {
        log.Errorf("Failed to create auth services %s", err)
        os.Exit(-1)
    }
    // ...
}

In Go, library code (anything under internal/) should return errors and let the caller in cmd/ decide what to do. Only main() should call os.Exit or log.Fatal.

Describe the solution you'd like

  1. Change StartUp() to return an error: func StartUp(config *configuration.BinocularsConfig) (func(), *sync.WaitGroup, error)
  2. Replace the os.Exit(-1) calls with wrapped error returns
  3. Update the caller in cmd/binoculars/main.go:74 to handle the error (log.Fatal is fine there)

Files to change

  • internal/binoculars/server.go - change signature, return errors
  • cmd/binoculars/main.go - handle the new error return

There's only one caller, so this is a small change.

Acceptance criteria

  • No os.Exit in internal/binoculars/server.go
  • StartUp() returns error as its last return value
  • cmd/binoculars/main.go handles the error
  • go build ./cmd/binoculars/... succeeds
  • golangci-lint run ./internal/binoculars/... ./cmd/binoculars/... passes

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions