Skip to content

services: collapse duplicated client and process-manager code #3116

Open

Description

@mikeland73

internal/services/client.go and manager.go contain copy-pasted functions, unused parameters and dead state. About 100 lines can go, and doing this fixes #3108 as a side effect.

client.go

  • StartServices, StopServices and RestartServices differ only in verb and HTTP method. Collapse them into one helper.
  • Use http.NewRequestWithContext in clientRequest, so ctx is finally used, and use io.ReadAll instead of a bytes.Buffer.
  • Remove the unused w from ListServices.
  • Make the argument order consistent: w is first in some functions and last in others.

manager.go

  • Shared config preamble. "Open, lock, read, defer close" of the global process-compose config appears 6 times. Extract withGlobalConfig(func(cfg, file) error) error.
  • Dead state. globalProcessComposeConfig.Path is written and never read. Pass *os.File explicitly instead of keeping config.File.
  • Start path.
    • StartProcessManager builds exec.Command twice.
    • The foreground and background paths both build and write instance{Pid, Port}. Do that once.
  • Slice aliasing. append(requestedServices, flags...) can alias the caller's slice. Build args := append([]string{"up"}, requested...) instead.
  • Unused parameters.
    • ctx is unused in StopProcessManager, StopAllProcessManagers and AttachToProcessManager.
    • w is unused in StopAllProcessManagers and AttachToProcessManager.
  • Write the instance file inline. In StopProcessManager, write it after signalling instead of in a defer. This fixes StopProcessManager drops the deferred write error #3108.
  • ports.go: the get closure in getAvailablePort adds nothing.

Risk: low to medium, because it touches lock ordering around the global config file.


From specs/code-improvements.md (#3060), items C7, C8, A17 and E1.

Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions