/
ssh.go
77 lines (66 loc) · 1.86 KB
/
ssh.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package devpod
import (
"context"
"fmt"
"io"
"os"
storagev1 "github.com/loft-sh/api/v3/pkg/apis/storage/v1"
"github.com/loft-sh/loftctl/v3/cmd/loftctl/flags"
"github.com/loft-sh/loftctl/v3/pkg/client"
devpodpkg "github.com/loft-sh/loftctl/v3/pkg/devpod"
"github.com/loft-sh/loftctl/v3/pkg/remotecommand"
"github.com/loft-sh/log"
"github.com/spf13/cobra"
)
// SshCmd holds the cmd flags
type SshCmd struct {
*flags.GlobalFlags
Log log.Logger
}
// NewSshCmd creates a new command
func NewSshCmd(globalFlags *flags.GlobalFlags) *cobra.Command {
cmd := &SshCmd{
GlobalFlags: globalFlags,
Log: log.GetInstance(),
}
c := &cobra.Command{
Hidden: true,
Use: "ssh",
Short: "Runs ssh on a workspace",
Long: `
#######################################################
################### loft devpod ssh ###################
#######################################################
`,
Args: cobra.NoArgs,
RunE: func(cobraCmd *cobra.Command, args []string) error {
return cmd.Run(cobraCmd.Context(), os.Stdin, os.Stdout, os.Stderr)
},
}
return c
}
func (cmd *SshCmd) Run(ctx context.Context, stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
baseClient, err := client.NewClientFromPath(cmd.Config)
if err != nil {
return err
}
info, err := devpodpkg.GetWorkspaceInfoFromEnv()
if err != nil {
return err
}
workspace, err := devpodpkg.FindWorkspace(ctx, baseClient, info.UID, info.ProjectName)
if err != nil {
return err
} else if workspace == nil {
return fmt.Errorf("couldn't find workspace")
}
conn, err := devpodpkg.DialWorkspace(baseClient, workspace, "ssh", devpodpkg.OptionsFromEnv(storagev1.DevPodFlagsSsh))
if err != nil {
return err
}
_, err = remotecommand.ExecuteConn(ctx, conn, stdin, stdout, stderr, cmd.Log.ErrorStreamOnly())
if err != nil {
return fmt.Errorf("error executing: %w", err)
}
return nil
}