clerk/internal/cli/helpers.go (view raw)
| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | |
| 9 | "olexsmir.xyz/clerk/journal" |
| 10 | ) |
| 11 | |
| 12 | func resolvePaths(paths []string) ([]string, error) { |
| 13 | if len(paths) == 0 { |
| 14 | return nil, nil |
| 15 | } |
| 16 | |
| 17 | seen := make(map[string]bool) |
| 18 | |
| 19 | var files []string |
| 20 | for _, p := range paths { |
| 21 | info, err := os.Stat(p) |
| 22 | if err != nil { |
| 23 | return nil, fmt.Errorf("%s: %w", p, err) |
| 24 | } |
| 25 | |
| 26 | if info.IsDir() { |
| 27 | filepath.Walk(p, func(fpath string, finfo os.FileInfo, err error) error { |
| 28 | if err != nil || finfo.IsDir() || !journal.IsJournalFile(fpath) { |
| 29 | return nil |
| 30 | } |
| 31 | if !seen[fpath] { |
| 32 | seen[fpath] = true |
| 33 | files = append(files, fpath) |
| 34 | } |
| 35 | return nil |
| 36 | }) |
| 37 | continue |
| 38 | } |
| 39 | |
| 40 | if journal.IsJournalFile(p) && !seen[p] { |
| 41 | seen[p] = true |
| 42 | files = append(files, p) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | return files, nil |
| 47 | } |
| 48 | |
| 49 | func readStdin() ([]byte, error) { |
| 50 | src, err := io.ReadAll(os.Stdin) |
| 51 | if err != nil { |
| 52 | return nil, fmt.Errorf("reading stdin: %w", err) |
| 53 | } |
| 54 | return src, nil |
| 55 | } |