From eff4015eb06a6e96653b183936d2e32f59106584 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Tue, 11 Apr 2023 14:23:29 -0500 Subject: [PATCH 1/2] chore: Move writeConfig to a cli middleware Trying to reduce the server LoC, it's very long and unwieldy. The workspace proxy code needs to replicate, and trying to reuse as much as possible --- cli/server.go | 47 ++++++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/cli/server.go b/cli/server.go index b625083bfc3e0..170f551a08357 100644 --- a/cli/server.go +++ b/cli/server.go @@ -168,31 +168,13 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd. Use: "server", Short: "Start a Coder server", Options: opts, - Middleware: clibase.RequireNArgs(0), + Middleware: clibase.Chain(writeConfigMW(cfg), clibase.RequireNArgs(0)), Handler: func(inv *clibase.Invocation) error { // Main command context for managing cancellation of running // services. ctx, cancel := context.WithCancel(inv.Context()) defer cancel() - if cfg.WriteConfig { - n, err := opts.MarshalYAML() - if err != nil { - return xerrors.Errorf("generate yaml: %w", err) - } - enc := yaml.NewEncoder(inv.Stdout) - enc.SetIndent(2) - err = enc.Encode(n) - if err != nil { - return xerrors.Errorf("encode yaml: %w", err) - } - err = enc.Close() - if err != nil { - return xerrors.Errorf("close yaml encoder: %w", err) - } - return nil - } - if cfg.Config != "" { cliui.Warnf(inv.Stderr, "YAML support is experimental and offers no compatibility guarantees.") } @@ -1222,6 +1204,33 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd. return serverCmd } +func writeConfigMW(cfg *codersdk.DeploymentValues) clibase.MiddlewareFunc { + return func(next clibase.HandlerFunc) clibase.HandlerFunc { + return func(inv *clibase.Invocation) error { + if !cfg.WriteConfig { + return next(inv) + } + + opts := inv.Command.Options + n, err := opts.MarshalYAML() + if err != nil { + return xerrors.Errorf("generate yaml: %w", err) + } + enc := yaml.NewEncoder(inv.Stdout) + enc.SetIndent(2) + err = enc.Encode(n) + if err != nil { + return xerrors.Errorf("encode yaml: %w", err) + } + err = enc.Close() + if err != nil { + return xerrors.Errorf("close yaml encoder: %w", err) + } + return nil + } + } +} + // isLocalURL returns true if the hostname of the provided URL appears to // resolve to a loopback address. func isLocalURL(ctx context.Context, u *url.URL) (bool, error) { From 00805d14b10a192d9db04abbe34da2f80c6d8231 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Tue, 11 Apr 2023 14:29:17 -0500 Subject: [PATCH 2/2] Move deprecation warnings too --- cli/server.go | 74 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 28 deletions(-) diff --git a/cli/server.go b/cli/server.go index 170f551a08357..3726a17a1399a 100644 --- a/cli/server.go +++ b/cli/server.go @@ -165,10 +165,14 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd. opts = cfg.Options() ) serverCmd := &clibase.Cmd{ - Use: "server", - Short: "Start a Coder server", - Options: opts, - Middleware: clibase.Chain(writeConfigMW(cfg), clibase.RequireNArgs(0)), + Use: "server", + Short: "Start a Coder server", + Options: opts, + Middleware: clibase.Chain( + writeConfigMW(cfg), + printDeprecatedOptions(), + clibase.RequireNArgs(0), + ), Handler: func(inv *clibase.Invocation) error { // Main command context for managing cancellation of running // services. @@ -179,30 +183,6 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd. cliui.Warnf(inv.Stderr, "YAML support is experimental and offers no compatibility guarantees.") } - // Print deprecation warnings. - for _, opt := range opts { - if opt.UseInstead == nil { - continue - } - - if opt.Value.String() == opt.Default { - 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, - ) - } - go dumpHandler(ctx) // Validate bind addresses. @@ -1204,6 +1184,44 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd. return serverCmd } +// printDeprecatedOptions loops through all command options, and prints +// a warning for usage of deprecated options. +func printDeprecatedOptions() clibase.MiddlewareFunc { + return func(next clibase.HandlerFunc) clibase.HandlerFunc { + return func(inv *clibase.Invocation) error { + opts := inv.Command.Options + // Print deprecation warnings. + for _, opt := range opts { + if opt.UseInstead == nil { + continue + } + + if opt.Value.String() == opt.Default { + 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. func writeConfigMW(cfg *codersdk.DeploymentValues) clibase.MiddlewareFunc { return func(next clibase.HandlerFunc) clibase.HandlerFunc { return func(inv *clibase.Invocation) error {