-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind.go
More file actions
65 lines (54 loc) · 1.22 KB
/
find.go
File metadata and controls
65 lines (54 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"context"
"fmt"
"log"
"os"
"os/user"
"path/filepath"
"slices"
"strings"
"github.com/urfave/cli/v3"
)
func Find() *cli.Command {
return &cli.Command{
Name: "find",
Usage: "List candidate directories for roots of new tmux sessions",
Action: func(ctx context.Context, cmd *cli.Command) error {
findDirs := k.Strings("find.dirs")
// TODO: should there be more validation of find.dirs data?
// e.g., ignore duplicates, handle empty slice?
var childDirs []string
for _, parent := range findDirs {
if strings.HasPrefix(parent, "~/") {
user, err := user.Current()
if err != nil {
log.Fatal(err)
}
parent = filepath.Join(user.HomeDir, parent[2:])
}
file, err := os.Open(parent)
if err != nil {
log.Fatal(err)
}
defer file.Close()
dirs, err := file.Readdirnames(0)
if err != nil {
log.Fatal(err)
}
path, err := filepath.Abs(parent)
if err != nil {
log.Fatal(err)
}
for _, dir := range dirs {
childDirs = append(childDirs, filepath.Join(path, dir))
}
}
// TODO: make optional?
slices.Sort(childDirs)
out := strings.Join(childDirs, "\n")
fmt.Println(out)
return nil
},
}
}