Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3e0c2a0

Browse files
committed
improve logger, fix config path realtive to workspace folder
1 parent ef089be commit 3e0c2a0

File tree

2 files changed

+68
-8
lines changed

2 files changed

+68
-8
lines changed

agent/agentcontainers/devcontainer.go

+21-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package agentcontainers
33
import (
44
"context"
55
"fmt"
6+
"os"
7+
"path/filepath"
68
"strings"
79

810
"cdr.dev/slog"
@@ -64,17 +66,33 @@ func devcontainerStartupScript(dc codersdk.WorkspaceAgentDevcontainer, script co
6466
}
6567

6668
func expandDevcontainerPaths(logger slog.Logger, expandPath func(string) (string, error), dc codersdk.WorkspaceAgentDevcontainer) codersdk.WorkspaceAgentDevcontainer {
69+
logger = logger.With(slog.F("workspace_folder", dc.WorkspaceFolder), slog.F("config_path", dc.ConfigPath))
70+
6771
if wf, err := expandPath(dc.WorkspaceFolder); err != nil {
6872
logger.Warn(context.Background(), "expand devcontainer workspace folder failed", slog.Error(err))
6973
} else {
7074
dc.WorkspaceFolder = wf
7175
}
7276
if dc.ConfigPath != "" {
73-
if cp, err := expandPath(dc.ConfigPath); err != nil {
74-
logger.Warn(context.Background(), "expand devcontainer config path failed", slog.Error(err))
77+
// Let expandPath handle home directory, otherwise assume relative to
78+
// workspace folder or absoulte.
79+
if dc.ConfigPath[0] == '~' {
80+
if cp, err := expandPath(dc.ConfigPath); err != nil {
81+
logger.Warn(context.Background(), "expand devcontainer config path failed", slog.Error(err))
82+
} else {
83+
dc.ConfigPath = cp
84+
}
7585
} else {
76-
dc.ConfigPath = cp
86+
dc.ConfigPath = relativePathToAbs(dc.WorkspaceFolder, dc.ConfigPath)
7787
}
7888
}
7989
return dc
8090
}
91+
92+
func relativePathToAbs(workdir, path string) string {
93+
path = os.ExpandEnv(path)
94+
if !filepath.IsAbs(path) {
95+
path = filepath.Join(workdir, path)
96+
}
97+
return path
98+
}

agent/agentcontainers/devcontainer_test.go

+47-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package agentcontainers_test
22

33
import (
4+
"path/filepath"
45
"strings"
56
"testing"
67

@@ -133,12 +134,12 @@ func TestExtractAndInitializeDevcontainerScripts(t *testing.T) {
133134
wantDevcontainerScripts: []codersdk.WorkspaceAgentScript{
134135
{
135136
ID: devcontainerIDs[0],
136-
Script: "devcontainer up --workspace-folder \"workspace1\" --config \"config1\"",
137+
Script: "devcontainer up --workspace-folder \"workspace1\" --config \"workspace1/config1\"",
137138
RunOnStart: false,
138139
},
139140
{
140141
ID: devcontainerIDs[1],
141-
Script: "devcontainer up --workspace-folder \"workspace2\" --config \"config2\"",
142+
Script: "devcontainer up --workspace-folder \"workspace2\" --config \"workspace2/config2\"",
142143
RunOnStart: false,
143144
},
144145
},
@@ -147,7 +148,7 @@ func TestExtractAndInitializeDevcontainerScripts(t *testing.T) {
147148
name: "scripts match devcontainers with expand path",
148149
args: args{
149150
expandPath: func(s string) (string, error) {
150-
return "expanded/" + s, nil
151+
return "/home/" + s, nil
151152
},
152153
devcontainers: []codersdk.WorkspaceAgentDevcontainer{
153154
{
@@ -170,12 +171,53 @@ func TestExtractAndInitializeDevcontainerScripts(t *testing.T) {
170171
wantDevcontainerScripts: []codersdk.WorkspaceAgentScript{
171172
{
172173
ID: devcontainerIDs[0],
173-
Script: "devcontainer up --workspace-folder \"expanded/workspace1\" --config \"expanded/config1\"",
174+
Script: "devcontainer up --workspace-folder \"/home/workspace1\" --config \"/home/workspace1/config1\"",
174175
RunOnStart: false,
175176
},
176177
{
177178
ID: devcontainerIDs[1],
178-
Script: "devcontainer up --workspace-folder \"expanded/workspace2\" --config \"expanded/config2\"",
179+
Script: "devcontainer up --workspace-folder \"/home/workspace2\" --config \"/home/workspace2/config2\"",
180+
RunOnStart: false,
181+
},
182+
},
183+
},
184+
{
185+
name: "expand config path when ~",
186+
args: args{
187+
expandPath: func(s string) (string, error) {
188+
s = strings.Replace(s, "~/", "", 1)
189+
if filepath.IsAbs(s) {
190+
return s, nil
191+
}
192+
return "/home/" + s, nil
193+
},
194+
devcontainers: []codersdk.WorkspaceAgentDevcontainer{
195+
{
196+
ID: devcontainerIDs[0],
197+
WorkspaceFolder: "workspace1",
198+
ConfigPath: "~/config1",
199+
},
200+
{
201+
ID: devcontainerIDs[1],
202+
WorkspaceFolder: "workspace2",
203+
ConfigPath: "/config2",
204+
},
205+
},
206+
scripts: []codersdk.WorkspaceAgentScript{
207+
{ID: devcontainerIDs[0], RunOnStart: true},
208+
{ID: devcontainerIDs[1], RunOnStart: true},
209+
},
210+
},
211+
wantFilteredScripts: []codersdk.WorkspaceAgentScript{},
212+
wantDevcontainerScripts: []codersdk.WorkspaceAgentScript{
213+
{
214+
ID: devcontainerIDs[0],
215+
Script: "devcontainer up --workspace-folder \"/home/workspace1\" --config \"/home/config1\"",
216+
RunOnStart: false,
217+
},
218+
{
219+
ID: devcontainerIDs[1],
220+
Script: "devcontainer up --workspace-folder \"/home/workspace2\" --config \"/config2\"",
179221
RunOnStart: false,
180222
},
181223
},

0 commit comments

Comments
 (0)