Skip to content

Conversation

@rubenhoenle
Copy link
Member

Description

relates to STACKITCLI-180


Recently all the factories for the subcommands of our CLI accepted a single parameter: a printer. I had the problem that I additionally need to pass the STACKIT CLI version number into all the commands. So I decided to make this refactoring which will allow us to add possibly required additional parameters way easier in the future without having to touch the whole codebase. The parameter struct is passed as a pointer to reduce memory consumption and copy operations.

- func NewCmd(p *print.Printer) *cobra.Command {
+ func NewCmd(params *params.CmdParams) *cobra.Command {  // <--- // params struct gets passed here instead of just a printer
	cmd := &cobra.Command{
		Use:   "ske",
		Short: "Provides functionality for SKE",
		Long:  "Provides functionality for STACKIT Kubernetes Engine (SKE).",
		Args:  args.NoArgs,
		Run:   utils.CmdHelp,
	}
-       addSubcommands(cmd, p)
+	addSubcommands(cmd, params)  // <--- // params struct will also get passed to all subcommands instead of just the printer
	return cmd
}

And this is what the new parameter struct looks like (see internal/cmd/params/cmd_params.go):

type CmdParams struct {
	Printer    *print.Printer
	CliVersion string
}

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)

@rubenhoenle rubenhoenle self-assigned this May 8, 2025
Copy link
Contributor

@marceljk marceljk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Only the contribution guide needs to be adjusted

func NewCmd(p *print.Printer) *cobra.Command {

@rubenhoenle rubenhoenle force-pushed the chore/refactor-cmd-params branch 2 times, most recently from 69be4e9 to eb22d42 Compare May 9, 2025 13:07
@rubenhoenle rubenhoenle force-pushed the chore/refactor-cmd-params branch from eb22d42 to a32d60e Compare May 9, 2025 13:20
@rubenhoenle rubenhoenle enabled auto-merge (squash) May 9, 2025 13:25
@rubenhoenle rubenhoenle merged commit 8622b5f into main May 9, 2025
3 checks passed
@rubenhoenle rubenhoenle deleted the chore/refactor-cmd-params branch May 9, 2025 13:25
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.

3 participants