Skip to content

Commit 327e3bc

Browse files
committed
use path for all the remote ops instead of os-sensitive filepath #166
1 parent 57ea684 commit 327e3bc

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

pkg/executor/executor.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package executor
44

55
import (
66
"context"
7-
"path/filepath"
7+
"path"
88
"strings"
99
"time"
1010
)
@@ -47,12 +47,12 @@ type DeleteOpts struct {
4747
Exclude []string // exclude files matching the given patterns
4848
}
4949

50-
func isExcluded(path string, excl []string) bool {
51-
pathSegments := strings.Split(path, string(filepath.Separator))
50+
func isExcluded(p string, excl []string) bool {
51+
pathSegments := strings.Split(p, string("/"))
5252
for i := range pathSegments {
53-
subpath := filepath.Join(pathSegments[:i+1]...)
53+
subpath := path.Join(pathSegments[:i+1]...)
5454
for _, ex := range excl {
55-
match, err := filepath.Match(ex, subpath)
55+
match, err := path.Match(ex, subpath)
5656
if err != nil {
5757
continue
5858
}
@@ -68,10 +68,10 @@ func isExcluded(path string, excl []string) bool {
6868
return false
6969
}
7070

71-
func isExcludedSubPath(path string, excl []string) bool {
72-
subpath := filepath.Join(path, "*")
71+
func isExcludedSubPath(p string, excl []string) bool {
72+
subpath := path.Join(p, "*")
7373
for _, ex := range excl {
74-
match, err := filepath.Match(subpath, strings.TrimSuffix(ex, "/*"))
74+
match, err := path.Match(subpath, strings.TrimSuffix(ex, "/*"))
7575
if err != nil {
7676
continue
7777
}

pkg/executor/remote.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"log"
1010
"net"
1111
"os"
12+
"path"
1213
"path/filepath"
1314
"sort"
1415
"strings"
@@ -83,7 +84,7 @@ func (ex *Remote) Upload(ctx context.Context, local, remote string, opts *UpDown
8384

8485
remoteFile := remote
8586
if len(matches) > 1 { // if there are multiple files, treat remote as a directory
86-
remoteFile = filepath.Join(remote, filepath.Base(match))
87+
remoteFile = path.Join(remote, filepath.Base(match))
8788
}
8889
req := sftpReq{
8990
client: ex.client,
@@ -132,7 +133,7 @@ func (ex *Remote) Download(ctx context.Context, remote, local string, opts *UpDo
132133
// if the remote basename does not equal the remoteFile basename,
133134
// treat remote as a glob pattern and local as a directory
134135
if filepath.Base(remote) != filepath.Base(remoteFile) {
135-
localFile = filepath.Join(local, filepath.Base(remoteFile))
136+
localFile = path.Join(local, filepath.Base(remoteFile))
136137
}
137138

138139
req := sftpReq{
@@ -170,8 +171,8 @@ func (ex *Remote) Sync(ctx context.Context, localDir, remoteDir string, opts *Sy
170171

171172
unmatchedFiles, deletedFiles := ex.findUnmatchedFiles(localFiles, remoteFiles, excl)
172173
for _, file := range unmatchedFiles {
173-
localPath := filepath.Join(localDir, file)
174-
remotePath := filepath.Join(remoteDir, file)
174+
localPath := path.Join(localDir, file)
175+
remotePath := path.Join(remoteDir, file)
175176
if err = ex.Upload(ctx, localPath, remotePath, &UpDownOpts{Mkdir: true}); err != nil {
176177
return nil, fmt.Errorf("failed to upload %s to %s: %w", localPath, remotePath, err)
177178
}
@@ -184,7 +185,7 @@ func (ex *Remote) Sync(ctx context.Context, localDir, remoteDir string, opts *Sy
184185
// note: this may cause attempts to remove files from already deleted directories, but it's ok, Delete is idempotent.
185186
for _, file := range deletedFiles {
186187
deleteOpts := &DeleteOpts{Recursive: remoteFiles[file].IsDir}
187-
if err = ex.Delete(ctx, filepath.Join(remoteDir, file), deleteOpts); err != nil {
188+
if err = ex.Delete(ctx, path.Join(remoteDir, file), deleteOpts); err != nil {
188189
return nil, fmt.Errorf("failed to delete %s: %w", file, err)
189190
}
190191
}
@@ -232,8 +233,8 @@ func (ex *Remote) Delete(ctx context.Context, remoteFile string, opts *DeleteOpt
232233
continue
233234
}
234235

235-
path := walker.Path()
236-
relPath, e := filepath.Rel(remoteFile, path)
236+
p := walker.Path()
237+
relPath, e := filepath.Rel(remoteFile, p)
237238
if e != nil {
238239
return e
239240
}
@@ -268,20 +269,20 @@ func (ex *Remote) Delete(ctx context.Context, remoteFile string, opts *DeleteOpt
268269
default:
269270
}
270271

271-
path := pathsToDelete[i]
272-
fi, stErr := sftpClient.Stat(path)
272+
p := pathsToDelete[i]
273+
fi, stErr := sftpClient.Stat(p)
273274
if stErr != nil {
274-
return fmt.Errorf("failed to stat %s: %w", path, stErr)
275+
return fmt.Errorf("failed to stat %s: %w", p, stErr)
275276
}
276277

277278
if fi.IsDir() {
278-
err = sftpClient.RemoveDirectory(path)
279+
err = sftpClient.RemoveDirectory(p)
279280
} else {
280-
err = sftpClient.Remove(path)
281+
err = sftpClient.Remove(p)
281282
}
282283

283284
if err != nil {
284-
return fmt.Errorf("failed to delete %s: %w", path, err)
285+
return fmt.Errorf("failed to delete %s: %w", p, err)
285286
}
286287
}
287288

@@ -563,7 +564,7 @@ func (ex *Remote) getRemoteFilesProperties(ctx context.Context, dir string, excl
563564
}
564565

565566
for _, entry := range entries {
566-
fullPath := filepath.Join(dir, entry.Name())
567+
fullPath := path.Join(dir, entry.Name())
567568
relPath, err := filepath.Rel(root, fullPath)
568569
if err != nil {
569570
log.Printf("[WARN] failed to get relative path for %s: %v", fullPath, err)

0 commit comments

Comments
 (0)