From 279f4a4aa8eeb422d3657b25d8eb12d7df9df775 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Fri, 1 Sep 2023 12:26:23 +0000 Subject: [PATCH 1/2] refactor(cli): avoid importing coderd in slim server This small change removes 11 MB from the slim binary size. Ref: #9380 --- cli/root.go | 6 +----- cli/server.go | 10 +++++++++- cli/server_slim.go | 4 +--- enterprise/cli/root.go | 2 +- enterprise/cli/server.go | 4 ++-- enterprise/cli/server_slim.go | 20 -------------------- 6 files changed, 14 insertions(+), 32 deletions(-) delete mode 100644 enterprise/cli/server_slim.go diff --git a/cli/root.go b/cli/root.go index 3ab2f0d7f33b9..36073d3579816 100644 --- a/cli/root.go +++ b/cli/root.go @@ -35,7 +35,6 @@ import ( "github.com/coder/coder/v2/cli/clibase" "github.com/coder/coder/v2/cli/cliui" "github.com/coder/coder/v2/cli/config" - "github.com/coder/coder/v2/coderd" "github.com/coder/coder/v2/coderd/gitauth" "github.com/coder/coder/v2/coderd/telemetry" "github.com/coder/coder/v2/codersdk" @@ -119,10 +118,7 @@ func (r *RootCmd) Core() []*clibase.Cmd { } func (r *RootCmd) AGPL() []*clibase.Cmd { - all := append(r.Core(), r.Server(func(_ context.Context, o *coderd.Options) (*coderd.API, io.Closer, error) { - api := coderd.New(o) - return api, api, nil - })) + all := append(r.Core(), r.Server( /* Do not import coderd here. */ )) return all } diff --git a/cli/server.go b/cli/server.go index 66cc22086418c..a13ad55cf9260 100644 --- a/cli/server.go +++ b/cli/server.go @@ -305,7 +305,15 @@ func enablePrometheus( ), nil } -func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.API, io.Closer, error)) *clibase.Cmd { +func (r *RootCmd) Server(optionalNewAPI ...func(context.Context, *coderd.Options) (*coderd.API, io.Closer, error)) *clibase.Cmd { + if len(optionalNewAPI) == 0 { + optionalNewAPI = append(optionalNewAPI, func(_ context.Context, o *coderd.Options) (*coderd.API, io.Closer, error) { + api := coderd.New(o) + return api, api, nil + }) + } + newAPI := optionalNewAPI[0] + var ( vals = new(codersdk.DeploymentValues) opts = vals.Options() diff --git a/cli/server_slim.go b/cli/server_slim.go index f5f17794f3f56..41ce21fca224e 100644 --- a/cli/server_slim.go +++ b/cli/server_slim.go @@ -3,17 +3,15 @@ package cli import ( - "context" "fmt" "io" "os" "github.com/coder/coder/v2/cli/clibase" "github.com/coder/coder/v2/cli/cliui" - "github.com/coder/coder/v2/coderd" ) -func (r *RootCmd) Server(_ func(context.Context, *coderd.Options) (*coderd.API, io.Closer, error)) *clibase.Cmd { +func (r *RootCmd) Server() *clibase.Cmd { root := &clibase.Cmd{ Use: "server", Short: "Start a Coder server", diff --git a/enterprise/cli/root.go b/enterprise/cli/root.go index 9f7bfb9039683..993b2a0a39b44 100644 --- a/enterprise/cli/root.go +++ b/enterprise/cli/root.go @@ -11,7 +11,7 @@ type RootCmd struct { func (r *RootCmd) enterpriseOnly() []*clibase.Cmd { return []*clibase.Cmd{ - r.server(), + r.Server(), r.workspaceProxy(), r.features(), r.licenses(), diff --git a/enterprise/cli/server.go b/enterprise/cli/server.go index 3801d2208f3a6..83bcc53f85724 100644 --- a/enterprise/cli/server.go +++ b/enterprise/cli/server.go @@ -25,8 +25,8 @@ import ( agplcoderd "github.com/coder/coder/v2/coderd" ) -func (r *RootCmd) server() *clibase.Cmd { - cmd := r.Server(func(ctx context.Context, options *agplcoderd.Options) (*agplcoderd.API, io.Closer, error) { +func (r *RootCmd) Server() *clibase.Cmd { + cmd := r.RootCmd.Server(func(ctx context.Context, options *agplcoderd.Options) (*agplcoderd.API, io.Closer, error) { if options.DeploymentValues.DERP.Server.RelayURL.String() != "" { _, err := url.Parse(options.DeploymentValues.DERP.Server.RelayURL.String()) if err != nil { diff --git a/enterprise/cli/server_slim.go b/enterprise/cli/server_slim.go deleted file mode 100644 index a0e9800ed760f..0000000000000 --- a/enterprise/cli/server_slim.go +++ /dev/null @@ -1,20 +0,0 @@ -//go:build slim - -package cli - -import ( - "context" - "io" - - "golang.org/x/xerrors" - - "github.com/coder/coder/v2/cli/clibase" - agplcoderd "github.com/coder/coder/v2/coderd" -) - -func (r *RootCmd) server() *clibase.Cmd { - cmd := r.Server(func(ctx context.Context, options *agplcoderd.Options) (*agplcoderd.API, io.Closer, error) { - return nil, nil, xerrors.Errorf("slim build does not support `coder server`") - }) - return cmd -} From 28324ae13c383537dde9cca6c5b75a88928213ef Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Fri, 1 Sep 2023 12:57:45 +0000 Subject: [PATCH 2/2] use clearer args without vararg --- cli/root.go | 2 +- cli/server.go | 9 ++++----- cli/server_slim.go | 2 +- enterprise/cli/root.go | 2 +- enterprise/cli/server.go | 2 +- 5 files changed, 8 insertions(+), 9 deletions(-) diff --git a/cli/root.go b/cli/root.go index 36073d3579816..3d0daafbfa7e9 100644 --- a/cli/root.go +++ b/cli/root.go @@ -118,7 +118,7 @@ func (r *RootCmd) Core() []*clibase.Cmd { } func (r *RootCmd) AGPL() []*clibase.Cmd { - all := append(r.Core(), r.Server( /* Do not import coderd here. */ )) + all := append(r.Core(), r.Server( /* Do not import coderd here. */ nil)) return all } diff --git a/cli/server.go b/cli/server.go index a13ad55cf9260..683d8aec58fb9 100644 --- a/cli/server.go +++ b/cli/server.go @@ -305,14 +305,13 @@ func enablePrometheus( ), nil } -func (r *RootCmd) Server(optionalNewAPI ...func(context.Context, *coderd.Options) (*coderd.API, io.Closer, error)) *clibase.Cmd { - if len(optionalNewAPI) == 0 { - optionalNewAPI = append(optionalNewAPI, func(_ context.Context, o *coderd.Options) (*coderd.API, io.Closer, error) { +func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.API, io.Closer, error)) *clibase.Cmd { + if newAPI == nil { + newAPI = func(_ context.Context, o *coderd.Options) (*coderd.API, io.Closer, error) { api := coderd.New(o) return api, api, nil - }) + } } - newAPI := optionalNewAPI[0] var ( vals = new(codersdk.DeploymentValues) diff --git a/cli/server_slim.go b/cli/server_slim.go index 41ce21fca224e..fadb703cc6466 100644 --- a/cli/server_slim.go +++ b/cli/server_slim.go @@ -11,7 +11,7 @@ import ( "github.com/coder/coder/v2/cli/cliui" ) -func (r *RootCmd) Server() *clibase.Cmd { +func (r *RootCmd) Server(_ func()) *clibase.Cmd { root := &clibase.Cmd{ Use: "server", Short: "Start a Coder server", diff --git a/enterprise/cli/root.go b/enterprise/cli/root.go index 993b2a0a39b44..360582a3e5193 100644 --- a/enterprise/cli/root.go +++ b/enterprise/cli/root.go @@ -11,7 +11,7 @@ type RootCmd struct { func (r *RootCmd) enterpriseOnly() []*clibase.Cmd { return []*clibase.Cmd{ - r.Server(), + r.Server(nil), r.workspaceProxy(), r.features(), r.licenses(), diff --git a/enterprise/cli/server.go b/enterprise/cli/server.go index 83bcc53f85724..021b66f2f7a60 100644 --- a/enterprise/cli/server.go +++ b/enterprise/cli/server.go @@ -25,7 +25,7 @@ import ( agplcoderd "github.com/coder/coder/v2/coderd" ) -func (r *RootCmd) Server() *clibase.Cmd { +func (r *RootCmd) Server(_ func()) *clibase.Cmd { cmd := r.RootCmd.Server(func(ctx context.Context, options *agplcoderd.Options) (*agplcoderd.API, io.Closer, error) { if options.DeploymentValues.DERP.Server.RelayURL.String() != "" { _, err := url.Parse(options.DeploymentValues.DERP.Server.RelayURL.String())