Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: consistent aliases #378

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions .ci/docs/md.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"sort"
"strings"

"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
Expand Down Expand Up @@ -120,9 +121,16 @@ func genMarkdown(cmd *cobra.Command, w io.Writer) error {
buf.WriteString(fmt.Sprintf("```\n%s\n```\n\n", cmd.UseLine()))
}

if len(cmd.Aliases) > 0 {
buf.WriteString("## Aliases\n\n")
buf.WriteString(fmt.Sprintf("%s\n\n", formatFlags(cmd.Aliases)))
ctx := cmd.Context()
if ctx != nil {
if wrapper := ctx.Value("command"); wrapper != nil {
if c, ok := wrapper.(*commands.BaseCommand); ok {
if aliases := c.Aliases(); len(aliases) > 0 {
buf.WriteString("## Aliases\n\n")
buf.WriteString(fmt.Sprintf("%s\n\n", formatFlags(aliases)))
}
}
}
}

if len(cmd.Example) > 0 {
Expand Down
20 changes: 19 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,25 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added support for Valkey properties
- Add termination_protection to upctl database show output
- Experimental support for token authentication by defining token in `UPCLOUD_TOKEN` environment variable.
- Experimental support for managing tokens with `account token` commands.
- - Experimental support for managing tokens with `account token` commands.
- Adding deprecation messages to some commands and aliases and adding new command names to improve consistency
- Deprecated commands:
- loadbalancer (new: load-balancer)
- networkpeering (new: network-peering)
- objectstorage (new: object-storage)
- servergroup (new: server-group)
- Deprecated aliases:
- object-storage: objsto
Comment on lines +27 to +33
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could announce these deprecated aliases in a Deprecated section similarly than here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the changes under a deprecated section. Thanks @kangasta

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could further be split so that new aliases are under ### Added and deprecated ones under ### Deprecated

- New aliases:
- account: acc
- gateway: gw
- network-peering: np
- object-storage: obs
- partner: pr
- router: rt
- server: srv
- server-group: sg
- storage: st

### Changed

Expand Down
63 changes: 56 additions & 7 deletions internal/commands/command.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commands

import (
"context"
"fmt"
"strings"

Expand All @@ -13,15 +14,28 @@ import (
"github.com/spf13/pflag"
)

type CommandContextKey string

const commandKey CommandContextKey = "command"

// New returns a BaseCommand that implements Command. It is used as a base to create custom commands from.
func New(name, usage string, examples ...string) *BaseCommand {
return &BaseCommand{
cobra: &cobra.Command{
Use: name,
Short: usage,
Example: strings.Join(examples, "\n"),
},
cmd := &cobra.Command{
Use: name,
Short: usage,
Example: strings.Join(examples, "\n"),
}

// Initialize BaseCommand
baseCmd := &BaseCommand{
cobra: cmd,
}

// Store reference to itself in the context - We need this to access the command in the CobraCommand interface
// Specifically to generate the reference documentation
cmd.SetContext(context.WithValue(context.Background(), commandKey, baseCmd))

return baseCmd
}

// Command is the base command type for all commands.
Expand Down Expand Up @@ -125,7 +139,42 @@ func BuildCommand(child Command, parent *cobra.Command, config *config.Config) C

// BaseCommand is the base type for all commands, implementing Command
type BaseCommand struct {
cobra *cobra.Command
cobra *cobra.Command
deprecatedAliases []string
}

// Aliases return non deprecated aliases
func (s *BaseCommand) Aliases() []string {
// Get all aliases from Cobra
allAliases := s.cobra.Aliases

// Filter out deprecated aliases
var filteredAliases []string
for _, alias := range allAliases {
if !s.isDeprecatedAlias(alias) {
filteredAliases = append(filteredAliases, alias)
}
}

return filteredAliases
}

// isDeprecatedAlias checks if an alias is deprecated
func (s *BaseCommand) isDeprecatedAlias(alias string) bool {
for _, deprecated := range s.deprecatedAliases {
if alias == deprecated {
return true
}
}
return false
}

func (s *BaseCommand) DeprecatedAliases() []string {
return s.deprecatedAliases
}

func (s *BaseCommand) SetDeprecatedAliases(aliases []string) {
s.deprecatedAliases = aliases
}

// MaximumExecutions return the max executed workers
Expand Down
8 changes: 0 additions & 8 deletions internal/commands/kubernetes/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,10 @@ func (c *configCommand) InitCommand() {
"",
"Absolute path for writing output. If the file exists, the config will be merged.")
c.AddFlags(flagSet)

// Deprecating k8s
// TODO: Remove this in the future
commands.SetSubcommandDeprecationHelp(c, []string{"k8s"})
}

// Execute implements commands.MultipleArgumentCommand
func (c *configCommand) Execute(exec commands.Executor, uuid string) (output.Output, error) {
// Deprecating k8s
// TODO: Remove this in the future
commands.SetSubcommandExecutionDeprecationMessage(c, []string{"k8s"}, "uks")

svc := exec.All()

msg := fmt.Sprintf("Getting kubeconfig for Kubernetes cluster %s", uuid)
Expand Down
8 changes: 0 additions & 8 deletions internal/commands/kubernetes/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,6 @@ func (c *createCommand) InitCommand() {
commands.Must(c.Cobra().MarkFlagRequired("network"))
commands.Must(c.Cobra().MarkFlagRequired("zone"))
commands.Must(c.Cobra().RegisterFlagCompletionFunc("name", cobra.NoFileCompletions))

// Deprecating k8s
// TODO: Remove this in the future
commands.SetSubcommandDeprecationHelp(c, []string{"k8s"})
}

func (c *createCommand) InitCommandWithConfig(cfg *config.Config) {
Expand All @@ -166,10 +162,6 @@ func (c *createCommand) InitCommandWithConfig(cfg *config.Config) {

// ExecuteWithoutArguments implements commands.NoArgumentCommand
func (c *createCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Output, error) {
// Deprecating k8s
// TODO: Remove this in the future
commands.SetSubcommandExecutionDeprecationMessage(c, []string{"k8s"}, "uks")

svc := exec.All()

if err := c.params.processParams(exec); err != nil {
Expand Down
11 changes: 0 additions & 11 deletions internal/commands/kubernetes/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,8 @@ type deleteCommand struct {
completion.Kubernetes
}

// InitCommand implements Command.InitCommand
func (s *deleteCommand) InitCommand() {
// Deprecating k8s
// TODO: Remove this in the future
commands.SetSubcommandDeprecationHelp(s, []string{"k8s"})
}

// Execute implements commands.MultipleArgumentCommand
func (s *deleteCommand) Execute(exec commands.Executor, arg string) (output.Output, error) {
// Deprecating k8s
// TODO: Remove this in the future
commands.SetSubcommandExecutionDeprecationMessage(s, []string{"k8s"}, "uks")

svc := exec.All()
msg := fmt.Sprintf("Deleting Kubernetes cluster %v", arg)
exec.PushProgressStarted(msg)
Expand Down
3 changes: 0 additions & 3 deletions internal/commands/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,4 @@ type kubernetesCommand struct {
// InitCommand implements Command.InitCommand
func (k *kubernetesCommand) InitCommand() {
k.Cobra().Aliases = []string{"k8s", "uks"}
// Deprecating k8s
// TODO: Remove this in the future
commands.SetDeprecationHelp(k.Cobra(), []string{"k8s"})
}
11 changes: 0 additions & 11 deletions internal/commands/kubernetes/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,8 @@ type listCommand struct {
*commands.BaseCommand
}

// InitCommand implements Command.InitCommand
func (s *listCommand) InitCommand() {
// Deprecating k8s
// TODO: Remove this in the future
commands.SetSubcommandDeprecationHelp(s, []string{"k8s"})
}

// ExecuteWithoutArguments implements commands.NoArgumentCommand
func (s *listCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Output, error) {
// Deprecating k8s
// TODO: Remove this in the future
commands.SetSubcommandExecutionDeprecationMessage(s, []string{"k8s"}, "uks")

svc := exec.All()
clusters, err := svc.GetKubernetesClusters(exec.Context(), &request.GetKubernetesClustersRequest{})
if err != nil {
Expand Down
8 changes: 0 additions & 8 deletions internal/commands/kubernetes/modify.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,10 @@ func (c *modifyCommand) InitCommand() {
c.AddFlags(fs)
c.Cobra().MarkFlagsMutuallyExclusive("label", "clear-labels")
commands.Must(c.Cobra().RegisterFlagCompletionFunc("label", cobra.NoFileCompletions))

// Deprecating k8s
// TODO: Remove this in the future
commands.SetSubcommandDeprecationHelp(c, []string{"k8s"})
}

// Execute implements commands.MultipleArgumentCommand
func (c *modifyCommand) Execute(exec commands.Executor, arg string) (output.Output, error) {
// Deprecating k8s
// TODO: Remove this in the future
commands.SetSubcommandExecutionDeprecationMessage(c, []string{"k8s"}, "uks")

msg := fmt.Sprintf("Modifying Kubernetes cluster %v", arg)
exec.PushProgressStarted(msg)

Expand Down
11 changes: 0 additions & 11 deletions internal/commands/kubernetes/plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,8 @@ type plansCommand struct {
*commands.BaseCommand
}

// InitCommand implements Command.InitCommand
func (s *plansCommand) InitCommand() {
// Deprecating k8s
// TODO: Remove this in the future
commands.SetSubcommandDeprecationHelp(s, []string{"k8s"})
}

// Execute implements commands.NoArgumentCommand
func (s *plansCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Output, error) {
// Deprecating k8s
// TODO: Remove this in the future
commands.SetSubcommandExecutionDeprecationMessage(s, []string{"k8s"}, "uks")

svc := exec.All()
plans, err := svc.GetKubernetesPlans(exec.Context(), &request.GetKubernetesPlansRequest{})
if err != nil {
Expand Down
11 changes: 0 additions & 11 deletions internal/commands/kubernetes/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,8 @@ type showCommand struct {
completion.Kubernetes
}

// InitCommand implements Command.InitCommand
func (s *showCommand) InitCommand() {
// Deprecating k8s
// TODO: Remove this in the future
commands.SetSubcommandDeprecationHelp(s, []string{"k8s"})
}

// Execute implements commands.MultipleArgumentCommand
func (s *showCommand) Execute(exec commands.Executor, uuid string) (output.Output, error) {
// Deprecating k8s
// TODO: Remove this in the future
commands.SetSubcommandExecutionDeprecationMessage(s, []string{"k8s"}, "uks")

svc := exec.All()
cluster, err := svc.GetKubernetesCluster(exec.Context(), &request.GetKubernetesClusterRequest{UUID: uuid})
if err != nil {
Expand Down
11 changes: 0 additions & 11 deletions internal/commands/kubernetes/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,8 @@ type versionsCommand struct {
*commands.BaseCommand
}

// InitCommand implements Command.InitCommand
func (s *versionsCommand) InitCommand() {
// Deprecating k8s
// TODO: Remove this in the future
commands.SetSubcommandDeprecationHelp(s, []string{"k8s"})
}

// ExecuteWithoutArguments implements commands.NoArgumentCommand
func (s *versionsCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Output, error) {
// Deprecating k8s
// TODO: Remove this in the future
commands.SetSubcommandExecutionDeprecationMessage(s, []string{"k8s"}, "uks")

svc := exec.All()
versions, err := svc.GetKubernetesVersions(exec.Context(), &request.GetKubernetesVersionsRequest{})
if err != nil {
Expand Down
12 changes: 9 additions & 3 deletions internal/commands/loadbalancer/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ import (

// BaseLoadBalancerCommand creates the base "loadbalancer" command
func BaseLoadBalancerCommand() commands.Command {
return &loadbalancerCommand{
commands.New("load-balancer", "Manage load balancers"),
// Initialize the BaseCommand properly
baseCmd := commands.New("load-balancer", "Manage load balancers")
baseCmd.SetDeprecatedAliases([]string{"loadbalancer"})

lbc := &loadbalancerCommand{
BaseCommand: baseCmd,
}

return lbc
}

type loadbalancerCommand struct {
Expand All @@ -21,5 +27,5 @@ func (lb *loadbalancerCommand) InitCommand() {

// Deprecating loadbalancer in favour of load-balancer
// TODO: Remove this in the future
commands.SetDeprecationHelp(lb.Cobra(), []string{"loadbalancer"})
commands.SetDeprecationHelp(lb.Cobra(), lb.DeprecatedAliases())
}
4 changes: 2 additions & 2 deletions internal/commands/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ func IsDeprecatedAliasUsed(deprecatedAlias string) bool {

// PrintDeprecationWarning prints a deprecation message
func PrintDeprecationWarning(deprecatedAlias, newCommand string) {
fmt.Fprintf(os.Stderr, "⚠️ Deprecation Warning: The alias '%s' is deprecated and will be removed in a future release.\n", deprecatedAlias)
fmt.Fprintf(os.Stderr, " Please use '%s' instead.\n", newCommand)
fmt.Fprintf(os.Stderr, "Deprecation Warning: The alias '%s' is deprecated and will be removed in a future release.\n", deprecatedAlias)
fmt.Fprintf(os.Stderr, "Please use '%s' instead.\n", newCommand)
}

func SetSubcommandExecutionDeprecationMessage(cmd Command, deprecatedParentAliases []string, mainParentAlias string) {
Expand Down
Loading