Skip to content

Commit 0e9016b

Browse files
Add utilities for 1:1 (#168)
exec a remote command remove a remote file
1 parent 8af6434 commit 0e9016b

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

services/exec/client/utils.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package client
2+
3+
import (
4+
"context"
5+
"errors"
6+
7+
"github.com/Snowflake-Labs/sansshell/proxy/proxy"
8+
pb "github.com/Snowflake-Labs/sansshell/services/exec"
9+
)
10+
11+
// ExecRemoteCommand is a helper function for execing a command on a remote host
12+
// using a proxy.Conn. If the conn is defined for >1 targets this will return an error.
13+
func ExecRemoteCommand(ctx context.Context, conn *proxy.Conn, binary string, args ...string) (*pb.ExecResponse, error) {
14+
if len(conn.Targets) != 1 {
15+
return nil, errors.New("ExecRemoteCommand only supports single targets")
16+
}
17+
18+
c := pb.NewExecClient(conn)
19+
return c.Run(ctx, &pb.ExecRequest{
20+
Command: binary,
21+
Args: args,
22+
})
23+
}

services/localfile/client/utils.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,23 @@ func WriteRemoteFile(ctx context.Context, conn *proxy.Conn, config *FileConfig,
124124
return nil
125125
}
126126

127+
// RemoveRemoteFile is a helper function for removing a file on a remote host
128+
// using a proxy.Conn. If the conn is defined for >1 targets this will return an error.
129+
func RemoveRemoteFile(ctx context.Context, conn *proxy.Conn, path string) error {
130+
if len(conn.Targets) != 1 {
131+
return errors.New("RemoveRemoteFile only supports single targets")
132+
}
133+
134+
c := pb.NewLocalFileClient(conn)
135+
_, err := c.Rm(ctx, &pb.RmRequest{
136+
Filename: path,
137+
})
138+
if err != nil {
139+
return fmt.Errorf("remove problem - %v", err)
140+
}
141+
return nil
142+
}
143+
127144
// CopyRemoteFile is a helper function for copying a file on a remote host
128145
// using a proxy.Conn. If the conn is defined for >1 targets this will return an error.
129146
func CopyRemoteFile(ctx context.Context, conn *proxy.Conn, source string, destination *FileConfig) error {
@@ -164,7 +181,7 @@ func CopyRemoteFile(ctx context.Context, conn *proxy.Conn, source string, destin
164181
}
165182
_, err := c.Copy(ctx, req)
166183
if err != nil {
167-
return fmt.Errorf("Copy problem for %s -> %s: %v", source, destination.Filename, err)
184+
return fmt.Errorf("copy problem for %s -> %s: %v", source, destination.Filename, err)
168185
}
169186
return nil
170187
}

0 commit comments

Comments
 (0)