Skip to content

Commit

Permalink
Introduce concept of primary flags (#812)
Browse files Browse the repository at this point in the history
* Add flag to point to the package root

* Introduce concept of primary flags

* Fix
  • Loading branch information
mtojek authored May 11, 2022
1 parent 71bb31f commit 12db194
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 21 deletions.
8 changes: 4 additions & 4 deletions cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
20 changes: 14 additions & 6 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions cmd/promote.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down
File renamed without changes.
File renamed without changes.
29 changes: 20 additions & 9 deletions internal/cobraext/const.go → internal/cobraext/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"

Expand All @@ -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"
Expand Down Expand Up @@ -113,7 +127,4 @@ const (

VariantFlagName = "variant"
VariantFlagDescription = "service variant"

VerboseFlagName = "verbose"
VerboseFlagDescription = "verbose mode"
)

0 comments on commit 12db194

Please sign in to comment.