Skip to content

Commit

Permalink
Add defaults for version flag while installing packages using kctrl
Browse files Browse the repository at this point in the history
Signed-off-by: Soumik Majumder <[email protected]>
  • Loading branch information
100mik committed Jul 12, 2024
1 parent 4821a0d commit cc30d9e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
19 changes: 6 additions & 13 deletions cli/pkg/kctrl/cmd/package/installed/create_or_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (
valuesFileKey = "values.yaml"
yttOverlayPrefix = "ext.packaging.carvel.dev/ytt-paths-from-secret-name"
yttOverlayAnnotation = yttOverlayPrefix + ".kctrl-ytt-overlays"
defaultPkgiVersion = ">= 0.0.0"
)

type CreateOrUpdateOptions struct {
Expand Down Expand Up @@ -100,7 +101,7 @@ func NewCreateCmd(o *CreateOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) *
}

cmd.Flags().StringVarP(&o.packageName, "package", "p", "", "Set package name (required)")
cmd.Flags().StringVarP(&o.version, "version", "v", "", "Set package version (required)")
cmd.Flags().StringVarP(&o.version, "version", "v", defaultPkgiVersion, "Set package version")
cmd.Flags().StringVar(&o.serviceAccountName, "service-account-name", "", "Name of an existing service account used to install underlying package contents, optional")
cmd.Flags().StringVar(&o.valuesFile, "values-file", "", "The path to the configuration values file, optional")
cmd.Flags().BoolVar(&o.values, "values", true, "Add or keep values supplied to package install, optional")
Expand Down Expand Up @@ -146,7 +147,7 @@ func NewInstallCmd(o *CreateOrUpdateOptions, flagsFactory cmdcore.FlagsFactory)
}

cmd.Flags().StringVarP(&o.packageName, "package", "p", "", "Set package name (required)")
cmd.Flags().StringVarP(&o.version, "version", "v", "", "Set package version (required)")
cmd.Flags().StringVarP(&o.version, "version", "v", defaultPkgiVersion, "Set package version")
cmd.Flags().StringVar(&o.serviceAccountName, "service-account-name", "", "Name of an existing service account used to install underlying package contents, optional")
cmd.Flags().StringVar(&o.valuesFile, "values-file", "", "The path to the configuration values file, optional")
cmd.Flags().BoolVar(&o.values, "values", true, "Add or keep values supplied to package install, optional")
Expand Down Expand Up @@ -235,17 +236,9 @@ func (o *CreateOrUpdateOptions) RunCreate(args []string) error {
return nil
}

if len(o.version) == 0 {
pkgClient, err := o.depsFactory.PackageClient()
if err != nil {
return err
}

err = o.showVersions(pkgClient)
if err != nil {
return err
}
return fmt.Errorf("Expected package version to be non empty")
if o.version == defaultPkgiVersion {
o.statusUI.PrintMessage(fmt.Sprintf(`Version constraint defaulted to '%s'. Use the '--version'
flag to lock to a non-latest version.`, defaultPkgiVersion))
}

client, err := o.depsFactory.CoreClient()
Expand Down
11 changes: 11 additions & 0 deletions cli/test/e2e/package_install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,3 +327,14 @@ key1: value1:
"--values-file", "-"}, RunOpts{StdinReader: strings.NewReader(valuesFile2)})
})
}

func TestPackageInstallVersionDefaults(t *testing.T) {
env := BuildEnv(t)
logger := Logger{}
kappCtrl := Kctrl{t, env.Namespace, env.KctrlBinaryPath, logger}

logger.Section("Listing with error in package install", func() {
out, _ := kappCtrl.RunWithOpts([]string{"package", "install", "-i", "asdf", "-p", "asdf.asdf.com", "--dry-run", "-n", "installs"}, RunOpts{})
require.Contains(t, out, "constraints: '>= 0.0.0'")
})
}

0 comments on commit cc30d9e

Please sign in to comment.