Skip to content

Commit a7a0196

Browse files
committed
make --image-ref-template alpha (and include simple framework for alpha flags and usage)
Signed-off-by: Joe Lanford <[email protected]>
1 parent e6f8920 commit a7a0196

File tree

8 files changed

+71
-34
lines changed

8 files changed

+71
-34
lines changed

cmd/opm/alpha/cmd.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ import (
99
"github.com/operator-framework/operator-registry/cmd/opm/alpha/template"
1010
)
1111

12-
func NewCmd() *cobra.Command {
12+
func NewCmd(showAlphaHelp bool) *cobra.Command {
1313
runCmd := &cobra.Command{
14-
Hidden: true,
15-
Use: "alpha",
16-
Short: "Run an alpha subcommand",
17-
Args: cobra.NoArgs,
18-
Run: func(_ *cobra.Command, _ []string) {}, // adding an empty function here to preserve non-zero exit status for misstated subcommands/flags for the command hierarchy
14+
Use: "alpha",
15+
Short: "Run an alpha subcommand",
16+
Args: cobra.NoArgs,
17+
Run: func(_ *cobra.Command, _ []string) {}, // adding an empty function here to preserve non-zero exit status for misstated subcommands/flags for the command hierarchy
18+
}
19+
20+
if !showAlphaHelp {
21+
runCmd.Hidden = true
1922
}
2023

2124
runCmd.AddCommand(

cmd/opm/index/add.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ var (
4141
`)
4242
)
4343

44-
func addIndexAddCmd(parent *cobra.Command) {
44+
func addIndexAddCmd(parent *cobra.Command, showAlphaHelp bool) {
4545
indexCmd := &cobra.Command{
4646
Use: "add",
4747
Short: "Add operator bundles to an index.",
@@ -78,8 +78,10 @@ func addIndexAddCmd(parent *cobra.Command) {
7878
logrus.Panic(err.Error())
7979
}
8080
indexCmd.Flags().Bool("enable-alpha", false, "enable unsupported alpha features of the OPM CLI")
81-
if err := indexCmd.Flags().MarkHidden("enable-alpha"); err != nil {
82-
logrus.Panic(err.Error())
81+
if !showAlphaHelp {
82+
if err := indexCmd.Flags().MarkHidden("enable-alpha"); err != nil {
83+
logrus.Panic(err.Error())
84+
}
8385
}
8486
if err := indexCmd.Flags().MarkHidden("debug"); err != nil {
8587
logrus.Panic(err.Error())

cmd/opm/index/cmd.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
// AddCommand adds the index subcommand to the given parent command.
11-
func AddCommand(parent *cobra.Command) {
11+
func AddCommand(parent *cobra.Command, showAlphaHelp bool) {
1212
cmd := &cobra.Command{
1313
Use: "index",
1414
Short: "generate operator index container images",
@@ -34,7 +34,7 @@ func AddCommand(parent *cobra.Command) {
3434
parent.AddCommand(cmd)
3535

3636
cmd.AddCommand(newIndexDeleteCmd())
37-
addIndexAddCmd(cmd)
37+
addIndexAddCmd(cmd, showAlphaHelp)
3838
cmd.AddCommand(newIndexExportCmd())
3939
cmd.AddCommand(newIndexPruneCmd())
4040
cmd.AddCommand(newIndexDeprecateTruncateCmd())

cmd/opm/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import (
1010
)
1111

1212
func main() {
13-
cmd := root.NewCmd()
13+
showAlphaHelp := os.Getenv("HELP_ALPHA") == "true"
14+
cmd := root.NewCmd(showAlphaHelp)
1415
if err := cmd.Execute(); err != nil {
1516
agg, ok := err.(utilerrors.Aggregate)
1617
if !ok {

cmd/opm/registry/add.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/operator-framework/operator-registry/pkg/sqlite"
1515
)
1616

17-
func newRegistryAddCmd() *cobra.Command {
17+
func newRegistryAddCmd(showAlphaHelp bool) *cobra.Command {
1818
rootCmd := &cobra.Command{
1919
Use: "add",
2020
Short: "add operator bundle to operator registry DB",
@@ -48,8 +48,10 @@ func newRegistryAddCmd() *cobra.Command {
4848
logrus.Panic(err.Error())
4949
}
5050
rootCmd.Flags().Bool("enable-alpha", false, "enable unsupported alpha features of the OPM CLI")
51-
if err := rootCmd.Flags().MarkHidden("enable-alpha"); err != nil {
52-
logrus.Panic(err.Error())
51+
if !showAlphaHelp {
52+
if err := rootCmd.Flags().MarkHidden("enable-alpha"); err != nil {
53+
logrus.Panic(err.Error())
54+
}
5355
}
5456
if err := rootCmd.Flags().MarkDeprecated("skip-tls", "use --use-http and --skip-tls-verify instead"); err != nil {
5557
logrus.Panic(err.Error())

cmd/opm/registry/cmd.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
// NewOpmRegistryCmd returns the appregistry-server command
11-
func NewOpmRegistryCmd() *cobra.Command {
11+
func NewOpmRegistryCmd(showAlphaHelp bool) *cobra.Command {
1212
rootCmd := &cobra.Command{
1313
Use: "registry",
1414
Short: "interact with operator-registry database",
@@ -28,7 +28,7 @@ func NewOpmRegistryCmd() *cobra.Command {
2828
}
2929

3030
rootCmd.AddCommand(newRegistryServeCmd())
31-
rootCmd.AddCommand(newRegistryAddCmd())
31+
rootCmd.AddCommand(newRegistryAddCmd(showAlphaHelp))
3232
rootCmd.AddCommand(newRegistryRmCmd())
3333
rootCmd.AddCommand(newRegistryPruneCmd())
3434
rootCmd.AddCommand(newRegistryPruneStrandedCmd())

cmd/opm/render/cmd.go

+19-13
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"github.com/operator-framework/operator-registry/pkg/sqlite"
1616
)
1717

18-
func NewCmd() *cobra.Command {
18+
func NewCmd(showAlphaHelp bool) *cobra.Command {
1919
var (
2020
render action.Render
2121
output string
@@ -27,17 +27,7 @@ func NewCmd() *cobra.Command {
2727
Long: `Generate a stream of file-based catalog objects to stdout from the provided
2828
catalog images, file-based catalog directories, bundle images, and sqlite
2929
database files.
30-
31-
If rendering sources that do not carry bundle image reference information
32-
(e.g. bundle directories), the --image-ref-template flag can be used to
33-
generate image references for the rendered file-based catalog objects.
34-
This is useful when generating a catalog with image references prior to
35-
those images actually existing. Available template variables are:
36-
- {{.Package}} : the package name the bundle belongs to
37-
- {{.Name}} : the name of the bundle (for registry+v1 bundles, this is the CSV name)
38-
- {{.Version}} : the version of the bundle
39-
40-
` + sqlite.DeprecationMessage,
30+
`,
4131
Args: cobra.MinimumNArgs(1),
4232
Run: func(cmd *cobra.Command, args []string) {
4333
render.Refs = args
@@ -85,7 +75,23 @@ those images actually existing. Available template variables are:
8575
}
8676
cmd.Flags().StringVarP(&output, "output", "o", "json", "Output format of the streamed file-based catalog objects (json|yaml)")
8777
cmd.Flags().BoolVar(&render.Migrate, "migrate", false, "Perform migrations on the rendered FBC")
88-
cmd.Flags().StringVar(&imageRefTemplate, "image-ref-template", "", "When bundle image reference information is unavailable, populate it with this template")
78+
79+
// Alpha flags
80+
cmd.Flags().StringVar(&imageRefTemplate, "alpha-image-ref-template", "", "When bundle image reference information is unavailable, populate it with this template")
81+
82+
if showAlphaHelp {
83+
cmd.Long += `
84+
If rendering sources that do not carry bundle image reference information
85+
(e.g. bundle directories), the --alpha-image-ref-template flag can be used to
86+
generate image references for the rendered file-based catalog objects.
87+
This is useful when generating a catalog with image references prior to
88+
those images actually existing. Available template variables are:
89+
- {{.Package}} : the package name the bundle belongs to
90+
- {{.Name}} : the name of the bundle (for registry+v1 bundles, this is the CSV name)
91+
- {{.Version}} : the version of the bundle
92+
`
93+
}
94+
cmd.Long += "\n" + sqlite.DeprecationMessage
8995
return cmd
9096
}
9197

cmd/opm/root/cmd.go

+27-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package root
22

33
import (
4+
"fmt"
5+
"strings"
6+
47
"github.com/sirupsen/logrus"
58
"github.com/spf13/cobra"
9+
"github.com/spf13/pflag"
610

711
"github.com/operator-framework/operator-registry/cmd/opm/alpha"
812
"github.com/operator-framework/operator-registry/cmd/opm/generate"
@@ -16,11 +20,13 @@ import (
1620
"github.com/operator-framework/operator-registry/cmd/opm/version"
1721
)
1822

19-
func NewCmd() *cobra.Command {
23+
func NewCmd(showAlphaHelp bool) *cobra.Command {
2024
cmd := &cobra.Command{
2125
Use: "opm",
2226
Short: "operator package manager",
23-
Long: "CLI to interact with operator-registry and build indexes of operator content",
27+
Long: `CLI to interact with operator-registry and build indexes of operator content.
28+
29+
To view help related to alpha features, set HELP_ALPHA=true in the environment.`,
2430
PreRunE: func(cmd *cobra.Command, _ []string) error {
2531
if debug, _ := cmd.Flags().GetBool("debug"); debug {
2632
logrus.SetLevel(logrus.DebugLevel)
@@ -38,14 +44,31 @@ func NewCmd() *cobra.Command {
3844
logrus.Panic(err.Error())
3945
}
4046

41-
cmd.AddCommand(registry.NewOpmRegistryCmd(), alpha.NewCmd(), initcmd.NewCmd(), migrate.NewCmd(), serve.NewCmd(), render.NewCmd(), validate.NewCmd(), generate.NewCmd())
42-
index.AddCommand(cmd)
47+
cmd.AddCommand(registry.NewOpmRegistryCmd(showAlphaHelp), alpha.NewCmd(showAlphaHelp), initcmd.NewCmd(), migrate.NewCmd(), serve.NewCmd(), render.NewCmd(showAlphaHelp), validate.NewCmd(), generate.NewCmd())
48+
index.AddCommand(cmd, showAlphaHelp)
4349
version.AddCommand(cmd)
4450

4551
cmd.Flags().Bool("debug", false, "enable debug logging")
4652
if err := cmd.Flags().MarkHidden("debug"); err != nil {
4753
logrus.Panic(err.Error())
4854
}
4955

56+
// Mark all alpha flags as hidden and prepend their usage with an alpha warning
57+
configureAlphaFlags(cmd, !showAlphaHelp)
58+
5059
return cmd
5160
}
61+
62+
func configureAlphaFlags(cmd *cobra.Command, hideFlags bool) {
63+
cmd.Flags().VisitAll(func(f *pflag.Flag) {
64+
if strings.HasPrefix(f.Name, "alpha-") {
65+
if hideFlags {
66+
f.Hidden = true
67+
}
68+
f.Usage = fmt.Sprintf("(ALPHA: This flag will be removed or renamed in a future release, potentially without notice) %s", f.Usage)
69+
}
70+
})
71+
for _, subCmd := range cmd.Commands() {
72+
configureAlphaFlags(subCmd, hideFlags)
73+
}
74+
}

0 commit comments

Comments
 (0)