Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8bb5eb6

Browse files
committed
refactor, remove control flag
1 parent44cc9a6 commit8bb5eb6

File tree

4 files changed

+34
-42
lines changed

4 files changed

+34
-42
lines changed

‎agent/agentcontainers/api.go

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,7 @@ func WithWatcher(w watcher.Watcher) Option {
117117
}
118118

119119
// NewAPI returns a new API with the given options applied.
120-
//
121-
//nolint:revive // experimentalDevcontainersEnabled is a control flag.
122-
funcNewAPI(logger slog.Logger,experimentalDevcontainersEnabledbool,options...Option)*API {
120+
funcNewAPI(logger slog.Logger,options...Option)*API {
123121
ctx,cancel:=context.WithCancel(context.Background())
124122
api:=&API{
125123
ctx:ctx,
@@ -133,34 +131,23 @@ func NewAPI(logger slog.Logger, experimentalDevcontainersEnabled bool, options .
133131
devcontainerNames:make(map[string]struct{}),
134132
knownDevcontainers: []codersdk.WorkspaceAgentDevcontainer{},
135133
configFileModifiedTimes:make(map[string]time.Time),
136-
137-
experimentalDevcontainersEnabled:experimentalDevcontainersEnabled,
138134
}
139135
for_,opt:=rangeoptions {
140136
opt(api)
141137
}
142-
ifapi.experimentalDevcontainersEnabled {
143-
ifapi.cl==nil {
144-
api.cl=NewDocker(api.execer)
145-
}
146-
ifapi.dccli==nil {
147-
api.dccli=NewDevcontainerCLI(logger.Named("devcontainer-cli"),api.execer)
148-
}
149-
ifapi.watcher==nil {
150-
varerrerror
151-
api.watcher,err=watcher.NewFSNotify()
152-
iferr!=nil {
153-
logger.Error(ctx,"create file watcher service failed",slog.Error(err))
154-
api.watcher=watcher.NewNoop()
155-
}
156-
}
157-
}else {
158-
ifapi.cl!=nil||api.dccli!=nil||api.watcher!=nil {
159-
logger.Warn(ctx,"devcontainers are disabled but API is configured with devcontainer services")
138+
ifapi.cl==nil {
139+
api.cl=NewDocker(api.execer)
140+
}
141+
ifapi.dccli==nil {
142+
api.dccli=NewDevcontainerCLI(logger.Named("devcontainer-cli"),api.execer)
143+
}
144+
ifapi.watcher==nil {
145+
varerrerror
146+
api.watcher,err=watcher.NewFSNotify()
147+
iferr!=nil {
148+
logger.Error(ctx,"create file watcher service failed",slog.Error(err))
149+
api.watcher=watcher.NewNoop()
160150
}
161-
api.cl=&NoopLister{}
162-
api.dccli=&noopDevcontainerCLI{}
163-
api.watcher=watcher.NewNoop()
164151
}
165152

166153
// Make sure we watch the devcontainer config files for changes.
@@ -219,14 +206,6 @@ func (api *API) start() {
219206
func (api*API)Routes() http.Handler {
220207
r:=chi.NewRouter()
221208

222-
if!api.experimentalDevcontainersEnabled {
223-
r.Get("/",api.handleDisabledEmptyList)
224-
r.Get("/devcontainers",api.handleDisabled)
225-
r.Post("/{id}/recreate",api.handleDisabled)
226-
227-
returnr
228-
}
229-
230209
r.Get("/",api.handleList)
231210
r.Get("/devcontainers",api.handleListDevcontainers)
232211
r.Post("/{id}/recreate",api.handleRecreate)

‎agent/agentcontainers/api_internal_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func TestAPI(t *testing.T) {
101101
mockLister=acmock.NewMockLister(ctrl)
102102
now=time.Now().UTC()
103103
logger=slogtest.Make(t,nil).Leveled(slog.LevelDebug)
104-
api=NewAPI(logger,true,WithLister(mockLister))
104+
api=NewAPI(logger,WithLister(mockLister))
105105
)
106106
deferapi.Close()
107107

‎agent/agentcontainers/api_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,6 @@ func TestAPI(t *testing.T) {
251251
r:=chi.NewRouter()
252252
api:=agentcontainers.NewAPI(
253253
logger,
254-
true,
255254
agentcontainers.WithLister(tt.lister),
256255
agentcontainers.WithDevcontainerCLI(tt.devcontainerCLI),
257256
)
@@ -565,7 +564,7 @@ func TestAPI(t *testing.T) {
565564
apiOptions=append(apiOptions,agentcontainers.WithDevcontainers(tt.knownDevcontainers))
566565
}
567566

568-
api:=agentcontainers.NewAPI(logger,true,apiOptions...)
567+
api:=agentcontainers.NewAPI(logger,apiOptions...)
569568
deferapi.Close()
570569
r.Mount("/",api.Routes())
571570

@@ -626,7 +625,6 @@ func TestAPI(t *testing.T) {
626625
logger:=slogtest.Make(t,nil).Leveled(slog.LevelDebug)
627626
api:=agentcontainers.NewAPI(
628627
logger,
629-
true,
630628
agentcontainers.WithLister(fLister),
631629
agentcontainers.WithWatcher(fWatcher),
632630
agentcontainers.WithClock(mClock),

‎agent/api.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,33 @@ func (a *agent) apiHandler() (http.Handler, func() error) {
3737
cacheDuration:cacheDuration,
3838
}
3939

40-
varcontainerAPIOpts []agentcontainers.Option
40+
varcontainerAPI*agentcontainers.API
4141
ifa.experimentalDevcontainersEnabled {
42+
containerAPIOpts:= []agentcontainers.Option{
43+
agentcontainers.WithExecer(a.execer),
44+
}
4245
manifest:=a.manifest.Load()
4346
ifmanifest!=nil&&len(manifest.Devcontainers)>0 {
4447
containerAPIOpts=append(
4548
containerAPIOpts,
4649
agentcontainers.WithDevcontainers(manifest.Devcontainers),
4750
)
4851
}
52+
53+
containerAPI=agentcontainers.NewAPI(a.logger.Named("containers"),containerAPIOpts...)
54+
55+
r.Mount("/api/v0/containers",containerAPI.Routes())
56+
}else {
57+
r.HandleFunc("/api/v0/containers",func(w http.ResponseWriter,r*http.Request) {
58+
httpapi.Write(r.Context(),w,http.StatusNotFound, codersdk.Response{
59+
Message:"Container API is not enabled.",
60+
Detail:"Set the --experimental-devcontainers flag to enable the container API.",
61+
})
62+
})
4963
}
50-
containerAPI:=agentcontainers.NewAPI(a.logger.Named("containers"),a.experimentalDevcontainersEnabled,containerAPIOpts...)
5164

5265
promHandler:=PrometheusMetricsHandler(a.prometheusRegistry,a.logger)
5366

54-
r.Mount("/api/v0/containers",containerAPI.Routes())
5567
r.Get("/api/v0/listening-ports",lp.handler)
5668
r.Get("/api/v0/netcheck",a.HandleNetcheck)
5769
r.Post("/api/v0/list-directory",a.HandleLS)
@@ -62,7 +74,10 @@ func (a *agent) apiHandler() (http.Handler, func() error) {
6274
r.Get("/debug/prometheus",promHandler.ServeHTTP)
6375

6476
returnr,func()error {
65-
returncontainerAPI.Close()
77+
ifcontainerAPI!=nil {
78+
returncontainerAPI.Close()
79+
}
80+
returnnil
6681
}
6782
}
6883

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp