Integrate options factory into commands

Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
Hubertbits 2025-04-16 15:46:25 +02:00 committed by yxxhero
parent 35bb7dca97
commit e6e269429d
8 changed files with 276 additions and 119 deletions

View File

@ -5,22 +5,23 @@ import (
"github.com/helmfile/helmfile/pkg/app"
"github.com/helmfile/helmfile/pkg/config"
"github.com/helmfile/helmfile/pkg/flags"
"github.com/helmfile/helmfile/pkg/factory"
)
// NewApplyCmd returns apply subcmd
func NewApplyCmd(globalCfg *config.GlobalImpl) *cobra.Command {
applyOptions := config.NewApplyOptions()
flagRegistrar := flags.NewApplyFlagRegistrar()
optionsFactory := factory.NewApplyOptionsFactory()
options := optionsFactory.CreateOptions().(*config.ApplyOptions)
flagRegistry := optionsFactory.GetFlagRegistry()
cmd := &cobra.Command{
Use: "apply",
Short: "Apply all resources from state file only when there are changes",
PreRun: func(cmd *cobra.Command, args []string) {
flagRegistrar.TransferFlags(cmd, applyOptions)
flagRegistry.TransferFlags(cmd, options)
},
RunE: func(cmd *cobra.Command, args []string) error {
applyImpl := config.NewApplyImpl(globalCfg, applyOptions)
applyImpl := config.NewApplyImpl(globalCfg, options)
err := config.NewCLIConfigImpl(applyImpl.GlobalImpl)
if err != nil {
@ -37,43 +38,43 @@ func NewApplyCmd(globalCfg *config.GlobalImpl) *cobra.Command {
}
f := cmd.Flags()
f.StringArrayVar(&applyOptions.Set, "set", nil, "additional values to be merged into the helm command --set flag")
f.StringArrayVar(&applyOptions.Values, "values", nil, "additional value files to be merged into the helm command --values flag")
f.IntVar(&applyOptions.Concurrency, "concurrency", 0, "maximum number of concurrent helm processes to run, 0 is unlimited")
f.BoolVar(&applyOptions.Validate, "validate", false, "validate your manifests against the Kubernetes cluster you are currently pointing at. Note that this requires access to a Kubernetes cluster to obtain information necessary for validating, like the list of available API versions")
f.IntVar(&applyOptions.Context, "context", 0, "output NUM lines of context around changes")
f.StringVar(&applyOptions.Output, "output", "", "output format for diff plugin")
f.BoolVar(&applyOptions.DetailedExitcode, "detailed-exitcode", false, "return a non-zero exit code 2 instead of 0 when there were changes detected AND the changes are synced successfully")
f.BoolVar(&applyOptions.StripTrailingCR, "strip-trailing-cr", false, "strip trailing carriage return on input")
f.StringVar(&applyOptions.DiffArgs, "diff-args", "", `pass args to helm helm-diff`)
f.StringVar(&applyOptions.SyncArgs, "sync-args", "", `pass args to helm upgrade`)
f.StringArrayVar(&options.Set, "set", nil, "additional values to be merged into the helm command --set flag")
f.StringArrayVar(&options.Values, "values", nil, "additional value files to be merged into the helm command --values flag")
f.IntVar(&options.Concurrency, "concurrency", 0, "maximum number of concurrent helm processes to run, 0 is unlimited")
f.BoolVar(&options.Validate, "validate", false, "validate your manifests against the Kubernetes cluster you are currently pointing at. Note that this requires access to a Kubernetes cluster to obtain information necessary for validating, like the list of available API versions")
f.IntVar(&options.Context, "context", 0, "output NUM lines of context around changes")
f.StringVar(&options.Output, "output", "", "output format for diff plugin")
f.BoolVar(&options.DetailedExitcode, "detailed-exitcode", false, "return a non-zero exit code 2 instead of 0 when there were changes detected AND the changes are synced successfully")
f.BoolVar(&options.StripTrailingCR, "strip-trailing-cr", false, "strip trailing carriage return on input")
f.StringVar(&options.DiffArgs, "diff-args", "", `pass args to helm helm-diff`)
f.StringVar(&options.SyncArgs, "sync-args", "", `pass args to helm upgrade`)
f.StringVar(&globalCfg.GlobalOptions.Args, "args", "", "pass args to helm exec")
f.BoolVar(&applyOptions.SkipCleanup, "skip-cleanup", false, "Stop cleaning up temporary values generated by helmfile and helm-secrets. Useful for debugging. Don't use in production for security")
f.BoolVar(&applyOptions.SkipNeeds, "skip-needs", true, `do not automatically include releases from the target release's "needs" when --selector/-l flag is provided. Does nothing when --selector/-l flag is not provided. Defaults to true when --include-needs or --include-transitive-needs is not provided`)
f.BoolVar(&applyOptions.IncludeNeeds, "include-needs", false, `automatically include releases from the target release's "needs" when --selector/-l flag is provided. Does nothing when --selector/-l flag is not provided`)
f.BoolVar(&applyOptions.IncludeTransitiveNeeds, "include-transitive-needs", false, `like --include-needs, but also includes transitive needs (needs of needs). Does nothing when --selector/-l flag is not provided. Overrides exclusions of other selectors and conditions.`)
f.BoolVar(&applyOptions.SkipDiffOnInstall, "skip-diff-on-install", false, "Skips running helm-diff on releases being newly installed on this apply. Useful when the release manifests are too huge to be reviewed, or it's too time-consuming to diff at all")
f.BoolVar(&applyOptions.IncludeTests, "include-tests", false, "enable the diffing of the helm test hooks")
f.StringArrayVar(&applyOptions.Suppress, "suppress", nil, "suppress specified Kubernetes objects in the diff output. Can be provided multiple times. For example: --suppress KeycloakClient --suppress VaultSecret")
f.BoolVar(&applyOptions.SuppressSecrets, "suppress-secrets", false, "suppress secrets in the diff output. highly recommended to specify on CI/CD use-cases")
f.BoolVar(&applyOptions.ShowSecrets, "show-secrets", false, "do not redact secret values in the diff output. should be used for debug purpose only")
f.BoolVar(&applyOptions.NoHooks, "no-hooks", false, "do not diff changes made by hooks.")
f.BoolVar(&applyOptions.HideNotes, "hide-notes", false, "add --hide-notes flag to helm")
f.BoolVar(&applyOptions.TakeOwnership, "take-ownership", false, "add --take-ownership flag to helm")
f.BoolVar(&applyOptions.SyncReleaseLabels, "sync-release-labels", false, "sync release labels to the target release")
f.BoolVar(&applyOptions.SuppressDiff, "suppress-diff", false, "suppress diff in the output. Usable in new installs")
f.BoolVar(&applyOptions.Wait, "wait", false, `Override helmDefaults.wait setting "helm upgrade --install --wait"`)
f.BoolVar(&applyOptions.WaitForJobs, "wait-for-jobs", false, `Override helmDefaults.waitForJobs setting "helm upgrade --install --wait-for-jobs"`)
f.BoolVar(&applyOptions.ReuseValues, "reuse-values", false, `Override helmDefaults.reuseValues "helm upgrade --install --reuse-values"`)
f.BoolVar(&applyOptions.ResetValues, "reset-values", false, `Override helmDefaults.reuseValues "helm upgrade --install --reset-values"`)
f.StringVar(&applyOptions.PostRenderer, "post-renderer", "", `pass --post-renderer to "helm template" or "helm upgrade --install"`)
f.StringArrayVar(&applyOptions.PostRendererArgs, "post-renderer-args", nil, `pass --post-renderer-args to "helm template" or "helm upgrade --install"`)
f.BoolVar(&applyOptions.SkipSchemaValidation, "skip-schema-validation", false, `pass --skip-schema-validation to "helm template" or "helm upgrade --install"`)
f.StringVar(&applyOptions.Cascade, "cascade", "", "pass cascade to helm exec, default: background")
f.StringArrayVar(&applyOptions.SuppressOutputLineRegex, "suppress-output-line-regex", nil, "a list of regex patterns to suppress output lines from the diff output")
f.BoolVar(&options.SkipCleanup, "skip-cleanup", false, "Stop cleaning up temporary values generated by helmfile and helm-secrets. Useful for debugging. Don't use in production for security")
f.BoolVar(&options.SkipNeeds, "skip-needs", true, `do not automatically include releases from the target release's "needs" when --selector/-l flag is provided. Does nothing when --selector/-l flag is not provided. Defaults to true when --include-needs or --include-transitive-needs is not provided`)
f.BoolVar(&options.IncludeNeeds, "include-needs", false, `automatically include releases from the target release's "needs" when --selector/-l flag is provided. Does nothing when --selector/-l flag is not provided`)
f.BoolVar(&options.IncludeTransitiveNeeds, "include-transitive-needs", false, `like --include-needs, but also includes transitive needs (needs of needs). Does nothing when --selector/-l flag is not provided. Overrides exclusions of other selectors and conditions.`)
f.BoolVar(&options.SkipDiffOnInstall, "skip-diff-on-install", false, "Skips running helm-diff on releases being newly installed on this apply. Useful when the release manifests are too huge to be reviewed, or it's too time-consuming to diff at all")
f.BoolVar(&options.IncludeTests, "include-tests", false, "enable the diffing of the helm test hooks")
f.StringArrayVar(&options.Suppress, "suppress", nil, "suppress specified Kubernetes objects in the diff output. Can be provided multiple times. For example: --suppress KeycloakClient --suppress VaultSecret")
f.BoolVar(&options.SuppressSecrets, "suppress-secrets", false, "suppress secrets in the diff output. highly recommended to specify on CI/CD use-cases")
f.BoolVar(&options.ShowSecrets, "show-secrets", false, "do not redact secret values in the diff output. should be used for debug purpose only")
f.BoolVar(&options.NoHooks, "no-hooks", false, "do not diff changes made by hooks.")
f.BoolVar(&options.HideNotes, "hide-notes", false, "add --hide-notes flag to helm")
f.BoolVar(&options.TakeOwnership, "take-ownership", false, "add --take-ownership flag to helm")
f.BoolVar(&options.SyncReleaseLabels, "sync-release-labels", false, "sync release labels to the target release")
f.BoolVar(&options.SuppressDiff, "suppress-diff", false, "suppress diff in the output. Usable in new installs")
f.BoolVar(&options.Wait, "wait", false, `Override helmDefaults.wait setting "helm upgrade --install --wait"`)
f.BoolVar(&options.WaitForJobs, "wait-for-jobs", false, `Override helmDefaults.waitForJobs setting "helm upgrade --install --wait-for-jobs"`)
f.BoolVar(&options.ReuseValues, "reuse-values", false, `Override helmDefaults.reuseValues "helm upgrade --install --reuse-values"`)
f.BoolVar(&options.ResetValues, "reset-values", false, `Override helmDefaults.reuseValues "helm upgrade --install --reset-values"`)
f.StringVar(&options.PostRenderer, "post-renderer", "", `pass --post-renderer to "helm template" or "helm upgrade --install"`)
f.StringArrayVar(&options.PostRendererArgs, "post-renderer-args", nil, `pass --post-renderer-args to "helm template" or "helm upgrade --install"`)
f.BoolVar(&options.SkipSchemaValidation, "skip-schema-validation", false, `pass --skip-schema-validation to "helm template" or "helm upgrade --install"`)
f.StringVar(&options.Cascade, "cascade", "", "pass cascade to helm exec, default: background")
f.StringArrayVar(&options.SuppressOutputLineRegex, "suppress-output-line-regex", nil, "a list of regex patterns to suppress output lines from the diff output")
// Register flags using the registrar
flagRegistrar.RegisterFlags(cmd)
// Register flags using the registry
flagRegistry.RegisterFlags(cmd)
return cmd
}

38
cmd/apply_test.go Normal file
View File

@ -0,0 +1,38 @@
// cmd/diff_test.go
package cmd
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/helmfile/helmfile/pkg/config"
"github.com/helmfile/helmfile/pkg/flags"
"github.com/helmfile/helmfile/pkg/testcmd"
)
func TestNewApplyCmd(t *testing.T) {
// Test the actual command properties
globalCfg := config.NewGlobalImpl(&config.GlobalOptions{HelmBinary: "helm"})
cmd := NewApplyCmd(globalCfg)
assert.Equal(t, "apply", cmd.Use)
// Use the test helper for testing flags
helper := testcmd.TestApplyCmd()
assert.Equal(t, helper.Cmd.Use, cmd.Use)
// Get the names of registered flags
registeredFlags := helper.Registry.GetRegisteredFlagNames()
// Verify flags and values
assert.Contains(t, registeredFlags, "include-crds")
assert.Contains(t, registeredFlags, "skip-crds")
includeCRDs, exists := flags.GetFlagValue[bool](helper.Registry, "include-crds")
assert.True(t, exists)
assert.False(t, includeCRDs)
skipCRDs, exists := flags.GetFlagValue[bool](helper.Registry, "skip-crds")
assert.True(t, exists)
assert.False(t, skipCRDs)
}

View File

@ -5,22 +5,23 @@ import (
"github.com/helmfile/helmfile/pkg/app"
"github.com/helmfile/helmfile/pkg/config"
"github.com/helmfile/helmfile/pkg/flags"
"github.com/helmfile/helmfile/pkg/factory"
)
// NewDiffCmd returns diff subcmd
func NewDiffCmd(globalCfg *config.GlobalImpl) *cobra.Command {
diffOptions := config.NewDiffOptions()
flagRegistrar := flags.NewDiffFlagRegistrar()
optionsFactory := factory.NewDiffOptionsFactory()
options := optionsFactory.CreateOptions().(*config.DiffOptions)
flagRegistry := optionsFactory.GetFlagRegistry()
cmd := &cobra.Command{
Use: "diff",
Short: "Diff releases defined in state file",
PreRun: func(cmd *cobra.Command, args []string) {
flagRegistrar.TransferFlags(cmd, diffOptions)
flagRegistry.TransferFlags(cmd, options)
},
RunE: func(cmd *cobra.Command, args []string) error {
diffImpl := config.NewDiffImpl(globalCfg, diffOptions)
diffImpl := config.NewDiffImpl(globalCfg, options)
err := config.NewCLIConfigImpl(diffImpl.GlobalImpl)
if err != nil {
return err
@ -36,33 +37,33 @@ func NewDiffCmd(globalCfg *config.GlobalImpl) *cobra.Command {
}
f := cmd.Flags()
f.StringVar(&diffOptions.DiffArgs, "diff-args", "", `pass args to helm helm-diff`)
f.StringVar(&options.DiffArgs, "diff-args", "", `pass args to helm helm-diff`)
f.StringVar(&globalCfg.GlobalOptions.Args, "args", "", "pass args to helm diff")
f.StringArrayVar(&diffOptions.Set, "set", nil, "additional values to be merged into the helm command --set flag")
f.StringArrayVar(&diffOptions.Values, "values", nil, "additional value files to be merged into the helm command --values flag")
f.IntVar(&diffOptions.Concurrency, "concurrency", 0, "maximum number of concurrent helm processes to run, 0 is unlimited")
f.BoolVar(&diffOptions.Validate, "validate", false, "validate your manifests against the Kubernetes cluster you are currently pointing at. Note that this requires access to a Kubernetes cluster to obtain information necessary for validating, like the diff of available API versions")
f.BoolVar(&diffOptions.SkipNeeds, "skip-needs", true, `do not automatically include releases from the target release's "needs" when --selector/-l flag is provided. Does nothing when --selector/-l flag is not provided. Defaults to true when --include-needs or --include-transitive-needs is not provided`)
f.BoolVar(&diffOptions.IncludeTests, "include-tests", false, "enable the diffing of the helm test hooks")
f.BoolVar(&diffOptions.IncludeNeeds, "include-needs", false, `automatically include releases from the target release's "needs" when --selector/-l flag is provided. Does nothing when --selector/-l flag is not provided`)
f.BoolVar(&diffOptions.IncludeTransitiveNeeds, "include-transitive-needs", false, `like --include-needs, but also includes transitive needs (needs of needs). Does nothing when --selector/-l flag is not provided. Overrides exclusions of other selectors and conditions.`)
f.BoolVar(&diffOptions.SkipDiffOnInstall, "skip-diff-on-install", false, "Skips running helm-diff on releases being newly installed on this apply. Useful when the release manifests are too huge to be reviewed, or it's too time-consuming to diff at all")
f.BoolVar(&diffOptions.ShowSecrets, "show-secrets", false, "do not redact secret values in the output. should be used for debug purpose only")
f.BoolVar(&diffOptions.NoHooks, "no-hooks", false, "do not diff changes made by hooks.")
f.BoolVar(&diffOptions.DetailedExitcode, "detailed-exitcode", false, "return a detailed exit code")
f.BoolVar(&diffOptions.StripTrailingCR, "strip-trailing-cr", false, "strip trailing carriage return on input")
f.IntVar(&diffOptions.Context, "context", 0, "output NUM lines of context around changes")
f.StringVar(&diffOptions.Output, "output", "", "output format for diff plugin")
f.BoolVar(&diffOptions.SuppressSecrets, "suppress-secrets", false, "suppress secrets in the output. highly recommended to specify on CI/CD use-cases")
f.StringArrayVar(&diffOptions.Suppress, "suppress", nil, "suppress specified Kubernetes objects in the output. Can be provided multiple times. For example: --suppress KeycloakClient --suppress VaultSecret")
f.BoolVar(&diffOptions.ReuseValues, "reuse-values", false, `Override helmDefaults.reuseValues "helm diff upgrade --install --reuse-values"`)
f.BoolVar(&diffOptions.ResetValues, "reset-values", false, `Override helmDefaults.reuseValues "helm diff upgrade --install --reset-values"`)
f.StringVar(&diffOptions.PostRenderer, "post-renderer", "", `pass --post-renderer to "helm template" or "helm upgrade --install"`)
f.StringArrayVar(&diffOptions.PostRendererArgs, "post-renderer-args", nil, `pass --post-renderer-args to "helm template" or "helm upgrade --install"`)
f.StringArrayVar(&diffOptions.SuppressOutputLineRegex, "suppress-output-line-regex", nil, "a list of regex patterns to suppress output lines from the diff output")
f.StringArrayVar(&options.Set, "set", nil, "additional values to be merged into the helm command --set flag")
f.StringArrayVar(&options.Values, "values", nil, "additional value files to be merged into the helm command --values flag")
f.IntVar(&options.Concurrency, "concurrency", 0, "maximum number of concurrent helm processes to run, 0 is unlimited")
f.BoolVar(&options.Validate, "validate", false, "validate your manifests against the Kubernetes cluster you are currently pointing at. Note that this requires access to a Kubernetes cluster to obtain information necessary for validating, like the diff of available API versions")
f.BoolVar(&options.SkipNeeds, "skip-needs", true, `do not automatically include releases from the target release's "needs" when --selector/-l flag is provided. Does nothing when --selector/-l flag is not provided. Defaults to true when --include-needs or --include-transitive-needs is not provided`)
f.BoolVar(&options.IncludeTests, "include-tests", false, "enable the diffing of the helm test hooks")
f.BoolVar(&options.IncludeNeeds, "include-needs", false, `automatically include releases from the target release's "needs" when --selector/-l flag is provided. Does nothing when --selector/-l flag is not provided`)
f.BoolVar(&options.IncludeTransitiveNeeds, "include-transitive-needs", false, `like --include-needs, but also includes transitive needs (needs of needs). Does nothing when --selector/-l flag is not provided. Overrides exclusions of other selectors and conditions.`)
f.BoolVar(&options.SkipDiffOnInstall, "skip-diff-on-install", false, "Skips running helm-diff on releases being newly installed on this apply. Useful when the release manifests are too huge to be reviewed, or it's too time-consuming to diff at all")
f.BoolVar(&options.ShowSecrets, "show-secrets", false, "do not redact secret values in the output. should be used for debug purpose only")
f.BoolVar(&options.NoHooks, "no-hooks", false, "do not diff changes made by hooks.")
f.BoolVar(&options.DetailedExitcode, "detailed-exitcode", false, "return a detailed exit code")
f.BoolVar(&options.StripTrailingCR, "strip-trailing-cr", false, "strip trailing carriage return on input")
f.IntVar(&options.Context, "context", 0, "output NUM lines of context around changes")
f.StringVar(&options.Output, "output", "", "output format for diff plugin")
f.BoolVar(&options.SuppressSecrets, "suppress-secrets", false, "suppress secrets in the output. highly recommended to specify on CI/CD use-cases")
f.StringArrayVar(&options.Suppress, "suppress", nil, "suppress specified Kubernetes objects in the output. Can be provided multiple times. For example: --suppress KeycloakClient --suppress VaultSecret")
f.BoolVar(&options.ReuseValues, "reuse-values", false, `Override helmDefaults.reuseValues "helm diff upgrade --install --reuse-values"`)
f.BoolVar(&options.ResetValues, "reset-values", false, `Override helmDefaults.reuseValues "helm diff upgrade --install --reset-values"`)
f.StringVar(&options.PostRenderer, "post-renderer", "", `pass --post-renderer to "helm template" or "helm upgrade --install"`)
f.StringArrayVar(&options.PostRendererArgs, "post-renderer-args", nil, `pass --post-renderer-args to "helm template" or "helm upgrade --install"`)
f.StringArrayVar(&options.SuppressOutputLineRegex, "suppress-output-line-regex", nil, "a list of regex patterns to suppress output lines from the diff output")
// Register flags using the registrar
flagRegistrar.RegisterFlags(cmd)
// Register flags using the registry
flagRegistry.RegisterFlags(cmd)
return cmd
}

44
cmd/diff_test.go Normal file
View File

@ -0,0 +1,44 @@
// cmd/diff_test.go
package cmd
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/helmfile/helmfile/pkg/config"
"github.com/helmfile/helmfile/pkg/flags"
"github.com/helmfile/helmfile/pkg/testcmd"
)
func TestNewDiffCmd(t *testing.T) {
// Test the actual command properties
globalCfg := config.NewGlobalImpl(&config.GlobalOptions{HelmBinary: "helm"})
cmd := NewDiffCmd(globalCfg)
assert.Equal(t, "diff", cmd.Use)
// Use the test helper for testing flags
helper := testcmd.TestDiffCmd()
assert.Equal(t, helper.Cmd.Use, cmd.Use)
// Get the names of registered flags
registeredFlags := helper.Registry.GetRegisteredFlagNames()
// Verify flags and values
assert.Contains(t, registeredFlags, "include-crds")
includeCRDs, exists := flags.GetFlagValue[bool](helper.Registry, "include-crds")
assert.True(t, exists)
assert.False(t, includeCRDs)
// Test other flags if needed
// For example, testing a string flag:
// outputFormat, exists := flags.GetFlagValue[string](helper.Registry, "output")
// assert.True(t, exists)
// assert.Equal(t, "", outputFormat) // Default should be empty string
// Or testing a string slice flag:
// suppress, exists := flags.GetFlagValue[[]string](helper.Registry, "suppress")
// assert.True(t, exists)
// assert.Empty(t, suppress) // Default should be empty slice
}

View File

@ -5,22 +5,23 @@ import (
"github.com/helmfile/helmfile/pkg/app"
"github.com/helmfile/helmfile/pkg/config"
"github.com/helmfile/helmfile/pkg/flags"
"github.com/helmfile/helmfile/pkg/factory"
)
// NewSyncCmd returns sync subcmd
func NewSyncCmd(globalCfg *config.GlobalImpl) *cobra.Command {
syncOptions := config.NewSyncOptions()
flagRegistrar := flags.NewSyncFlagRegistrar()
optionsFactory := factory.NewSyncOptionsFactory()
options := optionsFactory.CreateOptions().(*config.SyncOptions)
flagRegistry := optionsFactory.GetFlagRegistry()
cmd := &cobra.Command{
Use: "sync",
Short: "Sync releases defined in state file",
PreRun: func(cmd *cobra.Command, args []string) {
flagRegistrar.TransferFlags(cmd, syncOptions)
flagRegistry.TransferFlags(cmd, options)
},
RunE: func(cmd *cobra.Command, args []string) error {
syncImpl := config.NewSyncImpl(globalCfg, syncOptions)
syncImpl := config.NewSyncImpl(globalCfg, options)
err := config.NewCLIConfigImpl(syncImpl.GlobalImpl)
if err != nil {
return err
@ -37,28 +38,28 @@ func NewSyncCmd(globalCfg *config.GlobalImpl) *cobra.Command {
f := cmd.Flags()
f.StringVar(&globalCfg.GlobalOptions.Args, "args", "", "pass args to helm sync")
f.StringVar(&syncOptions.SyncArgs, "sync-args", "", "pass args to helm upgrade")
f.StringArrayVar(&syncOptions.Set, "set", nil, "additional values to be merged into the helm command --set flag")
f.StringArrayVar(&syncOptions.Values, "values", nil, "additional value files to be merged into the helm command --values flag")
f.IntVar(&syncOptions.Concurrency, "concurrency", 0, "maximum number of concurrent helm processes to run, 0 is unlimited")
f.BoolVar(&syncOptions.Validate, "validate", false, "validate your manifests against the Kubernetes cluster you are currently pointing at. Note that this requires access to a Kubernetes cluster to obtain information necessary for validating, like the sync of available API versions")
f.BoolVar(&syncOptions.SkipNeeds, "skip-needs", true, `do not automatically include releases from the target release's "needs" when --selector/-l flag is provided. Does nothing when --selector/-l flag is not provided. Defaults to true when --include-needs or --include-transitive-needs is not provided`)
f.BoolVar(&syncOptions.IncludeNeeds, "include-needs", false, `automatically include releases from the target release's "needs" when --selector/-l flag is provided. Does nothing when --selector/-l flag is not provided`)
f.BoolVar(&syncOptions.IncludeTransitiveNeeds, "include-transitive-needs", false, `like --include-needs, but also includes transitive needs (needs of needs). Does nothing when --selector/-l flag is not provided. Overrides exclusions of other selectors and conditions.`)
f.BoolVar(&syncOptions.HideNotes, "hide-notes", false, "add --hide-notes flag to helm")
f.BoolVar(&syncOptions.TakeOwnership, "take-ownership", false, `add --take-ownership flag to helm`)
f.BoolVar(&syncOptions.SyncReleaseLabels, "sync-release-labels", false, "sync release labels to the target release")
f.BoolVar(&syncOptions.Wait, "wait", false, `Override helmDefaults.wait setting "helm upgrade --install --wait"`)
f.BoolVar(&syncOptions.WaitForJobs, "wait-for-jobs", false, `Override helmDefaults.waitForJobs setting "helm upgrade --install --wait-for-jobs"`)
f.BoolVar(&syncOptions.ReuseValues, "reuse-values", false, `Override helmDefaults.reuseValues "helm upgrade --install --reuse-values"`)
f.BoolVar(&syncOptions.ResetValues, "reset-values", false, `Override helmDefaults.reuseValues "helm upgrade --install --reset-values"`)
f.StringVar(&syncOptions.PostRenderer, "post-renderer", "", `pass --post-renderer to "helm template" or "helm upgrade --install"`)
f.StringArrayVar(&syncOptions.PostRendererArgs, "post-renderer-args", nil, `pass --post-renderer-args to "helm template" or "helm upgrade --install"`)
f.BoolVar(&syncOptions.SkipSchemaValidation, "skip-schema-validation", false, `pass --skip-schema-validation to "helm template" or "helm upgrade --install"`)
f.StringVar(&syncOptions.Cascade, "cascade", "", "pass cascade to helm exec, default: background")
f.StringVar(&options.SyncArgs, "sync-args", "", "pass args to helm upgrade")
f.StringArrayVar(&options.Set, "set", nil, "additional values to be merged into the helm command --set flag")
f.StringArrayVar(&options.Values, "values", nil, "additional value files to be merged into the helm command --values flag")
f.IntVar(&options.Concurrency, "concurrency", 0, "maximum number of concurrent helm processes to run, 0 is unlimited")
f.BoolVar(&options.Validate, "validate", false, "validate your manifests against the Kubernetes cluster you are currently pointing at. Note that this requires access to a Kubernetes cluster to obtain information necessary for validating, like the sync of available API versions")
f.BoolVar(&options.SkipNeeds, "skip-needs", true, `do not automatically include releases from the target release's "needs" when --selector/-l flag is provided. Does nothing when --selector/-l flag is not provided. Defaults to true when --include-needs or --include-transitive-needs is not provided`)
f.BoolVar(&options.IncludeNeeds, "include-needs", false, `automatically include releases from the target release's "needs" when --selector/-l flag is provided. Does nothing when --selector/-l flag is not provided`)
f.BoolVar(&options.IncludeTransitiveNeeds, "include-transitive-needs", false, `like --include-needs, but also includes transitive needs (needs of needs). Does nothing when --selector/-l flag is not provided. Overrides exclusions of other selectors and conditions.`)
f.BoolVar(&options.HideNotes, "hide-notes", false, "add --hide-notes flag to helm")
f.BoolVar(&options.TakeOwnership, "take-ownership", false, `add --take-ownership flag to helm`)
f.BoolVar(&options.SyncReleaseLabels, "sync-release-labels", false, "sync release labels to the target release")
f.BoolVar(&options.Wait, "wait", false, `Override helmDefaults.wait setting "helm upgrade --install --wait"`)
f.BoolVar(&options.WaitForJobs, "wait-for-jobs", false, `Override helmDefaults.waitForJobs setting "helm upgrade --install --wait-for-jobs"`)
f.BoolVar(&options.ReuseValues, "reuse-values", false, `Override helmDefaults.reuseValues "helm upgrade --install --reuse-values"`)
f.BoolVar(&options.ResetValues, "reset-values", false, `Override helmDefaults.reuseValues "helm upgrade --install --reset-values"`)
f.StringVar(&options.PostRenderer, "post-renderer", "", `pass --post-renderer to "helm template" or "helm upgrade --install"`)
f.StringArrayVar(&options.PostRendererArgs, "post-renderer-args", nil, `pass --post-renderer-args to "helm template" or "helm upgrade --install"`)
f.BoolVar(&options.SkipSchemaValidation, "skip-schema-validation", false, `pass --skip-schema-validation to "helm template" or "helm upgrade --install"`)
f.StringVar(&options.Cascade, "cascade", "", "pass cascade to helm exec, default: background")
// Register flags using the registrar
flagRegistrar.RegisterFlags(cmd)
// Register flags using the registry
flagRegistry.RegisterFlags(cmd)
return cmd
}

38
cmd/sync_test.go Normal file
View File

@ -0,0 +1,38 @@
// cmd/diff_test.go
package cmd
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/helmfile/helmfile/pkg/config"
"github.com/helmfile/helmfile/pkg/flags"
"github.com/helmfile/helmfile/pkg/testcmd"
)
func TestNewSyncCmd(t *testing.T) {
// Test the actual command properties
globalCfg := config.NewGlobalImpl(&config.GlobalOptions{HelmBinary: "helm"})
cmd := NewSyncCmd(globalCfg)
assert.Equal(t, "sync", cmd.Use)
// Use the test helper for testing flags
helper := testcmd.TestSyncCmd()
assert.Equal(t, helper.Cmd.Use, cmd.Use)
// Get the names of registered flags
registeredFlags := helper.Registry.GetRegisteredFlagNames()
// Verify flags and values
assert.Contains(t, registeredFlags, "include-crds")
assert.Contains(t, registeredFlags, "skip-crds")
includeCRDs, exists := flags.GetFlagValue[bool](helper.Registry, "include-crds")
assert.True(t, exists)
assert.False(t, includeCRDs)
skipCRDs, exists := flags.GetFlagValue[bool](helper.Registry, "skip-crds")
assert.True(t, exists)
assert.False(t, skipCRDs)
}

View File

@ -5,22 +5,23 @@ import (
"github.com/helmfile/helmfile/pkg/app"
"github.com/helmfile/helmfile/pkg/config"
"github.com/helmfile/helmfile/pkg/flags"
"github.com/helmfile/helmfile/pkg/factory"
)
// NewTemplateCmd returm template subcmd
func NewTemplateCmd(globalCfg *config.GlobalImpl) *cobra.Command {
templateOptions := config.NewTemplateOptions()
flagRegistrar := flags.NewDiffFlagRegistrar()
optionsFactory := factory.NewTemplateOptionsFactory()
options := optionsFactory.CreateOptions().(*config.TemplateOptions)
flagRegistry := optionsFactory.GetFlagRegistry()
cmd := &cobra.Command{
Use: "template",
Short: "Template releases defined in state file",
PreRun: func(cmd *cobra.Command, args []string) {
flagRegistrar.TransferFlags(cmd, templateOptions)
flagRegistry.TransferFlags(cmd, options)
},
RunE: func(cmd *cobra.Command, args []string) error {
templateImpl := config.NewTemplateImpl(globalCfg, templateOptions)
templateImpl := config.NewTemplateImpl(globalCfg, options)
err := config.NewCLIConfigImpl(templateImpl.GlobalImpl)
if err != nil {
return err
@ -37,24 +38,24 @@ func NewTemplateCmd(globalCfg *config.GlobalImpl) *cobra.Command {
f := cmd.Flags()
f.StringVar(&globalCfg.GlobalOptions.Args, "args", "", "pass args to helm template")
f.StringArrayVar(&templateOptions.Set, "set", nil, "additional values to be merged into the helm command --set flag")
f.StringArrayVar(&templateOptions.Values, "values", nil, "additional value files to be merged into the helm command --values flag")
f.StringVar(&templateOptions.OutputDir, "output-dir", "", "output directory to pass to helm template (helm template --output-dir)")
f.StringVar(&templateOptions.OutputDirTemplate, "output-dir-template", "", "go text template for generating the output directory. Default: {{ .OutputDir }}/{{ .State.BaseName }}-{{ .State.AbsPathSHA1 }}-{{ .Release.Name}}")
f.IntVar(&templateOptions.Concurrency, "concurrency", 0, "maximum number of concurrent helm processes to run, 0 is unlimited")
f.BoolVar(&templateOptions.Validate, "validate", false, "validate your manifests against the Kubernetes cluster you are currently pointing at. Note that this requires access to a Kubernetes cluster to obtain information necessary for validating, like the template of available API versions")
f.BoolVar(&templateOptions.SkipTests, "skip-tests", false, "skip tests from templated output")
f.BoolVar(&templateOptions.SkipNeeds, "skip-needs", true, `do not automatically include releases from the target release's "needs" when --selector/-l flag is provided. Does nothing when --selector/-l flag is not provided. Defaults to true when --include-needs or --include-transitive-needs is not provided`)
f.BoolVar(&templateOptions.IncludeNeeds, "include-needs", false, `automatically include releases from the target release's "needs" when --selector/-l flag is provided. Does nothing when --selector/-l flag is not provided`)
f.BoolVar(&templateOptions.IncludeTransitiveNeeds, "include-transitive-needs", false, `like --include-needs, but also includes transitive needs (needs of needs). Does nothing when --selector/-l flag is not provided. Overrides exclusions of other selectors and conditions.`)
f.BoolVar(&templateOptions.SkipCleanup, "skip-cleanup", false, "Stop cleaning up temporary values generated by helmfile and helm-secrets. Useful for debugging. Don't use in production for security")
f.BoolVar(&templateOptions.NoHooks, "no-hooks", false, "do not template files made by hooks.")
f.StringVar(&templateOptions.PostRenderer, "post-renderer", "", `pass --post-renderer to "helm template" or "helm upgrade --install"`)
f.StringArrayVar(&templateOptions.PostRendererArgs, "post-renderer-args", nil, `pass --post-renderer-args to "helm template" or "helm upgrade --install"`)
f.BoolVar(&templateOptions.SkipSchemaValidation, "skip-schema-validation", false, `pass skip-schema-validation to "helm template" or "helm upgrade --install"`)
f.StringVar(&templateOptions.KubeVersion, "kube-version", "", `pass --kube-version to "helm template". Overrides kubeVersion in helmfile.yaml`)
f.StringArrayVar(&templateOptions.ShowOnly, "show-only", nil, `pass --show-only to "helm template"`)
f.StringArrayVar(&options.Set, "set", nil, "additional values to be merged into the helm command --set flag")
f.StringArrayVar(&options.Values, "values", nil, "additional value files to be merged into the helm command --values flag")
f.StringVar(&options.OutputDir, "output-dir", "", "output directory to pass to helm template (helm template --output-dir)")
f.StringVar(&options.OutputDirTemplate, "output-dir-template", "", "go text template for generating the output directory. Default: {{ .OutputDir }}/{{ .State.BaseName }}-{{ .State.AbsPathSHA1 }}-{{ .Release.Name}}")
f.IntVar(&options.Concurrency, "concurrency", 0, "maximum number of concurrent helm processes to run, 0 is unlimited")
f.BoolVar(&options.Validate, "validate", false, "validate your manifests against the Kubernetes cluster you are currently pointing at. Note that this requires access to a Kubernetes cluster to obtain information necessary for validating, like the template of available API versions")
f.BoolVar(&options.SkipTests, "skip-tests", false, "skip tests from templated output")
f.BoolVar(&options.SkipNeeds, "skip-needs", true, `do not automatically include releases from the target release's "needs" when --selector/-l flag is provided. Does nothing when --selector/-l flag is not provided. Defaults to true when --include-needs or --include-transitive-needs is not provided`)
f.BoolVar(&options.IncludeNeeds, "include-needs", false, `automatically include releases from the target release's "needs" when --selector/-l flag is provided. Does nothing when --selector/-l flag is not provided`)
f.BoolVar(&options.IncludeTransitiveNeeds, "include-transitive-needs", false, `like --include-needs, but also includes transitive needs (needs of needs). Does nothing when --selector/-l flag is not provided. Overrides exclusions of other selectors and conditions.`)
f.BoolVar(&options.SkipCleanup, "skip-cleanup", false, "Stop cleaning up temporary values generated by helmfile and helm-secrets. Useful for debugging. Don't use in production for security")
f.BoolVar(&options.NoHooks, "no-hooks", false, "do not template files made by hooks.")
f.StringVar(&options.PostRenderer, "post-renderer", "", `pass --post-renderer to "helm template" or "helm upgrade --install"`)
f.StringArrayVar(&options.PostRendererArgs, "post-renderer-args", nil, `pass --post-renderer-args to "helm template" or "helm upgrade --install"`)
f.BoolVar(&options.SkipSchemaValidation, "skip-schema-validation", false, `pass skip-schema-validation to "helm template" or "helm upgrade --install"`)
f.StringVar(&options.KubeVersion, "kube-version", "", `pass --kube-version to "helm template". Overrides kubeVersion in helmfile.yaml`)
f.StringArrayVar(&options.ShowOnly, "show-only", nil, `pass --show-only to "helm template"`)
flagRegistrar.RegisterFlags(cmd)
flagRegistry.RegisterFlags(cmd)
return cmd
}

33
cmd/template_test.go Normal file
View File

@ -0,0 +1,33 @@
// cmd/diff_test.go
package cmd
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/helmfile/helmfile/pkg/config"
"github.com/helmfile/helmfile/pkg/flags"
"github.com/helmfile/helmfile/pkg/testcmd"
)
func TestNewTemplateCmd(t *testing.T) {
// Test the actual command properties
globalCfg := config.NewGlobalImpl(&config.GlobalOptions{HelmBinary: "helm"})
cmd := NewTemplateCmd(globalCfg)
assert.Equal(t, "template", cmd.Use)
// Use the test helper for testing flags
helper := testcmd.TestTemplateCmd()
assert.Equal(t, helper.Cmd.Use, cmd.Use)
// Get the names of registered flags
registeredFlags := helper.Registry.GetRegisteredFlagNames()
// Verify flags and values
assert.Contains(t, registeredFlags, "include-crds")
includeCRDs, exists := flags.GetFlagValue[bool](helper.Registry, "include-crds")
assert.True(t, exists)
assert.False(t, includeCRDs)
}