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

croc: add --ignore to ignore folders with strings #865

Merged
merged 1 commit into from
Dec 31, 2024
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
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
Loading