-
-
Notifications
You must be signed in to change notification settings - Fork 517
/
command_remote.go
50 lines (45 loc) · 1.59 KB
/
command_remote.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
package cli
import (
"context"
"fmt"
"os"
"github.com/spf13/cobra"
empty "google.golang.org/protobuf/types/known/emptypb"
"github.com/telepresenceio/telepresence/rpc/v2/connector"
"github.com/telepresenceio/telepresence/rpc/v2/daemon"
"github.com/telepresenceio/telepresence/v2/pkg/client/cli/cliutil"
"github.com/telepresenceio/telepresence/v2/pkg/client/userd/commands"
)
func getRemoteCommands(ctx context.Context) (groups cliutil.CommandGroups, err error) {
err = cliutil.WithStartedConnector(ctx, false, func(ctx context.Context, connectorClient connector.ConnectorClient) error {
remote, err := connectorClient.ListCommands(ctx, &empty.Empty{})
if err != nil {
return fmt.Errorf("unable to call ListCommands: %w", err)
}
if groups, err = cliutil.RPCToCommands(remote, runRemote); err != nil {
groups = commands.GetCommandsForLocal(err)
}
userDaemonRunning = true
return nil
})
if err != nil && err != cliutil.ErrNoUserDaemon {
return nil, err
}
if !userDaemonRunning {
groups = commands.GetCommandsForLocal(err)
}
return groups, nil
}
func runRemote(cmd *cobra.Command, _ []string) error {
return cliutil.WithNetwork(cmd.Context(), func(ctx context.Context, _ daemon.DaemonClient) error {
return cliutil.WithConnector(ctx, func(ctx context.Context, connectorClient connector.ConnectorClient) error {
result, err := connectorClient.RunCommand(ctx, &connector.RunCommandRequest{OsArgs: os.Args[1:]})
if err != nil {
return err
}
_, _ = cmd.OutOrStdout().Write(result.GetStdout())
_, _ = cmd.ErrOrStderr().Write(result.GetStderr())
return nil
})
})
}