From 856e1c966ed0796929851d3410bc8cd6da0483c5 Mon Sep 17 00:00:00 2001 From: Elliot <67360013+elliot40404@users.noreply.github.com> Date: Mon, 7 Oct 2024 03:59:48 +0530 Subject: [PATCH 1/2] Added copy to clipboard functionality for windows,linux and mac --- src/croc/croc.go | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/croc/croc.go b/src/croc/croc.go index 806882dc2..bdece1838 100644 --- a/src/croc/croc.go +++ b/src/croc/croc.go @@ -12,22 +12,23 @@ import ( "math" "net" "os" + "os/exec" "path" "path/filepath" + "runtime" "strconv" "strings" "sync" "time" - "golang.org/x/term" - "golang.org/x/time/rate" - "github.com/denisbrodbeck/machineid" ignore "github.com/sabhiram/go-gitignore" log "github.com/schollz/logger" "github.com/schollz/pake/v3" "github.com/schollz/peerdiscovery" "github.com/schollz/progressbar/v3" + "golang.org/x/term" + "golang.org/x/time/rate" "github.com/schollz/croc/v10/src/comm" "github.com/schollz/croc/v10/src/compress" @@ -307,7 +308,6 @@ func isChild(parentPath, childPath string) bool { return false } return !strings.HasPrefix(relPath, "..") - } // This function retrieves the important file information @@ -330,7 +330,7 @@ func GetFilesInfo(fnames []string, zipfolder bool, ignoreGit bool) (filesInfo [] paths = append(paths, fname) } } - var ignoredPaths = make(map[string]bool) + ignoredPaths := make(map[string]bool) if ignoreGit { wd, wdErr := os.Stat(".gitignore") if wdErr == nil { @@ -659,6 +659,7 @@ On the other computer run: (For Linux/OSX) CROC_SECRET=%[1]q croc %[2]s `, c.Options.SharedSecret, flags.String()) + copyToClipboard(c.Options.SharedSecret) if c.Options.Ask { machid, _ := machineid.ID() fmt.Fprintf(os.Stderr, "\rYour machine ID is '%s'\n", machid) @@ -2123,3 +2124,23 @@ func (c *Client) sendData(i int) { } } } + +func copyToClipboard(str string) { + var cmd *exec.Cmd + switch runtime.GOOS { + case "windows": + cmd = exec.Command("cmd", "/c", "clip") + case "darwin": + cmd = exec.Command("pbcopy") + case "linux": + cmd = exec.Command("xclip", "-selection", "clipboard") + default: + return + } + cmd.Stdin = bytes.NewReader([]byte(str)) + if err := cmd.Run(); err != nil { + log.Debugf("error copying to clipboard", err) + return + } + fmt.Fprintf(os.Stderr, "Code copied to clipboard") +} From 234eb86f6968ff15e45bee69ce1375d95190d39b Mon Sep 17 00:00:00 2001 From: Elliot <67360013+elliot40404@users.noreply.github.com> Date: Tue, 8 Oct 2024 02:02:58 +0530 Subject: [PATCH 2/2] fixed log statement formatting error --- src/croc/croc.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/croc/croc.go b/src/croc/croc.go index bdece1838..e5e03c918 100644 --- a/src/croc/croc.go +++ b/src/croc/croc.go @@ -2139,7 +2139,7 @@ func copyToClipboard(str string) { } cmd.Stdin = bytes.NewReader([]byte(str)) if err := cmd.Run(); err != nil { - log.Debugf("error copying to clipboard", err) + log.Debugf("error copying to clipboard: %v", err) return } fmt.Fprintf(os.Stderr, "Code copied to clipboard")