From 64b5a0ab5f7799ba3210bf0afa1c5521d078d963 Mon Sep 17 00:00:00 2001 From: li mengyang Date: Fri, 13 Jan 2023 16:22:00 +0800 Subject: [PATCH] Optimize the logic of adding entry in wsl (#1414) Signed-off-by: hwdef Signed-off-by: hwdef --- pkg/ssh/config/entry.go | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/pkg/ssh/config/entry.go b/pkg/ssh/config/entry.go index 752431718..2aaa2d4c3 100644 --- a/pkg/ssh/config/entry.go +++ b/pkg/ssh/config/entry.go @@ -40,22 +40,13 @@ func AddEntry(eo EntryOptions) error { if err != nil { return err } + + // It helps user to directly use remote ssh plugin of VSCode on the host system. + // VSCode will only check the host ssh files. if osutil.IsWsl() { - logrus.Debug("Try adding entry to WSL's ssh-agent") - winSshConfig, err := osutil.GetWslHostSshConfig() - if err != nil { - return err - } - winKeyPath, err := osutil.CopyToWinEnvdHome(eo.PrivateKeyPath, 0600) + err := addEntryToWSLHost(eo) if err != nil { - return err - } - // Add the entry to the WSL host SSH config - logrus.Debugf("Adding entry to WSL's ssh-agent: %s", winSshConfig) - eo.PrivateKeyPath = winKeyPath - err = add(winSshConfig, eo) - if err != nil { - return err + logrus.Warnf("Failed to add entry to WSL host %s\n", err.Error()) } } return nil @@ -115,3 +106,23 @@ func RemoveEntry(name string) error { } return nil } + +func addEntryToWSLHost(eo EntryOptions) error { + logrus.Debug("Try adding entry to WSL's ssh-agent") + winSshConfig, err := osutil.GetWslHostSshConfig() + if err != nil { + return err + } + winKeyPath, err := osutil.CopyToWinEnvdHome(eo.PrivateKeyPath, 0600) + if err != nil { + return err + } + // Add the entry to the WSL host SSH config + logrus.Debugf("Adding entry to WSL's ssh-agent: %s", winSshConfig) + eo.PrivateKeyPath = winKeyPath + err = add(winSshConfig, eo) + if err != nil { + return err + } + return nil +}