Skip to content

Commit

Permalink
croc: add --ignore to ignore folders with strings
Browse files Browse the repository at this point in the history
Fixes #864
  • Loading branch information
schollz committed Dec 31, 2024
1 parent bc5063e commit 537d514
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func Run() (err error) {
&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.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",
Action: send,
Expand Down Expand Up @@ -274,6 +275,13 @@ func send(c *cli.Context) (err error) {
if transfersParam == 0 {
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)
for i := 0; i <= transfersParam; i++ {
Expand Down Expand Up @@ -305,6 +313,7 @@ func send(c *cli.Context) (err error) {
GitIgnore: c.Bool("git"),
ShowQrCode: c.Bool("qrcode"),
MulticastAddress: c.String("multicast"),
Exclude: excludeStrings,
}
if crocOptions.RelayAddress != models.DEFAULT_RELAY {
crocOptions.RelayAddress6 = ""
Expand Down Expand Up @@ -418,7 +427,7 @@ Or you can go back to the classic croc behavior by enabling classic mode:
// generate code phrase
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 {
return
}
Expand Down
15 changes: 14 additions & 1 deletion src/croc/croc.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ type Options struct {
GitIgnore bool
MulticastAddress string
ShowQrCode bool
Exclude []string
}

type SimpleMessage struct {
Expand Down Expand Up @@ -314,7 +315,7 @@ func isChild(parentPath, childPath string) bool {

// This function retrieves the important file information
// 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
totalNumberFolders = 0
var paths []string
Expand Down Expand Up @@ -379,6 +380,18 @@ func GetFilesInfo(fnames []string, zipfolder bool, ignoreGit bool) (filesInfo []
}

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 {
err = errAbs
Expand Down

0 comments on commit 537d514

Please sign in to comment.