Skip to content

Commit 2d7a556

Browse files
authored
refactor: parameters to structs where used for ssh config (#327)
Signed-off-by: Samuel K <skevetter@pm.me>
1 parent ad7aebf commit 2d7a556

File tree

11 files changed

+296
-186
lines changed

11 files changed

+296
-186
lines changed

cmd/agent/workspace/up.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,12 @@ func (cmd *UpCmd) Run(ctx context.Context) error {
8181

8282
tunnelClient, logger, credentialsDir, err := initWorkspace(cancelCtx, cancel, workspaceInfo, cmd.Debug, !workspaceInfo.CLIOptions.Platform.Enabled && !workspaceInfo.CLIOptions.DisableDaemon)
8383
if err != nil {
84-
err1 := clientimplementation.DeleteWorkspaceFolder(workspaceInfo.Workspace.Context, workspaceInfo.Workspace.ID, workspaceInfo.Workspace.SSHConfigPath, workspaceInfo.Workspace.SSHConfigIncludePath, logger)
84+
err1 := clientimplementation.DeleteWorkspaceFolder(clientimplementation.DeleteWorkspaceFolderParams{
85+
Context: workspaceInfo.Workspace.Context,
86+
WorkspaceID: workspaceInfo.Workspace.ID,
87+
SSHConfigPath: workspaceInfo.Workspace.SSHConfigPath,
88+
SSHConfigIncludePath: workspaceInfo.Workspace.SSHConfigIncludePath,
89+
}, logger)
8590
if err1 != nil {
8691
return fmt.Errorf("%s %w", err1.Error(), err)
8792
}

cmd/build.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,23 @@ func NewBuildCmd(flags *flags.GlobalFlags) *cobra.Command {
7878
baseWorkspaceClient, err := workspace2.Resolve(
7979
ctx,
8080
devPodConfig,
81-
"",
82-
nil,
83-
args,
84-
"",
85-
cmd.Machine,
86-
cmd.ProviderOptions,
87-
false,
88-
cmd.DevContainerImage,
89-
cmd.DevContainerPath,
90-
sshConfigPath,
91-
"",
92-
nil,
93-
cmd.UID,
94-
false,
95-
cmd.Owner,
81+
workspace2.ResolveParams{
82+
IDE: "",
83+
IDEOptions: nil,
84+
Args: args,
85+
DesiredID: "",
86+
DesiredMachine: cmd.Machine,
87+
ProviderUserOptions: cmd.ProviderOptions,
88+
ReconfigureProvider: false,
89+
DevContainerImage: cmd.DevContainerImage,
90+
DevContainerPath: cmd.DevContainerPath,
91+
SSHConfigPath: sshConfigPath,
92+
SSHConfigIncludePath: "",
93+
Source: nil,
94+
UID: cmd.UID,
95+
ChangeLastUsed: false,
96+
Owner: cmd.Owner,
97+
},
9698
log.Default,
9799
)
98100
if err != nil {

cmd/pro/delete.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,12 @@ func cleanupLocalWorkspaces(ctx context.Context, devPodConfig *config.Config, wo
143143
return
144144
}
145145
// delete workspace folder
146-
err = clientimplementation.DeleteWorkspaceFolder(devPodConfig.DefaultContext, client.Workspace(), client.WorkspaceConfig().SSHConfigPath, client.WorkspaceConfig().SSHConfigIncludePath, log)
146+
err = clientimplementation.DeleteWorkspaceFolder(clientimplementation.DeleteWorkspaceFolderParams{
147+
Context: devPodConfig.DefaultContext,
148+
WorkspaceID: client.Workspace(),
149+
SSHConfigPath: client.WorkspaceConfig().SSHConfigPath,
150+
SSHConfigIncludePath: client.WorkspaceConfig().SSHConfigIncludePath,
151+
}, log)
147152
if err != nil {
148153
log.WithFields(logrus.Fields{
149154
"workspaceId": w.ID,

cmd/up.go

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,14 @@ func (cmd *UpCmd) Run(
215215
setupGPGAgentForwarding := cmd.GPGAgentForwarding || devPodConfig.ContextOption(config.ContextOptionGPGAgentForwarding) == "true"
216216
sshConfigIncludePath := devPodConfig.ContextOption(config.ContextOptionSSHConfigIncludePath)
217217

218-
err = configureSSH(client, cmd.SSHConfigPath, sshConfigIncludePath, user, workdir, setupGPGAgentForwarding, devPodHome)
218+
err = configureSSH(client, configureSSHParams{
219+
sshConfigPath: cmd.SSHConfigPath,
220+
sshConfigIncludePath: sshConfigIncludePath,
221+
user: user,
222+
workdir: workdir,
223+
gpgagent: setupGPGAgentForwarding,
224+
devPodHome: devPodHome,
225+
})
219226
if err != nil {
220227
return err
221228
}
@@ -1003,13 +1010,23 @@ func startBrowserTunnel(
10031010
return nil
10041011
}
10051012

1006-
func configureSSH(client client2.BaseWorkspaceClient, sshConfigPath, sshConfigIncludePath, user, workdir string, gpgagent bool, devPodHome string) error {
1007-
path, err := devssh.ResolveSSHConfigPath(sshConfigPath)
1013+
type configureSSHParams struct {
1014+
sshConfigPath string
1015+
sshConfigIncludePath string
1016+
user string
1017+
workdir string
1018+
gpgagent bool
1019+
devPodHome string
1020+
}
1021+
1022+
func configureSSH(client client2.BaseWorkspaceClient, params configureSSHParams) error {
1023+
path, err := devssh.ResolveSSHConfigPath(params.sshConfigPath)
10081024
if err != nil {
10091025
return fmt.Errorf("invalid ssh config path %w", err)
10101026
}
1011-
sshConfigPath = path
1027+
sshConfigPath := path
10121028

1029+
sshConfigIncludePath := params.sshConfigIncludePath
10131030
if sshConfigIncludePath != "" {
10141031
includePath, err := devssh.ResolveSSHConfigPath(sshConfigIncludePath)
10151032
if err != nil {
@@ -1018,17 +1035,17 @@ func configureSSH(client client2.BaseWorkspaceClient, sshConfigPath, sshConfigIn
10181035
sshConfigIncludePath = includePath
10191036
}
10201037

1021-
err = devssh.ConfigureSSHConfig(
1022-
sshConfigPath,
1023-
sshConfigIncludePath,
1024-
client.Context(),
1025-
client.Workspace(),
1026-
user,
1027-
workdir,
1028-
gpgagent,
1029-
devPodHome,
1030-
log.Default,
1031-
)
1038+
err = devssh.ConfigureSSHConfig(devssh.SSHConfigParams{
1039+
SSHConfigPath: sshConfigPath,
1040+
SSHConfigIncludePath: sshConfigIncludePath,
1041+
Context: client.Context(),
1042+
Workspace: client.Workspace(),
1043+
User: params.user,
1044+
Workdir: params.workdir,
1045+
GPGAgent: params.gpgagent,
1046+
DevPodHome: params.devPodHome,
1047+
Log: log.Default,
1048+
})
10321049
if err != nil {
10331050
return err
10341051
}
@@ -1450,21 +1467,23 @@ func (cmd *UpCmd) prepareClient(ctx context.Context, devPodConfig *config.Config
14501467
client, err := workspace2.Resolve(
14511468
ctx,
14521469
devPodConfig,
1453-
cmd.IDE,
1454-
cmd.IDEOptions,
1455-
args,
1456-
cmd.ID,
1457-
cmd.Machine,
1458-
cmd.ProviderOptions,
1459-
cmd.Reconfigure,
1460-
cmd.DevContainerImage,
1461-
cmd.DevContainerPath,
1462-
cmd.SSHConfigPath,
1463-
sshConfigIncludePath,
1464-
source,
1465-
cmd.UID,
1466-
true,
1467-
cmd.Owner,
1470+
workspace2.ResolveParams{
1471+
IDE: cmd.IDE,
1472+
IDEOptions: cmd.IDEOptions,
1473+
Args: args,
1474+
DesiredID: cmd.ID,
1475+
DesiredMachine: cmd.Machine,
1476+
ProviderUserOptions: cmd.ProviderOptions,
1477+
ReconfigureProvider: cmd.Reconfigure,
1478+
DevContainerImage: cmd.DevContainerImage,
1479+
DevContainerPath: cmd.DevContainerPath,
1480+
SSHConfigPath: cmd.SSHConfigPath,
1481+
SSHConfigIncludePath: sshConfigIncludePath,
1482+
Source: source,
1483+
UID: cmd.UID,
1484+
ChangeLastUsed: true,
1485+
Owner: cmd.Owner,
1486+
},
14681487
logger,
14691488
)
14701489
if err != nil {

pkg/client/clientimplementation/daemonclient/delete.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ func (c *client) Delete(ctx context.Context, opt clientpkg.DeleteOptions) error
2727
return err
2828
} else if workspace == nil {
2929
// delete the workspace folder
30-
err = clientimplementation.DeleteWorkspaceFolder(c.workspace.Context, c.workspace.ID, c.workspace.SSHConfigPath, c.workspace.SSHConfigIncludePath, c.log)
30+
err = clientimplementation.DeleteWorkspaceFolder(clientimplementation.DeleteWorkspaceFolderParams{
31+
Context: c.workspace.Context,
32+
WorkspaceID: c.workspace.ID,
33+
SSHConfigPath: c.workspace.SSHConfigPath,
34+
SSHConfigIncludePath: c.workspace.SSHConfigIncludePath,
35+
}, c.log)
3136
if err != nil {
3237
return err
3338
}
@@ -67,7 +72,12 @@ func (c *client) Delete(ctx context.Context, opt clientpkg.DeleteOptions) error
6772
}
6873

6974
// delete the workspace folder
70-
err = clientimplementation.DeleteWorkspaceFolder(c.workspace.Context, c.workspace.ID, c.workspace.SSHConfigPath, c.workspace.SSHConfigIncludePath, c.log)
75+
err = clientimplementation.DeleteWorkspaceFolder(clientimplementation.DeleteWorkspaceFolderParams{
76+
Context: c.workspace.Context,
77+
WorkspaceID: c.workspace.ID,
78+
SSHConfigPath: c.workspace.SSHConfigPath,
79+
SSHConfigIncludePath: c.workspace.SSHConfigIncludePath,
80+
}, c.log)
7181
if err != nil {
7282
return err
7383
}

pkg/client/clientimplementation/proxy_client.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,12 @@ func (s *proxyClient) Delete(ctx context.Context, opt client.DeleteOptions) erro
317317
s.log.Errorf("Error deleting workspace: %v", err)
318318
}
319319

320-
return DeleteWorkspaceFolder(s.workspace.Context, s.workspace.ID, s.workspace.SSHConfigPath, s.workspace.SSHConfigIncludePath, s.log)
320+
return DeleteWorkspaceFolder(DeleteWorkspaceFolderParams{
321+
Context: s.workspace.Context,
322+
WorkspaceID: s.workspace.ID,
323+
SSHConfigPath: s.workspace.SSHConfigPath,
324+
SSHConfigIncludePath: s.workspace.SSHConfigIncludePath,
325+
}, s.log)
321326
}
322327

323328
func (s *proxyClient) Stop(ctx context.Context, opt client.StopOptions) error {

pkg/client/clientimplementation/workspace_client.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,12 @@ func (s *workspaceClient) Delete(ctx context.Context, opt client.DeleteOptions)
410410
}
411411
}
412412

413-
return DeleteWorkspaceFolder(s.workspace.Context, s.workspace.ID, s.workspace.SSHConfigPath, s.workspace.SSHConfigIncludePath, s.log)
413+
return DeleteWorkspaceFolder(DeleteWorkspaceFolderParams{
414+
Context: s.workspace.Context,
415+
WorkspaceID: s.workspace.ID,
416+
SSHConfigPath: s.workspace.SSHConfigPath,
417+
SSHConfigIncludePath: s.workspace.SSHConfigIncludePath,
418+
}, s.log)
414419
}
415420

416421
func (s *workspaceClient) isMachineRunning(ctx context.Context) (bool, error) {
@@ -643,13 +648,21 @@ func DeleteMachineFolder(context, machineID string) error {
643648
return nil
644649
}
645650

646-
func DeleteWorkspaceFolder(context string, workspaceID string, sshConfigPath string, sshConfigIncludePath string, log log.Logger) error {
647-
path, err := ssh.ResolveSSHConfigPath(sshConfigPath)
651+
type DeleteWorkspaceFolderParams struct {
652+
Context string
653+
WorkspaceID string
654+
SSHConfigPath string
655+
SSHConfigIncludePath string
656+
}
657+
658+
func DeleteWorkspaceFolder(params DeleteWorkspaceFolderParams, log log.Logger) error {
659+
path, err := ssh.ResolveSSHConfigPath(params.SSHConfigPath)
648660
if err != nil {
649661
return err
650662
}
651-
sshConfigPath = path
663+
sshConfigPath := path
652664

665+
sshConfigIncludePath := params.SSHConfigIncludePath
653666
if sshConfigIncludePath != "" {
654667
includePath, err := ssh.ResolveSSHConfigPath(sshConfigIncludePath)
655668
if err != nil {
@@ -658,12 +671,12 @@ func DeleteWorkspaceFolder(context string, workspaceID string, sshConfigPath str
658671
sshConfigIncludePath = includePath
659672
}
660673

661-
err = ssh.RemoveFromConfig(workspaceID, sshConfigPath, sshConfigIncludePath, log)
674+
err = ssh.RemoveFromConfig(params.WorkspaceID, sshConfigPath, sshConfigIncludePath, log)
662675
if err != nil {
663-
log.Errorf("Remove workspace '%s' from ssh config: %v", workspaceID, err)
676+
log.Errorf("Remove workspace '%s' from ssh config: %v", params.WorkspaceID, err)
664677
}
665678

666-
workspaceFolder, err := provider.GetWorkspaceDir(context, workspaceID)
679+
workspaceFolder, err := provider.GetWorkspaceDir(params.Context, params.WorkspaceID)
667680
if err != nil {
668681
return err
669682
}

pkg/ssh/config.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,34 @@ var (
2323
MarkerEndPrefix = "# DevPod End "
2424
)
2525

26-
func ConfigureSSHConfig(sshConfigPath, sshConfigIncludePath, context, workspace, user, workdir string, gpgagent bool, devPodHome string, log log.Logger) error {
27-
return configureSSHConfigSameFile(sshConfigPath, sshConfigIncludePath, context, workspace, user, workdir, "", gpgagent, devPodHome, log)
26+
type SSHConfigParams struct {
27+
SSHConfigPath string
28+
SSHConfigIncludePath string
29+
Context string
30+
Workspace string
31+
User string
32+
Workdir string
33+
Command string
34+
GPGAgent bool
35+
DevPodHome string
36+
Log log.Logger
2837
}
2938

30-
func configureSSHConfigSameFile(sshConfigPath, sshConfigIncludePath, context, workspace, user, workdir, command string, gpgagent bool, devPodHome string, log log.Logger) error {
39+
func ConfigureSSHConfig(params SSHConfigParams) error {
3140
configLock.Lock()
3241
defer configLock.Unlock()
3342

34-
targetPath := sshConfigPath
35-
if sshConfigIncludePath != "" {
36-
targetPath = sshConfigIncludePath
43+
targetPath := params.SSHConfigPath
44+
if params.SSHConfigIncludePath != "" {
45+
targetPath = params.SSHConfigIncludePath
3746
}
3847

39-
newFile, err := addHost(targetPath, workspace+"."+"devpod", user, context, workspace, workdir, command, gpgagent, devPodHome)
48+
newFile, err := addHost(targetPath, params.Workspace+"."+"devpod", params.User, params.Context, params.Workspace, params.Workdir, params.Command, params.GPGAgent, params.DevPodHome)
4049
if err != nil {
4150
return fmt.Errorf("parse ssh config %w", err)
4251
}
4352

44-
return writeSSHConfig(targetPath, newFile, log)
53+
return writeSSHConfig(targetPath, newFile, params.Log)
4554
}
4655

4756
type DevPodSSHEntry struct {

pkg/workspace/delete.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ func Delete(ctx context.Context, devPodConfig *config.Config, args []string, ign
3636
log.Errorf("Error retrieving workspace: %v", err)
3737

3838
// delete workspace folder
39-
err = clientimplementation.DeleteWorkspaceFolder(devPodConfig.DefaultContext, workspaceID, "", "", log)
39+
err = clientimplementation.DeleteWorkspaceFolder(clientimplementation.DeleteWorkspaceFolderParams{
40+
Context: devPodConfig.DefaultContext,
41+
WorkspaceID: workspaceID,
42+
SSHConfigPath: "",
43+
SSHConfigIncludePath: "",
44+
}, log)
4045
if err != nil {
4146
return "", err
4247
}
@@ -52,7 +57,12 @@ func Delete(ctx context.Context, devPodConfig *config.Config, args []string, ign
5257
workspaceConfig := client.WorkspaceConfig()
5358
if !force && workspaceConfig.Imported {
5459
// delete workspace folder
55-
err = clientimplementation.DeleteWorkspaceFolder(devPodConfig.DefaultContext, client.Workspace(), workspaceConfig.SSHConfigPath, workspaceConfig.SSHConfigIncludePath, log)
60+
err = clientimplementation.DeleteWorkspaceFolder(clientimplementation.DeleteWorkspaceFolderParams{
61+
Context: devPodConfig.DefaultContext,
62+
WorkspaceID: client.Workspace(),
63+
SSHConfigPath: workspaceConfig.SSHConfigPath,
64+
SSHConfigIncludePath: workspaceConfig.SSHConfigIncludePath,
65+
}, log)
5666
if err != nil {
5767
return "", err
5868
}
@@ -138,7 +148,12 @@ func deleteSingleMachine(ctx context.Context, client client2.BaseWorkspaceClient
138148
}
139149

140150
// delete workspace folder
141-
err = clientimplementation.DeleteWorkspaceFolder(client.Context(), client.Workspace(), client.WorkspaceConfig().SSHConfigPath, client.WorkspaceConfig().SSHConfigIncludePath, log)
151+
err = clientimplementation.DeleteWorkspaceFolder(clientimplementation.DeleteWorkspaceFolderParams{
152+
Context: client.Context(),
153+
WorkspaceID: client.Workspace(),
154+
SSHConfigPath: client.WorkspaceConfig().SSHConfigPath,
155+
SSHConfigIncludePath: client.WorkspaceConfig().SSHConfigIncludePath,
156+
}, log)
142157
if err != nil {
143158
return false, err
144159
}

pkg/workspace/list.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ func List(ctx context.Context, devPodConfig *config.Config, skipPro bool, owner
5353
for _, localWorkspace := range localWorkspaces {
5454
if localWorkspace.IsPro() {
5555
if shouldDeleteLocalWorkspace(ctx, localWorkspace, proWorkspaceResults) {
56-
err = clientimplementation.DeleteWorkspaceFolder(devPodConfig.DefaultContext, localWorkspace.ID, localWorkspace.SSHConfigPath, localWorkspace.SSHConfigIncludePath, log)
56+
err = clientimplementation.DeleteWorkspaceFolder(clientimplementation.DeleteWorkspaceFolderParams{
57+
Context: devPodConfig.DefaultContext,
58+
WorkspaceID: localWorkspace.ID,
59+
SSHConfigPath: localWorkspace.SSHConfigPath,
60+
SSHConfigIncludePath: localWorkspace.SSHConfigIncludePath,
61+
}, log)
5762
if err != nil {
5863
log.Debugf("failed to delete local workspace %s: %v", localWorkspace.ID, err)
5964
}

0 commit comments

Comments
 (0)