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

Commit55c5c46

Browse files
committed
improve logger, fix config path realtive to workspace folder
1 parentef089be commit55c5c46

File tree

2 files changed

+68
-8
lines changed

2 files changed

+68
-8
lines changed

‎agent/agentcontainers/devcontainer.go

Lines changed: 21 additions & 3 deletions
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
funcexpandDevcontainerPaths(logger slog.Logger,expandPathfunc(string) (string,error),dc codersdk.WorkspaceAgentDevcontainer) codersdk.WorkspaceAgentDevcontainer {
69+
logger=logger.With(slog.F("devcontainer",dc.Name),slog.F("workspace_folder",dc.WorkspaceFolder),slog.F("config_path",dc.ConfigPath))
70+
6771
ifwf,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
ifdc.ConfigPath!="" {
73-
ifcp,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+
ifdc.ConfigPath[0]=='~' {
80+
ifcp,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
returndc
8090
}
91+
92+
funcrelativePathToAbs(workdir,pathstring)string {
93+
path=os.ExpandEnv(path)
94+
if!filepath.IsAbs(path) {
95+
path=filepath.Join(workdir,path)
96+
}
97+
returnpath
98+
}

‎agent/agentcontainers/devcontainer_test.go

Lines changed: 47 additions & 5 deletions
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(sstring) (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(sstring) (string,error) {
188+
s=strings.Replace(s,"~/","",1)
189+
iffilepath.IsAbs(s) {
190+
returns,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)

[8]ページ先頭

©2009-2025 Movatter.jp