From af68219ddb32410290a8226dc489ee6b714b8b50 Mon Sep 17 00:00:00 2001 From: lei Date: Tue, 2 Jun 2026 10:38:10 +0300 Subject: [PATCH] feat(help): show "s3 (beta)" tag in the root command list A command with Annotations[TagAnnotation]= now renders as "name (tag)" in the grouped `verda` help. Tag the s3 command "beta". Generic so any command can carry a status label later. Co-Authored-By: Claude Opus 4.8 (1M context) --- internal/verda-cli/cmd/s3/s3.go | 3 +- internal/verda-cli/cmd/util/command_groups.go | 10 ++++- .../verda-cli/cmd/util/command_groups_test.go | 43 +++++++++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 internal/verda-cli/cmd/util/command_groups_test.go diff --git a/internal/verda-cli/cmd/s3/s3.go b/internal/verda-cli/cmd/s3/s3.go index ff86dd2..c3cba1f 100644 --- a/internal/verda-cli/cmd/s3/s3.go +++ b/internal/verda-cli/cmd/s3/s3.go @@ -37,7 +37,8 @@ func NewCmdS3(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cobra.Command { Show current S3 credential status: verda s3 show `), - Run: cmdutil.DefaultSubCommandRun(ioStreams.Out), + Annotations: map[string]string{cmdutil.TagAnnotation: "beta"}, + Run: cmdutil.DefaultSubCommandRun(ioStreams.Out), } cmd.AddCommand( diff --git a/internal/verda-cli/cmd/util/command_groups.go b/internal/verda-cli/cmd/util/command_groups.go index e254c2f..62bb293 100644 --- a/internal/verda-cli/cmd/util/command_groups.go +++ b/internal/verda-cli/cmd/util/command_groups.go @@ -21,6 +21,10 @@ import ( "github.com/spf13/cobra" ) +// TagAnnotation marks a command with a short status label (e.g. "beta") shown +// next to its name in the grouped root help. Set via cmd.Annotations. +const TagAnnotation = "verda.tag" + // CommandGroup represents a logical group of subcommands with a heading message. type CommandGroup struct { Message string @@ -84,7 +88,11 @@ func SetUsageTemplate(cmd *cobra.Command, groups CommandGroups) { if c.Hidden { continue } - fmt.Fprintf(&b, " %-18s %s\n", c.Name(), c.Short) + name := c.Name() + if tag := c.Annotations[TagAnnotation]; tag != "" { + name += " (" + tag + ")" + } + fmt.Fprintf(&b, " %-18s %s\n", name, c.Short) } b.WriteString("\n") } diff --git a/internal/verda-cli/cmd/util/command_groups_test.go b/internal/verda-cli/cmd/util/command_groups_test.go new file mode 100644 index 0000000..6ef9b56 --- /dev/null +++ b/internal/verda-cli/cmd/util/command_groups_test.go @@ -0,0 +1,43 @@ +// Copyright 2026 Verda Cloud Oy +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package util + +import ( + "strings" + "testing" + + "github.com/spf13/cobra" +) + +// TestSetUsageTemplate_TagAnnotation verifies a command's TagAnnotation renders +// as "name (tag)" in the grouped help, while untagged commands stay plain. +func TestSetUsageTemplate_TagAnnotation(t *testing.T) { + t.Parallel() + tagged := &cobra.Command{Use: "s3", Short: "Manage S3 object storage", + Annotations: map[string]string{TagAnnotation: "beta"}} + plain := &cobra.Command{Use: "volume", Short: "Manage volumes"} + root := &cobra.Command{Use: "verda"} + groups := CommandGroups{{Message: "Resource Commands:", Commands: []*cobra.Command{tagged, plain}}} + groups.Add(root) + SetUsageTemplate(root, groups) + + tmpl := root.UsageTemplate() + if !strings.Contains(tmpl, "s3 (beta)") { + t.Errorf("tagged command should render as \"s3 (beta)\"; got:\n%s", tmpl) + } + if strings.Contains(tmpl, "volume (") { + t.Errorf("untagged command must not get a tag; got:\n%s", tmpl) + } +}