1
1
Fork 0
mirror of https://github.com/schollz/croc.git synced 2025-10-11 05:11:06 +02:00

croc: add --ignore to ignore folders with strings

Fixes #864
This commit is contained in:
Zackary Scholl 2024-12-31 07:51:13 -08:00
parent bc5063e2c5
commit 537d514f9f
2 changed files with 24 additions and 2 deletions

View file

@ -78,6 +78,7 @@ func Run() (err error) {
&cli.IntFlag{Name: "port", Value: 9009, Usage: "base port for the relay"}, &cli.IntFlag{Name: "port", Value: 9009, Usage: "base port for the relay"},
&cli.IntFlag{Name: "transfers", Value: 4, Usage: "number of ports to use for transfers"}, &cli.IntFlag{Name: "transfers", Value: 4, Usage: "number of ports to use for transfers"},
&cli.BoolFlag{Name: "qrcode", Aliases: []string{"qr"}, Usage: "show receive code as a qrcode"}, &cli.BoolFlag{Name: "qrcode", Aliases: []string{"qr"}, Usage: "show receive code as a qrcode"},
&cli.StringFlag{Name: "exclude", Value: "", Usage: "exclude files if they contain any of the comma separated strings"},
}, },
HelpName: "croc send", HelpName: "croc send",
Action: send, Action: send,
@ -274,6 +275,13 @@ func send(c *cli.Context) (err error) {
if transfersParam == 0 { if transfersParam == 0 {
transfersParam = 4 transfersParam = 4
} }
excludeStrings := []string{}
for _, v := range strings.Split(c.String("exclude"), ",") {
v = strings.ToLower(strings.TrimSpace(v))
if v != "" {
excludeStrings = append(excludeStrings, v)
}
}
ports := make([]string, transfersParam+1) ports := make([]string, transfersParam+1)
for i := 0; i <= transfersParam; i++ { for i := 0; i <= transfersParam; i++ {
@ -305,6 +313,7 @@ func send(c *cli.Context) (err error) {
GitIgnore: c.Bool("git"), GitIgnore: c.Bool("git"),
ShowQrCode: c.Bool("qrcode"), ShowQrCode: c.Bool("qrcode"),
MulticastAddress: c.String("multicast"), MulticastAddress: c.String("multicast"),
Exclude: excludeStrings,
} }
if crocOptions.RelayAddress != models.DEFAULT_RELAY { if crocOptions.RelayAddress != models.DEFAULT_RELAY {
crocOptions.RelayAddress6 = "" crocOptions.RelayAddress6 = ""
@ -418,7 +427,7 @@ Or you can go back to the classic croc behavior by enabling classic mode:
// generate code phrase // generate code phrase
crocOptions.SharedSecret = utils.GetRandomName() crocOptions.SharedSecret = utils.GetRandomName()
} }
minimalFileInfos, emptyFoldersToTransfer, totalNumberFolders, err := croc.GetFilesInfo(fnames, crocOptions.ZipFolder, crocOptions.GitIgnore) minimalFileInfos, emptyFoldersToTransfer, totalNumberFolders, err := croc.GetFilesInfo(fnames, crocOptions.ZipFolder, crocOptions.GitIgnore, crocOptions.Exclude)
if err != nil { if err != nil {
return return
} }

View file

@ -87,6 +87,7 @@ type Options struct {
GitIgnore bool GitIgnore bool
MulticastAddress string MulticastAddress string
ShowQrCode bool ShowQrCode bool
Exclude []string
} }
type SimpleMessage struct { type SimpleMessage struct {
@ -314,7 +315,7 @@ func isChild(parentPath, childPath string) bool {
// This function retrieves the important file information // This function retrieves the important file information
// for every file that will be transferred // for every file that will be transferred
func GetFilesInfo(fnames []string, zipfolder bool, ignoreGit bool) (filesInfo []FileInfo, emptyFolders []FileInfo, totalNumberFolders int, err error) { func GetFilesInfo(fnames []string, zipfolder bool, ignoreGit bool, exclusions []string) (filesInfo []FileInfo, emptyFolders []FileInfo, totalNumberFolders int, err error) {
// fnames: the relative/absolute paths of files/folders that will be transferred // fnames: the relative/absolute paths of files/folders that will be transferred
totalNumberFolders = 0 totalNumberFolders = 0
var paths []string var paths []string
@ -379,6 +380,18 @@ func GetFilesInfo(fnames []string, zipfolder bool, ignoreGit bool) (filesInfo []
} }
absPath, errAbs := filepath.Abs(fpath) absPath, errAbs := filepath.Abs(fpath)
absPathLower := strings.ToLower(absPath)
ignorePath := false
for _, exclusion := range exclusions {
if strings.Contains(absPathLower, exclusion) {
ignorePath = true
break
}
}
if ignorePath {
log.Debugf("Ignoring %s", absPath)
continue
}
if errAbs != nil { if errAbs != nil {
err = errAbs err = errAbs