| 26 | } |
| 27 | |
| 28 | func CompileSelector(glob, homeDir string) (g Selector, err error) { |
| 29 | if strings.HasPrefix(glob, "~/") { |
| 30 | if len(homeDir) == 0 { |
| 31 | err = fmt.Errorf("cannot expand %q pattern, home directory is unknown", homeDir) |
| 32 | return |
| 33 | } |
| 34 | glob = filepath.Join(homeDir, strings.TrimPrefix(glob, "~/")) |
| 35 | } |
| 36 | |
| 37 | dir, name := filepath.Split(glob) |
| 38 | |
| 39 | check := func(s string) error { |
| 40 | if strings.IndexRune(s, '*') != -1 { |
| 41 | return fmt.Errorf("bad glob: '*' and '**' are supported only as last component of the path") |
| 42 | } |
| 43 | return nil |
| 44 | } |
| 45 | |
| 46 | if name == "*" { |
| 47 | err = check(dir) |
| 48 | if err != nil { |
| 49 | return |
| 50 | } |
| 51 | g = starGlob(dir) |
| 52 | } else if name == "**" { |
| 53 | err = check(dir) |
| 54 | if err != nil { |
| 55 | return |
| 56 | } |
| 57 | g = starStarGlob(dir) |
| 58 | } else if strings.ContainsRune(glob, os.PathSeparator) { |
| 59 | err = check(glob) |
| 60 | if err != nil { |
| 61 | return |
| 62 | } |
| 63 | g = noStarGlob(glob) |
| 64 | } else { |
| 65 | err = check(glob) |
| 66 | if err != nil { |
| 67 | return |
| 68 | } |
| 69 | g = baseNameGlob(glob) |
| 70 | } |
| 71 | return |
| 72 | } |
| 73 | |
| 74 | type baseNameGlob string |
| 75 | |