Skip to content

Commit f1f9cb0

Browse files
authored
refactor(cli): avoid importing coderd in slim server (#9483)
This small change removes 11 MB from the slim binary size. Ref: #9380
1 parent 5d7a779 commit f1f9cb0

File tree

6 files changed

+12
-31
lines changed

6 files changed

+12
-31
lines changed

cli/root.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535
"github.com/coder/coder/v2/cli/clibase"
3636
"github.com/coder/coder/v2/cli/cliui"
3737
"github.com/coder/coder/v2/cli/config"
38-
"github.com/coder/coder/v2/coderd"
3938
"github.com/coder/coder/v2/coderd/gitauth"
4039
"github.com/coder/coder/v2/coderd/telemetry"
4140
"github.com/coder/coder/v2/codersdk"
@@ -119,10 +118,7 @@ func (r *RootCmd) Core() []*clibase.Cmd {
119118
}
120119

121120
func (r *RootCmd) AGPL() []*clibase.Cmd {
122-
all := append(r.Core(), r.Server(func(_ context.Context, o *coderd.Options) (*coderd.API, io.Closer, error) {
123-
api := coderd.New(o)
124-
return api, api, nil
125-
}))
121+
all := append(r.Core(), r.Server( /* Do not import coderd here. */ nil))
126122
return all
127123
}
128124

cli/server.go

+7
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,13 @@ func enablePrometheus(
306306
}
307307

308308
func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.API, io.Closer, error)) *clibase.Cmd {
309+
if newAPI == nil {
310+
newAPI = func(_ context.Context, o *coderd.Options) (*coderd.API, io.Closer, error) {
311+
api := coderd.New(o)
312+
return api, api, nil
313+
}
314+
}
315+
309316
var (
310317
vals = new(codersdk.DeploymentValues)
311318
opts = vals.Options()

cli/server_slim.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@
33
package cli
44

55
import (
6-
"context"
76
"fmt"
87
"io"
98
"os"
109

1110
"github.com/coder/coder/v2/cli/clibase"
1211
"github.com/coder/coder/v2/cli/cliui"
13-
"github.com/coder/coder/v2/coderd"
1412
)
1513

16-
func (r *RootCmd) Server(_ func(context.Context, *coderd.Options) (*coderd.API, io.Closer, error)) *clibase.Cmd {
14+
func (r *RootCmd) Server(_ func()) *clibase.Cmd {
1715
root := &clibase.Cmd{
1816
Use: "server",
1917
Short: "Start a Coder server",

enterprise/cli/root.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type RootCmd struct {
1111

1212
func (r *RootCmd) enterpriseOnly() []*clibase.Cmd {
1313
return []*clibase.Cmd{
14-
r.server(),
14+
r.Server(nil),
1515
r.workspaceProxy(),
1616
r.features(),
1717
r.licenses(),

enterprise/cli/server.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import (
2525
agplcoderd "github.com/coder/coder/v2/coderd"
2626
)
2727

28-
func (r *RootCmd) server() *clibase.Cmd {
29-
cmd := r.Server(func(ctx context.Context, options *agplcoderd.Options) (*agplcoderd.API, io.Closer, error) {
28+
func (r *RootCmd) Server(_ func()) *clibase.Cmd {
29+
cmd := r.RootCmd.Server(func(ctx context.Context, options *agplcoderd.Options) (*agplcoderd.API, io.Closer, error) {
3030
if options.DeploymentValues.DERP.Server.RelayURL.String() != "" {
3131
_, err := url.Parse(options.DeploymentValues.DERP.Server.RelayURL.String())
3232
if err != nil {

enterprise/cli/server_slim.go

-20
This file was deleted.

0 commit comments

Comments
 (0)