|
| 1 | +// |
| 2 | +// finddir.go - a simple directory tree walker that looks for directories |
| 3 | +// by name, basename or extension. Basically a unix "find" light to |
| 4 | +// demonstrate walking the file system |
| 5 | +// |
| 6 | +// @author R. S. Doiel, <[email protected]> |
| 7 | +// |
| 8 | +// Copyright (c) 2017, Caltech |
| 9 | +// All rights not granted herein are expressly reserved by Caltech. |
| 10 | +// |
| 11 | +// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: |
| 12 | +// |
| 13 | +// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. |
| 14 | +// |
| 15 | +// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. |
| 16 | +// |
| 17 | +// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. |
| 18 | +// |
| 19 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 20 | +// |
| 21 | +package main |
| 22 | + |
| 23 | +import ( |
| 24 | + "flag" |
| 25 | + "fmt" |
| 26 | + "log" |
| 27 | + "os" |
| 28 | + "path" |
| 29 | + "path/filepath" |
| 30 | + "strings" |
| 31 | + "time" |
| 32 | + |
| 33 | + // CaltechLibrary packages |
| 34 | + "github.com/caltechlibrary/cli" |
| 35 | + "github.com/caltechlibrary/shelltools" |
| 36 | +) |
| 37 | + |
| 38 | +var ( |
| 39 | + usage = `USAGE: %s [OPTIONS] [TARGET] [DIRECTORIES_TO_SEARCH]` |
| 40 | + |
| 41 | + description = ` |
| 42 | +SYNOPSIS |
| 43 | +
|
| 44 | +%s finds directory based on matching prefix, suffix or contained text in base filename. |
| 45 | +` |
| 46 | + |
| 47 | + examples = ` |
| 48 | +EXAMPLE |
| 49 | +
|
| 50 | + %s -p img |
| 51 | +
|
| 52 | +Find all subdirectories starting with "img". |
| 53 | +` |
| 54 | + |
| 55 | + // Standard Options |
| 56 | + showHelp bool |
| 57 | + showVersion bool |
| 58 | + showLicense bool |
| 59 | + |
| 60 | + // Application Options |
| 61 | + showModificationTime bool |
| 62 | + findPrefix bool |
| 63 | + findContains bool |
| 64 | + findSuffix bool |
| 65 | + findAll bool |
| 66 | + stopOnErrors bool |
| 67 | + outputFullPath bool |
| 68 | + optDepth int |
| 69 | + pathSep string |
| 70 | +) |
| 71 | + |
| 72 | +func display(docroot, p string, m time.Time) { |
| 73 | + var s string |
| 74 | + if outputFullPath == true { |
| 75 | + s, _ = filepath.Abs(p) |
| 76 | + } else { |
| 77 | + s, _ = filepath.Rel(docroot, p) |
| 78 | + } |
| 79 | + if showModificationTime == true { |
| 80 | + fmt.Printf("%s %s\n", m.Format("2006-01-02 15:04:05 -0700"), s) |
| 81 | + return |
| 82 | + } |
| 83 | + fmt.Printf("%s\n", s) |
| 84 | +} |
| 85 | + |
| 86 | +func walkPath(docroot string, target string) error { |
| 87 | + return filepath.Walk(docroot, func(p string, info os.FileInfo, err error) error { |
| 88 | + if err != nil { |
| 89 | + if stopOnErrors == true { |
| 90 | + return fmt.Errorf("Can't read %s, %s", p, err) |
| 91 | + } |
| 92 | + return nil |
| 93 | + } |
| 94 | + // If a directory then apply rules for display |
| 95 | + if info.Mode().IsDir() == true { |
| 96 | + if optDepth > 0 { |
| 97 | + currentDepth := strings.Count(p, pathSep) + 1 |
| 98 | + if currentDepth > optDepth { |
| 99 | + return filepath.SkipDir |
| 100 | + } |
| 101 | + } |
| 102 | + //s := filepath.Base(p) |
| 103 | + s := p |
| 104 | + |
| 105 | + switch { |
| 106 | + case findPrefix == true && strings.HasPrefix(s, target) == true: |
| 107 | + display(docroot, p, info.ModTime()) |
| 108 | + case findSuffix == true && strings.HasSuffix(s, target) == true: |
| 109 | + display(docroot, p, info.ModTime()) |
| 110 | + case findContains == true && strings.Contains(s, target) == true: |
| 111 | + display(docroot, p, info.ModTime()) |
| 112 | + case strings.Compare(s, target) == 0: |
| 113 | + display(docroot, p, info.ModTime()) |
| 114 | + case findAll == true: |
| 115 | + display(docroot, p, info.ModTime()) |
| 116 | + } |
| 117 | + } |
| 118 | + return nil |
| 119 | + }) |
| 120 | +} |
| 121 | + |
| 122 | +func init() { |
| 123 | + // Standard Options |
| 124 | + flag.BoolVar(&showHelp, "h", false, "display this help message") |
| 125 | + flag.BoolVar(&showHelp, "help", false, "display this help message") |
| 126 | + flag.BoolVar(&showLicense, "l", false, "display license information") |
| 127 | + flag.BoolVar(&showLicense, "license", false, "display license information") |
| 128 | + flag.BoolVar(&showVersion, "v", false, "display version message") |
| 129 | + flag.BoolVar(&showVersion, "version", false, "display version message") |
| 130 | + |
| 131 | + // Application Specific Options |
| 132 | + flag.BoolVar(&showModificationTime, "m", false, "display file modification time before the path") |
| 133 | + flag.BoolVar(&showModificationTime, "mod-time", false, "display file modification time before the path") |
| 134 | + flag.BoolVar(&stopOnErrors, "e", false, "Stop walk on file system errors (e.g. permissions)") |
| 135 | + flag.BoolVar(&stopOnErrors, "error-stop", false, "Stop walk on file system errors (e.g. permissions)") |
| 136 | + flag.BoolVar(&findPrefix, "p", false, "find file(s) based on basename prefix") |
| 137 | + flag.BoolVar(&findPrefix, "prefix", false, "find file(s) based on basename prefix") |
| 138 | + flag.BoolVar(&findContains, "c", false, "find file(s) based on basename containing text") |
| 139 | + flag.BoolVar(&findContains, "contains", false, "find file(s) based on basename containing text") |
| 140 | + flag.BoolVar(&findSuffix, "s", false, "find file(s) based on basename suffix") |
| 141 | + flag.BoolVar(&findSuffix, "suffix", false, "find file(s) based on basename suffix") |
| 142 | + flag.BoolVar(&outputFullPath, "f", false, "list full path for files found") |
| 143 | + flag.BoolVar(&outputFullPath, "full-path", false, "list full path for files found") |
| 144 | + flag.IntVar(&optDepth, "d", 0, "Limit depth of directories walked") |
| 145 | + flag.IntVar(&optDepth, "depth", 0, "Limit depth of directories walked") |
| 146 | + pathSep = string(os.PathSeparator) |
| 147 | +} |
| 148 | + |
| 149 | +func main() { |
| 150 | + appName := path.Base(os.Args[0]) |
| 151 | + flag.Parse() |
| 152 | + args := flag.Args() |
| 153 | + |
| 154 | + // Configuration and command line interation |
| 155 | + cfg := cli.New(appName, appName, fmt.Sprintf(shelltools.LicenseText, appName, shelltools.Version), shelltools.Version) |
| 156 | + cfg.UsageText = fmt.Sprintf(usage, appName) |
| 157 | + cfg.DescriptionText = fmt.Sprintf(description, appName) |
| 158 | + cfg.ExampleText = fmt.Sprintf(examples, appName) |
| 159 | + |
| 160 | + if showHelp == true { |
| 161 | + fmt.Println(cfg.Usage()) |
| 162 | + os.Exit(0) |
| 163 | + } |
| 164 | + |
| 165 | + if showLicense == true { |
| 166 | + fmt.Println(cfg.License()) |
| 167 | + os.Exit(0) |
| 168 | + } |
| 169 | + |
| 170 | + if showVersion == true { |
| 171 | + fmt.Println(cfg.Version()) |
| 172 | + os.Exit(0) |
| 173 | + } |
| 174 | + |
| 175 | + if findPrefix == false && findSuffix == false && findContains == false { |
| 176 | + findAll = true |
| 177 | + } |
| 178 | + |
| 179 | + if findAll == false && len(args) == 0 { |
| 180 | + fmt.Println(cfg.Usage()) |
| 181 | + os.Exit(1) |
| 182 | + } |
| 183 | + |
| 184 | + // Shift the target directory name so args holds the directories to search |
| 185 | + target := "" |
| 186 | + if findAll == false && len(args) > 0 { |
| 187 | + target, args = args[0], args[1:] |
| 188 | + } |
| 189 | + |
| 190 | + // Handle the case where currect work directory is assumed |
| 191 | + if len(args) == 0 { |
| 192 | + args = []string{"."} |
| 193 | + } |
| 194 | + |
| 195 | + // For each directory to search walk the path |
| 196 | + for _, dir := range args { |
| 197 | + err := walkPath(dir, target) |
| 198 | + if err != nil { |
| 199 | + log.Fatal(err) |
| 200 | + } |
| 201 | + } |
| 202 | +} |
0 commit comments