Skip to content

Commit d85e9a2

Browse files
committed
refactor
1 parent e1048b1 commit d85e9a2

File tree

2 files changed

+46
-40
lines changed

2 files changed

+46
-40
lines changed

agent/agent.go

+8-40
Original file line numberDiff line numberDiff line change
@@ -1116,38 +1116,19 @@ func (a *agent) handleManifest(manifestOK *checkpoint) func(ctx context.Context,
11161116
}
11171117

11181118
var (
1119-
// Clone the scripts so we can remove the devcontainer scripts.
1120-
scripts = slices.Clone(manifest.Scripts)
1119+
scripts = manifest.Scripts
1120+
scriptRunnerOpts []agentscripts.InitOption
1121+
)
1122+
if a.experimentalDevcontainersEnabled {
1123+
var dcScripts []codersdk.WorkspaceAgentScript
1124+
scripts, dcScripts = agentcontainers.ExtractDevcontainerScripts(a.logger, expandPathToAbs, manifest.Devcontainers, scripts)
11211125
// The post-start scripts are used to autostart Dev Containers
11221126
// after the start scripts have completed. This is necessary
11231127
// because the Dev Container may depend on the workspace being
11241128
// initialized (git clone, etc).
1125-
postStartScripts []codersdk.WorkspaceAgentScript
1126-
)
1127-
if a.experimentalDevcontainersEnabled {
1128-
for _, dc := range manifest.Devcontainers {
1129-
// TODO(mafredri): Verify `@devcontainers/cli` presence.
1130-
// TODO(mafredri): Verify workspace folder exists.
1131-
// TODO(mafredri): If set, verify config path exists.
1132-
dc = expandDevcontainerPaths(a.logger, dc)
1133-
1134-
for i, s := range scripts {
1135-
// The devcontainer scripts match the devcontainer ID for
1136-
// identification.
1137-
if s.ID == dc.ID {
1138-
scripts = slices.Delete(scripts, i, i+1)
1139-
postStartScripts = append(postStartScripts, agentcontainers.DevcontainerStartupScript(dc, s))
1140-
break
1141-
}
1142-
}
1143-
}
1129+
scriptRunnerOpts = append(scriptRunnerOpts, agentscripts.WithPostStartScripts(dcScripts...))
11441130
}
1145-
1146-
err = a.scriptRunner.Init(
1147-
manifest.Scripts,
1148-
aAPI.ScriptCompleted,
1149-
agentscripts.WithPostStartScripts(postStartScripts...),
1150-
)
1131+
err = a.scriptRunner.Init(scripts, aAPI.ScriptCompleted, scriptRunnerOpts...)
11511132
if err != nil {
11521133
return xerrors.Errorf("init script runner: %w", err)
11531134
}
@@ -1911,19 +1892,6 @@ func expandPathToAbs(path string) (string, error) {
19111892
return path, nil
19121893
}
19131894

1914-
func expandDevcontainerPaths(logger slog.Logger, dc codersdk.WorkspaceAgentDevcontainer) codersdk.WorkspaceAgentDevcontainer {
1915-
var err error
1916-
if dc.WorkspaceFolder, err = expandPathToAbs(dc.WorkspaceFolder); err != nil {
1917-
logger.Warn(context.Background(), "expand devcontainer workspace folder failed", slog.Error(err))
1918-
}
1919-
if dc.ConfigPath != "" {
1920-
if dc.ConfigPath, err = expandPathToAbs(dc.ConfigPath); err != nil {
1921-
logger.Warn(context.Background(), "expand devcontainer config path failed", slog.Error(err))
1922-
}
1923-
}
1924-
return dc
1925-
}
1926-
19271895
// EnvAgentSubsystem is the environment variable used to denote the
19281896
// specialized environment in which the agent is running
19291897
// (e.g. envbox, envbuilder).

agent/agentcontainers/devcontainer.go

+38
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package agentcontainers
22

33
import (
4+
"context"
45
"fmt"
56

7+
"cdr.dev/slog"
8+
69
"github.com/coder/coder/v2/codersdk"
710
)
811

@@ -15,3 +18,38 @@ func DevcontainerStartupScript(dc codersdk.WorkspaceAgentDevcontainer, script co
1518
script.Script = cmd
1619
return script
1720
}
21+
22+
func ExtractDevcontainerScripts(
23+
logger slog.Logger,
24+
expandPath func(string) (string, error),
25+
devcontainers []codersdk.WorkspaceAgentDevcontainer,
26+
scripts []codersdk.WorkspaceAgentScript,
27+
) (other []codersdk.WorkspaceAgentScript, devcontainerScripts []codersdk.WorkspaceAgentScript) {
28+
for _, dc := range devcontainers {
29+
dc = expandDevcontainerPaths(logger, expandPath, dc)
30+
for _, script := range scripts {
31+
// The devcontainer scripts match the devcontainer ID for
32+
// identification.
33+
if script.ID == dc.ID {
34+
devcontainerScripts = append(devcontainerScripts, DevcontainerStartupScript(dc, script))
35+
} else {
36+
other = append(other, script)
37+
}
38+
}
39+
}
40+
41+
return other, devcontainerScripts
42+
}
43+
44+
func expandDevcontainerPaths(logger slog.Logger, expandPath func(string) (string, error), dc codersdk.WorkspaceAgentDevcontainer) codersdk.WorkspaceAgentDevcontainer {
45+
var err error
46+
if dc.WorkspaceFolder, err = expandPath(dc.WorkspaceFolder); err != nil {
47+
logger.Warn(context.Background(), "expand devcontainer workspace folder failed", slog.Error(err))
48+
}
49+
if dc.ConfigPath != "" {
50+
if dc.ConfigPath, err = expandPath(dc.ConfigPath); err != nil {
51+
logger.Warn(context.Background(), "expand devcontainer config path failed", slog.Error(err))
52+
}
53+
}
54+
return dc
55+
}

0 commit comments

Comments
 (0)