Skip to content

chore(cli): use option source name for deprecation warnings #15581

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

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions cli/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func (r *RootCmd) restart() *serpent.Command {
Short: "Restart a workspace",
Middleware: serpent.Chain(
serpent.RequireNArgs(1),
PrintDeprecatedOptions(),
r.InitClient(client),
),
Options: serpent.OptionSet{cliui.SkipPromptOption()},
Expand Down
50 changes: 50 additions & 0 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -1307,3 +1307,53 @@ func headerTransport(ctx context.Context, serverURL *url.URL, header []string, h
}
return transport, nil
}

// printDeprecatedOptions loops through all command options, and prints
// a warning for usage of deprecated options.
func PrintDeprecatedOptions() serpent.MiddlewareFunc {
return func(next serpent.HandlerFunc) serpent.HandlerFunc {
return func(inv *serpent.Invocation) error {
opts := inv.Command.Options
// Print deprecation warnings.
for _, opt := range opts {
if opt.UseInstead == nil {
continue
}

if opt.ValueSource == serpent.ValueSourceNone || opt.ValueSource == serpent.ValueSourceDefault {
continue
}

warnStr := translateSource(opt.ValueSource, opt) + " is deprecated, please use "
for i, use := range opt.UseInstead {
warnStr += translateSource(opt.ValueSource, use) + " "
if i != len(opt.UseInstead)-1 {
warnStr += "and "
}
}
warnStr += "instead.\n"

cliui.Warn(inv.Stderr,
warnStr,
)
}

return next(inv)
}
}
}

// translateSource provides the name of the source of the option, depending on the
// supplied target ValueSource.
func translateSource(target serpent.ValueSource, opt serpent.Option) string {
switch target {
case serpent.ValueSourceFlag:
return fmt.Sprintf("`--%s`", opt.Flag)
case serpent.ValueSourceEnv:
return fmt.Sprintf("`%s`", opt.Env)
case serpent.ValueSourceYAML:
return fmt.Sprintf("`%s`", opt.YAML)
default:
return opt.Name
}
}
35 changes: 0 additions & 35 deletions cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1240,41 +1240,6 @@ func templateHelpers(options *coderd.Options) map[string]any {
}
}

// printDeprecatedOptions loops through all command options, and prints
// a warning for usage of deprecated options.
func PrintDeprecatedOptions() serpent.MiddlewareFunc {
return func(next serpent.HandlerFunc) serpent.HandlerFunc {
return func(inv *serpent.Invocation) error {
opts := inv.Command.Options
// Print deprecation warnings.
for _, opt := range opts {
if opt.UseInstead == nil {
continue
}

if opt.ValueSource == serpent.ValueSourceNone || opt.ValueSource == serpent.ValueSourceDefault {
continue
}

warnStr := opt.Name + " is deprecated, please use "
for i, use := range opt.UseInstead {
warnStr += use.Name + " "
if i != len(opt.UseInstead)-1 {
warnStr += "and "
}
}
warnStr += "instead.\n"

cliui.Warn(inv.Stderr,
warnStr,
)
}

return next(inv)
}
}
}

// writeConfigMW will prevent the main command from running if the write-config
// flag is set. Instead, it will marshal the command options to YAML and write
// them to stdout.
Expand Down
1 change: 1 addition & 0 deletions cli/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func (r *RootCmd) ssh() *serpent.Command {
Middleware: serpent.Chain(
serpent.RequireNArgs(1),
r.InitClient(client),
PrintDeprecatedOptions(),
initAppearance(client, &appearanceConfig),
),
Handler: func(inv *serpent.Invocation) (retErr error) {
Expand Down
1 change: 1 addition & 0 deletions cli/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func (r *RootCmd) start() *serpent.Command {
Short: "Start a workspace",
Middleware: serpent.Chain(
serpent.RequireNArgs(1),
PrintDeprecatedOptions(),
r.InitClient(client),
),
Options: serpent.OptionSet{cliui.SkipPromptOption()},
Expand Down
1 change: 1 addition & 0 deletions cli/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func (r *RootCmd) update() *serpent.Command {
Long: "Use --always-prompt to change the parameter values of the workspace.",
Middleware: serpent.Chain(
serpent.RequireNArgs(1),
PrintDeprecatedOptions(),
r.InitClient(client),
),
Handler: func(inv *serpent.Invocation) error {
Expand Down
1 change: 1 addition & 0 deletions enterprise/cli/provisionerdaemonstart.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func (r *RootCmd) provisionerDaemonStart() *serpent.Command {
Use: "start",
Short: "Run a provisioner daemon",
Middleware: serpent.Chain(
agpl.PrintDeprecatedOptions(),
// disable checks and warnings because this command starts a daemon; it is
// not meant for humans typing commands. Furthermore, the checks are
// incompatible with PSK auth that this command uses
Expand Down
Loading