Skip to content

Commit 7f92bc3

Browse files
chore: consistent aliases (#378)
Co-authored-by: Toni Kangas <kangasta@users.noreply.github.com>
1 parent f420a92 commit 7f92bc3

File tree

17 files changed

+156
-59
lines changed

17 files changed

+156
-59
lines changed

.ci/docs/md.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"sort"
1111
"strings"
1212

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

123-
if len(cmd.Aliases) > 0 {
124-
buf.WriteString("## Aliases\n\n")
125-
buf.WriteString(fmt.Sprintf("%s\n\n", formatFlags(cmd.Aliases)))
124+
ctx := cmd.Context()
125+
if ctx != nil {
126+
if wrapper := ctx.Value("command"); wrapper != nil {
127+
if c, ok := wrapper.(*commands.BaseCommand); ok {
128+
if aliases := c.Aliases(); len(aliases) > 0 {
129+
buf.WriteString("## Aliases\n\n")
130+
buf.WriteString(fmt.Sprintf("%s\n\n", formatFlags(aliases)))
131+
}
132+
}
133+
}
126134
}
127135

128136
if len(cmd.Example) > 0 {

CHANGELOG.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- Add termination_protection to upctl database show output
1414
- Experimental support for token authentication by defining token in `UPCLOUD_TOKEN` environment variable.
1515
- Experimental support for managing tokens with `account token` commands.
16+
- New command names and aliases added to improve consistency:
17+
- Commands:
18+
- load-balancer
19+
- network-peering
20+
- object-storage
21+
- server-group
22+
- Aliases:
23+
- account: acc
24+
- gateway: gw
25+
- network-peering: np
26+
- object-storage: obs
27+
- partner: pr
28+
- router: rt
29+
- server: srv
30+
- server-group: sg
31+
- storage: st
1632

1733
### Changed
1834

@@ -22,6 +38,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2238

2339
- Prevent filename completion of flags that don't take filename args.
2440

41+
### Deprecated
42+
- Deprecation of some commands and aliases ( new command names added to improve consistency )
43+
- Deprecated commands:
44+
- loadbalancer
45+
- networkpeering
46+
- objectstorage
47+
- servergroup
48+
- Deprecated aliases:
49+
- object-storage: objsto
50+
51+
2552
## [3.14.0] - 2025-01-08
2653

2754
### Added

internal/commands/command.go

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package commands
22

33
import (
4+
"context"
45
"fmt"
56
"strings"
67

@@ -13,15 +14,28 @@ import (
1314
"github.com/spf13/pflag"
1415
)
1516

17+
type CommandContextKey string
18+
19+
const commandKey CommandContextKey = "command"
20+
1621
// New returns a BaseCommand that implements Command. It is used as a base to create custom commands from.
1722
func New(name, usage string, examples ...string) *BaseCommand {
18-
return &BaseCommand{
19-
cobra: &cobra.Command{
20-
Use: name,
21-
Short: usage,
22-
Example: strings.Join(examples, "\n"),
23-
},
23+
cmd := &cobra.Command{
24+
Use: name,
25+
Short: usage,
26+
Example: strings.Join(examples, "\n"),
2427
}
28+
29+
// Initialize BaseCommand
30+
baseCmd := &BaseCommand{
31+
cobra: cmd,
32+
}
33+
34+
// Store reference to itself in the context - We need this to access the command in the CobraCommand interface
35+
// Specifically to generate the reference documentation
36+
cmd.SetContext(context.WithValue(context.Background(), commandKey, baseCmd))
37+
38+
return baseCmd
2539
}
2640

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

126140
// BaseCommand is the base type for all commands, implementing Command
127141
type BaseCommand struct {
128-
cobra *cobra.Command
142+
cobra *cobra.Command
143+
deprecatedAliases []string
144+
}
145+
146+
// Aliases return non deprecated aliases
147+
func (s *BaseCommand) Aliases() []string {
148+
// Get all aliases from Cobra
149+
allAliases := s.cobra.Aliases
150+
151+
// Filter out deprecated aliases
152+
var filteredAliases []string
153+
for _, alias := range allAliases {
154+
if !s.isDeprecatedAlias(alias) {
155+
filteredAliases = append(filteredAliases, alias)
156+
}
157+
}
158+
159+
return filteredAliases
160+
}
161+
162+
// isDeprecatedAlias checks if an alias is deprecated
163+
func (s *BaseCommand) isDeprecatedAlias(alias string) bool {
164+
for _, deprecated := range s.deprecatedAliases {
165+
if alias == deprecated {
166+
return true
167+
}
168+
}
169+
return false
170+
}
171+
172+
func (s *BaseCommand) DeprecatedAliases() []string {
173+
return s.deprecatedAliases
174+
}
175+
176+
func (s *BaseCommand) SetDeprecatedAliases(aliases []string) {
177+
s.deprecatedAliases = aliases
129178
}
130179

131180
// MaximumExecutions return the max executed workers

internal/commands/kubernetes/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ func (c *configCommand) InitCommand() {
4747
"Absolute path for writing output. If the file exists, the config will be merged.")
4848
c.AddFlags(flagSet)
4949

50-
// Deprecating k8s
50+
// Deprecating uks in favor of k8s
5151
// TODO: Remove this in the future
52-
commands.SetSubcommandDeprecationHelp(c, []string{"k8s"})
52+
commands.SetSubcommandDeprecationHelp(c, []string{"uks"})
5353
}
5454

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

internal/commands/kubernetes/create.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,9 @@ func (c *createCommand) InitCommand() {
153153
commands.Must(c.Cobra().MarkFlagRequired("zone"))
154154
commands.Must(c.Cobra().RegisterFlagCompletionFunc("name", cobra.NoFileCompletions))
155155

156-
// Deprecating k8s
156+
// Deprecating uks in favor of k8s
157157
// TODO: Remove this in the future
158-
commands.SetSubcommandDeprecationHelp(c, []string{"k8s"})
158+
commands.SetSubcommandDeprecationHelp(c, []string{"uks"})
159159
}
160160

161161
func (c *createCommand) InitCommandWithConfig(cfg *config.Config) {
@@ -166,7 +166,7 @@ func (c *createCommand) InitCommandWithConfig(cfg *config.Config) {
166166

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

internal/commands/kubernetes/delete.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,21 @@ func DeleteCommand() commands.Command {
2323
}
2424
}
2525

26+
func (s *deleteCommand) InitCommand() {
27+
// Deprecating uks
28+
// TODO: Remove this in the future
29+
commands.SetSubcommandDeprecationHelp(s, []string{"uks"})
30+
}
31+
2632
type deleteCommand struct {
2733
*commands.BaseCommand
2834
resolver.CachingKubernetes
2935
completion.Kubernetes
3036
}
3137

32-
// InitCommand implements Command.InitCommand
33-
func (s *deleteCommand) InitCommand() {
34-
// Deprecating k8s
35-
// TODO: Remove this in the future
36-
commands.SetSubcommandDeprecationHelp(s, []string{"k8s"})
37-
}
38-
3938
// Execute implements commands.MultipleArgumentCommand
4039
func (s *deleteCommand) Execute(exec commands.Executor, arg string) (output.Output, error) {
41-
// Deprecating k8s
40+
// Deprecating uks
4241
// TODO: Remove this in the future
4342
commands.SetSubcommandExecutionDeprecationMessage(s, []string{"k8s"}, "uks")
4443

internal/commands/kubernetes/kubernetes.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ import (
66

77
// BaseKubernetesCommand creates the base "kubernetes" command
88
func BaseKubernetesCommand() commands.Command {
9+
baseCmd := commands.New("kubernetes", "Manage Kubernetes clusters")
10+
baseCmd.SetDeprecatedAliases([]string{"uks"})
11+
912
return &kubernetesCommand{
10-
commands.New("kubernetes", "Manage Kubernetes clusters"),
13+
BaseCommand: baseCmd,
1114
}
1215
}
1316

@@ -18,7 +21,7 @@ type kubernetesCommand struct {
1821
// InitCommand implements Command.InitCommand
1922
func (k *kubernetesCommand) InitCommand() {
2023
k.Cobra().Aliases = []string{"k8s", "uks"}
21-
// Deprecating k8s
24+
2225
// TODO: Remove this in the future
23-
commands.SetDeprecationHelp(k.Cobra(), []string{"k8s"})
26+
commands.SetDeprecationHelp(k.Cobra(), k.DeprecatedAliases())
2427
}

internal/commands/kubernetes/list.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,15 @@ type listCommand struct {
2020
*commands.BaseCommand
2121
}
2222

23-
// InitCommand implements Command.InitCommand
2423
func (s *listCommand) InitCommand() {
25-
// Deprecating k8s
24+
// Deprecating uks
2625
// TODO: Remove this in the future
27-
commands.SetSubcommandDeprecationHelp(s, []string{"k8s"})
26+
commands.SetSubcommandDeprecationHelp(s, []string{"uks"})
2827
}
2928

3029
// ExecuteWithoutArguments implements commands.NoArgumentCommand
3130
func (s *listCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Output, error) {
32-
// Deprecating k8s
31+
// Deprecating uks
3332
// TODO: Remove this in the future
3433
commands.SetSubcommandExecutionDeprecationMessage(s, []string{"k8s"}, "uks")
3534

internal/commands/kubernetes/modify.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ type modifyCommand struct {
3939

4040
// InitCommand implements Command.InitCommand
4141
func (c *modifyCommand) InitCommand() {
42+
// Deprecating uks
43+
// TODO: Remove this in the future
44+
commands.SetSubcommandDeprecationHelp(c, []string{"uks"})
45+
4246
fs := &pflag.FlagSet{}
4347
fs.StringArrayVar(
4448
&c.controlPlaneIPFilter,
@@ -52,15 +56,11 @@ func (c *modifyCommand) InitCommand() {
5256
c.AddFlags(fs)
5357
c.Cobra().MarkFlagsMutuallyExclusive("label", "clear-labels")
5458
commands.Must(c.Cobra().RegisterFlagCompletionFunc("label", cobra.NoFileCompletions))
55-
56-
// Deprecating k8s
57-
// TODO: Remove this in the future
58-
commands.SetSubcommandDeprecationHelp(c, []string{"k8s"})
5959
}
6060

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

internal/commands/kubernetes/plans.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@ type plansCommand struct {
1717
*commands.BaseCommand
1818
}
1919

20-
// InitCommand implements Command.InitCommand
2120
func (s *plansCommand) InitCommand() {
22-
// Deprecating k8s
21+
// Deprecating uks
2322
// TODO: Remove this in the future
24-
commands.SetSubcommandDeprecationHelp(s, []string{"k8s"})
23+
commands.SetSubcommandDeprecationHelp(s, []string{"uks"})
2524
}
2625

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

0 commit comments

Comments
 (0)