github.com/xyproto/u-root@v6.0.1-0.20200302025726-5528e0c77a3c+incompatible/cmds/core/find/find.go (about)

     1  // Copyright 2013-2017 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Find finds files. It is similar to the Unix command. It uses REs, not globs,
     6  // for matching.
     7  //
     8  // OPTIONS:
     9  //     -d: enable debugging in the find package
    10  //     -mode integer-arg: match against mode, e.g. -mode 0755
    11  //     -type: match against a file type, e.g. -type f will match files
    12  //     -name: glob to match against file
    13  //     -l: long listing. It's not very good, yet, but it's useful enough.
    14  package main
    15  
    16  import (
    17  	"context"
    18  	"flag"
    19  	"fmt"
    20  	"log"
    21  	"os"
    22  	"strings"
    23  
    24  	"github.com/u-root/u-root/pkg/find"
    25  )
    26  
    27  const cmd = "find [opts] starting-at-path"
    28  
    29  var (
    30  	perm      = flag.Int("mode", -1, "Permissions")
    31  	fileType  = flag.String("type", "", "File type")
    32  	name      = flag.String("name", "", "glob for name")
    33  	long      = flag.Bool("l", false, "long listing")
    34  	debug     = flag.Bool("d", false, "Enable debugging in the find package")
    35  	fileTypes = map[string]os.FileMode{
    36  		"f":         0,
    37  		"file":      0,
    38  		"d":         os.ModeDir,
    39  		"directory": os.ModeDir,
    40  	}
    41  )
    42  
    43  func init() {
    44  	defUsage := flag.Usage
    45  	flag.Usage = func() {
    46  		os.Args[0] = cmd
    47  		defUsage()
    48  		os.Exit(1)
    49  	}
    50  }
    51  
    52  func main() {
    53  	flag.Parse()
    54  	a := flag.Args()
    55  	if len(a) != 1 {
    56  		flag.Usage()
    57  	}
    58  	root := a[0]
    59  
    60  	var mask, mode os.FileMode
    61  	if *perm != -1 {
    62  		mask = os.ModePerm
    63  		mode = os.FileMode(*perm)
    64  	}
    65  	if *fileType != "" {
    66  		intType, ok := fileTypes[*fileType]
    67  		if !ok {
    68  			var keys []string
    69  			for key := range fileTypes {
    70  				keys = append(keys, key)
    71  			}
    72  			log.Fatalf("%v is not a valid file type\n valid types are %v", *fileType, strings.Join(keys, ","))
    73  		}
    74  		mode |= intType
    75  		mask |= os.ModeType
    76  	}
    77  
    78  	debugLog := func(string, ...interface{}) {}
    79  	if *debug {
    80  		debugLog = log.Printf
    81  	}
    82  	names := find.Find(context.Background(),
    83  		find.WithRoot(root),
    84  		find.WithModeMatch(mode, mask),
    85  		find.WithFilenameMatch(*name),
    86  		find.WithDebugLog(debugLog),
    87  	)
    88  	for l := range names {
    89  		if l.Err != nil {
    90  			fmt.Fprintf(os.Stderr, "%v: %v\n", l.Name, l.Err)
    91  			continue
    92  		}
    93  		if *long {
    94  			fmt.Printf("%s\n", l)
    95  			continue
    96  		}
    97  		fmt.Printf("%s\n", l.Name)
    98  	}
    99  }