Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# Output directories
.localgo_security/
downloads/
scripts/*.log
*.log

# Dependency directories (if vendoring)
vendor/
Expand Down
41 changes: 9 additions & 32 deletions cmd/localgo/cmd/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var sendCmd = &cobra.Command{
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
files := sendfiles
var sendOpts []send.SendOption

if sendclipboard && sendstdin {
return fmt.Errorf("cannot use both --clipboard and --stdin")
Expand All @@ -55,19 +56,7 @@ var sendCmd = &cobra.Command{
if len(textBytes) == 0 {
return fmt.Errorf("standard input is empty")
}

tempFile, err := os.CreateTemp("", "localgo-clip-stdin-*.txt")
if err != nil {
return fmt.Errorf("failed to create temporary file for stdin: %w", err)
}
defer os.Remove(tempFile.Name())

if _, err := tempFile.Write(textBytes); err != nil {
tempFile.Close()
return fmt.Errorf("failed to write standard input content: %w", err)
}
tempFile.Close()
files = []string{tempFile.Name()}
sendOpts = append(sendOpts, send.WithInMemoryFile("stdin.txt", textBytes))
}

if sendclipboard {
Expand All @@ -78,34 +67,22 @@ var sendCmd = &cobra.Command{
if strings.TrimSpace(text) == "" {
return fmt.Errorf("clipboard is empty or does not contain text")
}

tempFile, err := os.CreateTemp("", "localgo-clip-*.txt")
if err != nil {
return fmt.Errorf("failed to create temporary file for clipboard: %w", err)
}
defer os.Remove(tempFile.Name())

if _, err := tempFile.WriteString(text); err != nil {
tempFile.Close()
return fmt.Errorf("failed to write clipboard text: %w", err)
}
tempFile.Close()
files = []string{tempFile.Name()}
sendOpts = append(sendOpts, send.WithInMemoryFile("clipboard.txt", []byte(text)))
}

if len(files) == 0 {
if len(files) == 0 && len(sendOpts) == 0 {
selected, err := cli.LaunchFilePicker()
if err == nil && selected != "" {
files = []string{selected}
}
}

if len(files) == 0 {
if len(files) == 0 && len(sendOpts) == 0 {
return fmt.Errorf("no file specified: use --file flag, --clipboard, or select from the file browser")
}

for _, file := range files {
if _, err := os.Stat(file); os.IsNotExist(err) && !sendclipboard {
if _, err := os.Stat(file); os.IsNotExist(err) && len(sendOpts) == 0 {
return fmt.Errorf("file not found: %s", file)
}
}
Expand Down Expand Up @@ -165,7 +142,7 @@ var sendCmd = &cobra.Command{
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(sendtimeout)*time.Second)
defer cancel()

if err := send.SendToDevice(ctx, Cfg, device, files, zap.S()); err != nil {
if err := send.SendToDevice(ctx, Cfg, device, files, zap.S(), sendOpts...); err != nil {
return fmt.Errorf("failed to send files: %w", err)
}

Expand Down Expand Up @@ -276,11 +253,11 @@ var sendCmd = &cobra.Command{
if selectedDevice != nil {
cli.PrintInfo("To: %s (%s:%d)", selectedDevice.Alias, selectedDevice.IP, selectedDevice.Port)
cli.PrintInfo("From: %s", fromAlias)
err = send.SendToDevice(ctx, Cfg, selectedDevice, files, zap.S())
err = send.SendToDevice(ctx, Cfg, selectedDevice, files, zap.S(), sendOpts...)
} else {
cli.PrintInfo("To: %s", target)
cli.PrintInfo("From: %s", fromAlias)
err = send.SendFiles(ctx, Cfg, files, target, sendport, zap.S())
err = send.SendFiles(ctx, Cfg, files, target, sendport, zap.S(), sendOpts...)
}
if err != nil {
return fmt.Errorf("failed to send files: %w", err)
Expand Down
13 changes: 9 additions & 4 deletions pkg/clipboard/clipboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func Write(text string) error {
cmd := exec.Command(provider.cmd, provider.args...) //nolint:gosec
cmd.Stdin = strings.NewReader(text)
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("clipboard write failed: %w: %s", err, strings.TrimSpace(string(out)))
return fmt.Errorf("clipboard write failed (%s): %w: %s", provider.cmd, err, strings.TrimSpace(string(out)))
}
return nil
}
Expand All @@ -44,12 +44,17 @@ func Write(text string) error {
// Returns an error when no suitable clipboard tool is available.
func Read() (string, error) {
if provider == nil || provider.readCmd == "" {
return "", fmt.Errorf("clipboard read unavailable: no supported tool found")
return "", fmt.Errorf("clipboard read unavailable: no supported tool found (install xclip, xsel, wl-paste, pbpaste, or Get-Clipboard)")
}
cmd := exec.Command(provider.readCmd, provider.readArgs...) //nolint:gosec
out, err := cmd.Output()
out, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("clipboard read failed: %w", err)
// Some tools (xclip, wl-paste) exit with 1 when the clipboard is empty
// and produce no output. Treat this as empty, not an error.
if len(out) == 0 {
return "", nil
}
return "", fmt.Errorf("clipboard read failed (%s): %w: %s", provider.readCmd, err, strings.TrimSpace(string(out)))
}
// Normalize Windows CRLF line endings to unix LF
return strings.ReplaceAll(string(out), "\r\n", "\n"), nil
Expand Down
Loading
Loading