Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added copy to clipboard functionality for windows,linux and mac #822

Merged
merged 2 commits into from
Oct 7, 2024
Merged
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
31 changes: 26 additions & 5 deletions src/croc/croc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -307,7 +308,6 @@ func isChild(parentPath, childPath string) bool {
return false
}
return !strings.HasPrefix(relPath, "..")

}

// This function retrieves the important file information
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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: %v", err)
return
}
fmt.Fprintf(os.Stderr, "Code copied to clipboard")
}
Loading