From 12db194afa2d0d65326159a59911953a3dcd0e4a Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Wed, 11 May 2022 12:42:07 +0200 Subject: [PATCH] Introduce concept of primary flags (#812) * Add flag to point to the package root * Introduce concept of primary flags * Fix --- cmd/dump.go | 8 ++--- cmd/install.go | 20 +++++++++---- cmd/promote.go | 4 +-- .../cobraext/{command_action.go => action.go} | 0 .../cobraext/{command_flag.go => errors.go} | 0 internal/cobraext/{const.go => flags.go} | 29 +++++++++++++------ 6 files changed, 40 insertions(+), 21 deletions(-) rename internal/cobraext/{command_action.go => action.go} (100%) rename internal/cobraext/{command_flag.go => errors.go} (100%) rename internal/cobraext/{const.go => flags.go} (89%) diff --git a/cmd/dump.go b/cmd/dump.go index 47287dbe3..9c9285905 100644 --- a/cmd/dump.go +++ b/cmd/dump.go @@ -33,8 +33,8 @@ func setupDumpCommand() *cobraext.Command { Short: "Dump package assets", Long: dumpLongDescription, } - cmd.PersistentFlags().StringP(cobraext.DumpPackageFlagName, "p", "", cobraext.DumpPackageFlagDescription) - cmd.MarkFlagRequired(cobraext.DumpPackageFlagName) + cmd.PersistentFlags().StringP(cobraext.PackageFlagName, cobraext.PackageFlagShorthand, "", cobraext.PackageFlagDescription) + cmd.MarkFlagRequired(cobraext.PackageFlagName) cmd.PersistentFlags().StringP(cobraext.DumpOutputFlagName, "o", "package-dump", cobraext.DumpOutputFlagDescription) cmd.AddCommand(dumpInstalledObjectsCmd) @@ -43,9 +43,9 @@ func setupDumpCommand() *cobraext.Command { } func dumpInstalledObjectsCmd(cmd *cobra.Command, args []string) error { - packageName, err := cmd.Flags().GetString(cobraext.DumpPackageFlagName) + packageName, err := cmd.Flags().GetString(cobraext.PackageFlagName) if err != nil { - return cobraext.FlagParsingError(err, cobraext.DumpPackageFlagName) + return cobraext.FlagParsingError(err, cobraext.PackageFlagName) } outputPath, err := cmd.Flags().GetString(cobraext.DumpOutputFlagName) diff --git a/cmd/install.go b/cmd/install.go index 20f500d4a..12450ee76 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -25,17 +25,25 @@ func setupInstallCommand() *cobraext.Command { RunE: installCommandAction, } cmd.Flags().StringSliceP(cobraext.CheckConditionFlagName, "c", nil, cobraext.CheckConditionFlagDescription) + cmd.Flags().StringP(cobraext.PackageRootFlagName, cobraext.PackageRootFlagShorthand, "", cobraext.PackageRootFlagDescription) return cobraext.NewCommand(cmd, cobraext.ContextPackage) } -func installCommandAction(cmd *cobra.Command, args []string) error { - packageRootPath, found, err := packages.FindPackageRoot() - if !found { - return errors.New("package root not found") - } +func installCommandAction(cmd *cobra.Command, _ []string) error { + packageRootPath, err := cmd.Flags().GetString(cobraext.PackageRootFlagName) if err != nil { - return errors.Wrap(err, "locating package root failed") + return cobraext.FlagParsingError(err, cobraext.PackageRootFlagName) + } + if packageRootPath == "" { + var found bool + packageRootPath, found, err = packages.FindPackageRoot() + if !found { + return errors.New("package root not found") + } + if err != nil { + return errors.Wrap(err, "locating package root failed") + } } m, err := packages.ReadPackageManifestFromPackageRoot(packageRootPath) diff --git a/cmd/promote.go b/cmd/promote.go index 933e6175f..a8d35cc8e 100644 --- a/cmd/promote.go +++ b/cmd/promote.go @@ -43,7 +43,7 @@ func setupPromoteCommand() *cobraext.Command { } cmd.Flags().StringP(cobraext.DirectionFlagName, "d", "", cobraext.DirectionFlagDescription) cmd.Flags().BoolP(cobraext.NewestOnlyFlagName, "n", false, cobraext.NewestOnlyFlagDescription) - cmd.Flags().StringSliceP(cobraext.PackagesFlagName, "p", nil, cobraext.PackagesFlagDescription) + cmd.Flags().StringSliceP(cobraext.PromotedPackagesFlagName, "p", nil, cobraext.PromotedPackagesFlagDescription) return cobraext.NewCommand(cmd, cobraext.ContextGlobal) } @@ -210,7 +210,7 @@ func promptPromoteNewestOnly(cmd *cobra.Command) (bool, error) { } func promptPackages(cmd *cobra.Command, packages storage.PackageVersions) (storage.PackageVersions, error) { - revisions, _ := cmd.Flags().GetStringSlice(cobraext.PackagesFlagName) + revisions, _ := cmd.Flags().GetStringSlice(cobraext.PromotedPackagesFlagName) if len(revisions) > 0 { parsed, err := storage.ParsePackageVersions(revisions) if err != nil { diff --git a/internal/cobraext/command_action.go b/internal/cobraext/action.go similarity index 100% rename from internal/cobraext/command_action.go rename to internal/cobraext/action.go diff --git a/internal/cobraext/command_flag.go b/internal/cobraext/errors.go similarity index 100% rename from internal/cobraext/command_flag.go rename to internal/cobraext/errors.go diff --git a/internal/cobraext/const.go b/internal/cobraext/flags.go similarity index 89% rename from internal/cobraext/const.go rename to internal/cobraext/flags.go index c0e5b799f..401bf5af1 100644 --- a/internal/cobraext/const.go +++ b/internal/cobraext/flags.go @@ -4,7 +4,24 @@ package cobraext -// Flag names and descriptions used by CLI commands. +// Global flags +const ( + VerboseFlagName = "verbose" + VerboseFlagDescription = "verbose mode" +) + +// Primary flags reused by multiple commands +const ( + PackageRootFlagName = "root" + PackageRootFlagShorthand = "R" + PackageRootFlagDescription = "root directory of the package" + + PackageFlagName = "package" + PackageFlagShorthand = "P" + PackageFlagDescription = "name of the package" +) + +// Flag names and descriptions used by CLI commands const ( BuildZipFlagName = "zip" BuildZipFlagDescription = "archive the built package" @@ -48,9 +65,6 @@ const ( DumpOutputFlagName = "output" DumpOutputFlagDescription = "path to directory where exported assets will be stored" - DumpPackageFlagName = "package" - DumpPackageFlagDescription = "package whose assets will be collected" - FailOnMissingFlagName = "fail-on-missing" FailOnMissingFlagDescription = "fail if tests are missing" @@ -75,8 +89,8 @@ const ( NewestOnlyFlagName = "newest-only" NewestOnlyFlagDescription = "promote newest packages and remove old ones" - PackagesFlagName = "packages" - PackagesFlagDescription = "packages to be promoted (comma-separated values: apache-1.2.3,nginx-5.6.7)" + PromotedPackagesFlagName = "packages" + PromotedPackagesFlagDescription = "packages to be promoted (comma-separated values: apache-1.2.3,nginx-5.6.7)" ReportFormatFlagName = "report-format" ReportFormatFlagDescription = "format of test report" @@ -113,7 +127,4 @@ const ( VariantFlagName = "variant" VariantFlagDescription = "service variant" - - VerboseFlagName = "verbose" - VerboseFlagDescription = "verbose mode" )