Skip to content

Commit 91b2054

Browse files
committed
fix a bunch of issues in wshcmd-file
1 parent c89a3e1 commit 91b2054

File tree

1 file changed

+20
-54
lines changed

1 file changed

+20
-54
lines changed

cmd/wsh/cmd/wshcmd-file.go

Lines changed: 20 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"io"
1212
"log"
1313
"os"
14-
"path/filepath"
1514
"strings"
1615
"text/tabwriter"
1716
"time"
@@ -54,12 +53,11 @@ Supported URI schemes:
5453

5554
var fileCmd = &cobra.Command{
5655
Use: "file",
57-
Short: "manage files across different storage systems",
58-
Long: `Manage files across different storage systems.
56+
Short: "manage files across local and remote systems",
57+
Long: `Manage files across local and remote systems.
5958
60-
Wave Terminal is capable of managing files from remote SSH hosts, S3-compatible
61-
systems, and the internal Wave filesystem. Files are addressed via URIs, which
62-
vary depending on the storage system.` + UriHelpText}
59+
Wave Terminal is capable of managing files from remote SSH hosts and your local
60+
computer. Files are addressed via URIs.` + UriHelpText}
6361

6462
var fileTimeout int64
6563

@@ -254,17 +252,15 @@ func fileWriteRun(cmd *cobra.Command, args []string) error {
254252
Info: &wshrpc.FileInfo{
255253
Path: path}}
256254

257-
buf := make([]byte, MaxFileSize)
258-
n, err := WrappedStdin.Read(buf)
259-
if err != nil && err != io.EOF {
255+
limitReader := io.LimitReader(WrappedStdin, MaxFileSize+1)
256+
data, err := io.ReadAll(limitReader)
257+
if err != nil {
260258
return fmt.Errorf("reading input: %w", err)
261259
}
262-
if int64(n) == MaxFileSize {
263-
if _, err := WrappedStdin.Read(make([]byte, 1)); err != io.EOF {
264-
return fmt.Errorf("input exceeds maximum file size of %d bytes", MaxFileSize)
265-
}
260+
if len(data) > MaxFileSize {
261+
return fmt.Errorf("input exceeds maximum file size of %d bytes", MaxFileSize)
266262
}
267-
fileData.Data64 = base64.StdEncoding.EncodeToString(buf[:n])
263+
fileData.Data64 = base64.StdEncoding.EncodeToString(data)
268264
err = wshclient.FileWriteCommand(RpcClient, fileData, &wshrpc.RpcOpts{Timeout: fileTimeout})
269265
if err != nil {
270266
return fmt.Errorf("writing file: %w", err)
@@ -353,22 +349,6 @@ func checkFileSize(path string, maxSize int64) (*wshrpc.FileInfo, error) {
353349
return info, nil
354350
}
355351

356-
func getTargetPath(src, dst string) (string, error) {
357-
srcBase := filepath.Base(src)
358-
359-
dstInfo, err := os.Stat(dst)
360-
if err == nil && dstInfo.IsDir() {
361-
// If it's an existing directory, use the source filename
362-
return filepath.Join(dst, srcBase), nil
363-
}
364-
if err != nil && !os.IsNotExist(err) {
365-
// Return error if it's something other than not exists
366-
return "", fmt.Errorf("checking destination path: %w", err)
367-
}
368-
369-
return dst, nil
370-
}
371-
372352
func fileCpRun(cmd *cobra.Command, args []string) error {
373353
src, dst := args[0], args[1]
374354
merge, err := cmd.Flags().GetBool("merge")
@@ -477,28 +457,31 @@ func filePrintColumns(filesChan <-chan wshrpc.RespOrErrorUnion[wshrpc.CommandRem
477457
}
478458

479459
func filePrintLong(filesChan <-chan wshrpc.RespOrErrorUnion[wshrpc.CommandRemoteListEntriesRtnData]) error {
480-
// Sample first 100 files to determine name width
481-
maxNameLen := 0
482-
var samples []*wshrpc.FileInfo
460+
var allFiles []*wshrpc.FileInfo
483461

484462
for respUnion := range filesChan {
485463
if respUnion.Error != nil {
486464
return respUnion.Error
487465
}
488466
resp := respUnion.Response
489-
samples = append(samples, resp.FileInfo...)
467+
allFiles = append(allFiles, resp.FileInfo...)
468+
}
469+
470+
maxNameLen := 0
471+
for _, fi := range allFiles {
472+
if len(fi.Name) > maxNameLen {
473+
maxNameLen = len(fi.Name)
474+
}
490475
}
491476

492-
// Use sampled width, but cap it at 60 chars to prevent excessive width
493477
nameWidth := maxNameLen + 2
494478
if nameWidth > 60 {
495479
nameWidth = 60
496480
}
497481

498482
writer := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)
499483

500-
// Print samples
501-
for _, f := range samples {
484+
for _, f := range allFiles {
502485
name := f.Name
503486
t := time.Unix(f.ModTime/1000, 0)
504487
timestamp := utilfn.FormatLsTime(t)
@@ -508,23 +491,6 @@ func filePrintLong(filesChan <-chan wshrpc.RespOrErrorUnion[wshrpc.CommandRemote
508491
fmt.Fprintf(writer, "%-*s\t%8d\t%s\n", nameWidth, name, f.Size, timestamp)
509492
}
510493
}
511-
512-
// Continue with remaining files
513-
for respUnion := range filesChan {
514-
if respUnion.Error != nil {
515-
return respUnion.Error
516-
}
517-
for _, f := range respUnion.Response.FileInfo {
518-
name := f.Name
519-
t := time.Unix(f.ModTime/1000, 0)
520-
timestamp := utilfn.FormatLsTime(t)
521-
if f.Size == 0 && strings.HasSuffix(name, "/") {
522-
fmt.Fprintf(writer, "%-*s\t%8s\t%s\n", nameWidth, name, "-", timestamp)
523-
} else {
524-
fmt.Fprintf(writer, "%-*s\t%8d\t%s\n", nameWidth, name, f.Size, timestamp)
525-
}
526-
}
527-
}
528494
writer.Flush()
529495

530496
return nil

0 commit comments

Comments
 (0)