Skip to content

Commit 99a8eaa

Browse files
committed
FEAT: Allow positional directory input
1 parent 44c82b3 commit 99a8eaa

File tree

5 files changed

+73
-38
lines changed

5 files changed

+73
-38
lines changed

.github/workflows/go.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ jobs:
2222
- name: Set up Go
2323
uses: actions/setup-go@v4
2424
with:
25-
go-version: 1.21
25+
go-version: '>=1.21.0'
26+
cache: true
2627

2728
- name: Build the project
2829
run: go build -v ./...

.github/workflows/goreleaser.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@ jobs:
1616
- uses: actions/checkout@v3
1717
with:
1818
fetch-depth: 0
19+
1920
- run: git fetch --force --tags
21+
2022
- uses: actions/setup-go@v4
2123
with:
2224
go-version: '>=1.21.0'
2325
cache: true
26+
2427
# More assembly might be required: Docker logins, GPG, etc. It all depends
2528
# on your needs.
2629
- uses: goreleaser/goreleaser-action@v4

README.md

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,42 @@
11
# Go Find
2-
A file finder written in Go for my needs.
2+
A file finder written in Go.
33

4-
Uses a worker pool to search through directory trees quicker and take advantage of multi-core machines.
5-
Go concurrency allows it to run on single core machines.
6-
7-
If you're looking for something specific you can limit the number of responses
4+
Uses a worker pool to search through directory trees quicker by taking advantage of multi-core machines.
5+
Go concurrency allows it to run on single core machines.
86

97
### Features
10-
- Concurrency which may speed up or slow down the search depending on the tree structure.
8+
- Concurrency search directories for desired pattern
9+
- Exit early with a max results option
1110
- Common paths are ignored to save time and checks, such as `node_modules`, `.git`, and python `venv`.
1211
- Searches for a pattern using basic sub-string
12+
- Use a worker pool to avoid spinning up too many go routines if there are _many_ directories
1313

1414
## Warning
1515
> **warning**
1616
> Does not work on windows and I'm not trying too support this
1717
18-
## TODO
19-
- [ ] user cobra for for a better cli experience
20-
- [ ] sub command to print paths that are ignored
21-
- [ ] create case sensative/insensative searching
22-
- [ ] exclude additional directories or patters
23-
- [ ] include specific patterns in the search path
24-
- [ ] switch to ignore all hidden files/directories
25-
- [x] can we create a worker pool instead of the number of initial directories
26-
- [x] cap the number of returned responses, say 1 (quit after the first match!)
27-
- [x] Auto-generate releases
28-
- [ ] cap the depth of the search
29-
- [x] use select statement with a fallback queue to prevent deadlock from happening
30-
3118
## Using
19+
### Usage
20+
Customise your search with the following flags
21+
22+
```bash
23+
Usage: goFind [OPTIONS] <pattern> <path>
24+
set pattern & path positionally or via flags
25+
-c int
26+
The maximum number of results to find (default -1)
27+
-d string
28+
The starting directory to check for files (default ".")
29+
-i perform a case insensative search
30+
-p string
31+
A pattern to check for within the file names
32+
-q int
33+
The max work queue size (default 512)
34+
-v print the version and build information
35+
-w int
36+
Number of workers (default -1)
37+
```
38+
39+
## Contributing
3240
### Build
3341
build with `go build -o gf main.go` and run with `./gf -d <starting-path> -p <pattern-to-match-on>`
3442
#### Requirements
@@ -40,18 +48,16 @@ You can use the [makefile](./Makefile) to build a production release with `make
4048
- Make
4149
- Go 1.21+
4250
43-
### Flags
44-
Customise your search with the following flags
45-
```bash
46-
Usage of ./gf:
47-
-c int
48-
The maximum number of results to find (default -1)
49-
-d string
50-
The starting directory to check for files (default ".")
51-
-p string
52-
A pattern to check for within the file names
53-
-q int
54-
The max work queue size (default 2048)
55-
-w int
56-
Number of workers (default 8)
57-
```
51+
## Roadmap
52+
- [x] can we create a worker pool instead of the number of initial directories
53+
- [x] cap the number of returned responses, say 1 (quit after the first match!)
54+
- [x] Auto-generate releases
55+
- [x] cap the depth of the search
56+
- [x] use select statement with a fallback queue to prevent deadlock from happening
57+
- [x] use postional args or flags to set the pattern/path
58+
- [ ] Add flag to print which paths will be ignored
59+
- [ ] create case sensative/insensative searching
60+
- [ ] exclude additional directories or patters
61+
- [ ] include specific patterns in the search path
62+
- [ ] switch to ignore all hidden files/directories
63+
- [ ] look for `.gitignore` files and use contents to build ignored patterns

cli.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package main
22

33
import (
44
"flag"
5+
"fmt"
6+
"os"
57
"runtime"
68
)
79

@@ -16,7 +18,22 @@ type Config struct {
1618
Version bool
1719
}
1820

19-
func parseCLI() *Config {
21+
// myUsage overwrite default usage message from ./<bin> --help
22+
func myUsage() {
23+
bin := "gof"
24+
if len(os.Args) > 0 {
25+
bin = os.Args[0]
26+
}
27+
28+
fmt.Printf("Usage: %s [OPTIONS] <pattern> <path>\n", bin)
29+
fmt.Println(" set pattern & path positionally or via flags")
30+
flag.PrintDefaults()
31+
}
32+
33+
// parseArgs all arguments from the user to use with the program
34+
func parseArgs() *Config {
35+
flag.Usage = myUsage
36+
2037
//The number of workers. If there are more workers the system can read from
2138
//the work queue more often and a larger queue is not required.
2239
workers := flag.Int("w", -1, "Number of workers")
@@ -25,7 +42,7 @@ func parseCLI() *Config {
2542
maxResults := flag.Int("c", -1, "The maximum number of results to find")
2643
dir := flag.String("d", ".", "The starting directory to check for files")
2744
pattern := flag.String("p", "", "A pattern to check for within the file names")
28-
insensative := flag.Bool("i", true, "perform a case insensative search")
45+
insensative := flag.Bool("i", false, "perform a case insensative search")
2946
v := flag.Bool("v", false, "print the version and build information")
3047
flag.Parse()
3148

@@ -40,6 +57,10 @@ func parseCLI() *Config {
4057
w = runtime.NumCPU() + 2
4158
}
4259

60+
if *dir == "." && flag.Arg(1) != "" {
61+
*dir = flag.Arg(1)
62+
}
63+
4364
//Only for OSX/Linux, sorry windows
4465
//Remove any trailing slashes in the path
4566
if (*dir)[len(*dir)-1:] == "/" {

main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func printBuildInfo() {
2626
}
2727

2828
func main() {
29-
cfg := parseCLI()
29+
cfg := parseArgs()
3030
if cfg.Version {
3131
printBuildInfo()
3232
return
@@ -37,6 +37,10 @@ func main() {
3737
return
3838
}
3939

40+
if cfg.Insensative {
41+
fmt.Println("WARN: case insensative search is not yet supported")
42+
}
43+
4044
printCh := make(chan string, cfg.Workers)
4145
//The system will reach deadlock if the work queue reaches capacity
4246
workQ := make(chan string, cfg.QueueSize)

0 commit comments

Comments
 (0)