Skip to content

refac(io) set IO once in main to allow overriding with in memory io i…#1363

Open
cgoetz-inovex wants to merge 2 commits intomainfrom
feat/STACKITCLI-318_secret-flags
Open

refac(io) set IO once in main to allow overriding with in memory io i…#1363
cgoetz-inovex wants to merge 2 commits intomainfrom
feat/STACKITCLI-318_secret-flags

Conversation

@cgoetz-inovex
Copy link
Copy Markdown
Contributor

…n tests

print.Printer had a reference to a cobra.Command for using its IO streams. Each Command also used a Printer, resulting in an awkward circular dependency.
Refactored Printer to use IO streams directly. When using the application these are set in main, when used in tests these can be set to bytes.Buffers.
Also replaced usages of os.Args with just a string slice. Also set in main.
cobra.Commands Args and IO-streams are set in NewRootCmd with traverseCommands.
CmdParams also has an fs.FS, currently unused but will allow using the real FS during regular use and an in-memory-FS during tests.

This change prepares the application for integrative testing while keeping good isolation. Generally speaking the goal is to move all things with side effects into main (compare with https://grafana.com/blog/how-i-write-http-services-in-go-after-13-years/#func-main-only-calls-run)

Description

relates to #1234

Checklist

  • Issue was linked above
  • Code format was applied: make fmt
  • Examples were added / adjusted (see e.g. here)
  • Docs are up-to-date: make generate-docs (will be checked by CI)
  • Unit tests got implemented or updated
  • Unit tests are passing: make test (will be checked by CI)
  • No linter issues: make lint (will be checked by CI)

…n tests

`print.Printer` had a reference to a `cobra.Command` for using its IO
streams. Each Command also used a Printer, resulting in an awkward
circular dependency.
Refactored Printer to use IO streams directly. When using the application
these are set in `main`, when used in tests these can be set to
`bytes.Buffer`s.
Also replaced usages of `os.Args` with just a string slice. Also set
in `main`.
`cobra.Command`s `Args` and IO-streams are set in `NewRootCmd` with
`traverseCommands`.
`CmdParams` also has an `fs.FS`, currently unused but will allow using
the real FS during regular use and an in-memory-FS during tests.

This change prepares the application for integrative testing while keeping
good isolation. Generally speaking the goal is to move all things with
side effects into main (compare with https://grafana.com/blog/how-i-write-http-services-in-go-after-13-years/#func-main-only-calls-run)
@cgoetz-inovex cgoetz-inovex requested a review from a team as a code owner April 9, 2026 15:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant