Example #1
0
func main() {
	pt := pt.PlatinumSearcher{Out: os.Stdout, Err: os.Stderr}
	exitCode := pt.Run(os.Args[1:])
	os.Exit(exitCode)
}
Example #2
0
func main() {
	opts.Color = opts.SetEnableColor
	opts.NoColor = opts.SetDisableColor
	opts.EnableColor = true
	opts.SkipVcsIgnore = opts.SkipVcsIgnores

	parser := flags.NewParser(&opts, flags.Default)
	parser.Name = "pt"
	parser.Usage = "[OPTIONS] PATTERN [PATH]"

	args, err := parser.Parse()
	if err != nil {
		os.Exit(1)
	}

	if opts.Version {
		fmt.Printf("%s\n", version)
		os.Exit(0)
	}

	if len(args) == 0 && opts.FilesWithRegexp == "" {
		parser.WriteHelp(os.Stdout)
		os.Exit(1)
	}

	opts.SearchStream = false
	if len(args) == 1 {
		fi, err := os.Stdin.Stat()
		if runtime.GOOS == "windows" {
			if err == nil {
				opts.SearchStream = true
				opts.NoGroup = true
			}
		} else {
			if err != nil {
				os.Exit(1)
			}

			mode := fi.Mode()
			if (mode&os.ModeNamedPipe != 0) || mode.IsRegular() {
				opts.SearchStream = true
				opts.NoGroup = true
			}
		}
	}

	var roots = []string{"."}
	if len(args) >= 2 {
		roots = []string{}
		for _, root := range args[1:] {
			root = strings.TrimRight(root, "\"")
			_, err := os.Lstat(root)
			if err != nil {
				fmt.Fprintf(os.Stderr, "%s\n", err)
				os.Exit(1)
			}
			roots = append(roots, root)
		}
	}

	opts.Proc = runtime.NumCPU()

	if !terminal.IsTerminal(os.Stdout) {
		if !opts.ForceColor {
			opts.EnableColor = false
		}
		opts.NoGroup = true
	}

	if opts.Context > 0 {
		opts.Before = opts.Context
		opts.After = opts.Context
	}

	pattern := ""
	if len(args) > 0 {
		pattern = args[0]
	}

	if opts.WordRegexp {
		opts.Regexp = true
		pattern = "\\b" + pattern + "\\b"
	}

	start := time.Now()

	searcher := pt.PlatinumSearcher{roots, pattern, &opts}
	err = searcher.Search()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", err)
		os.Exit(1)
	}
	if opts.Stats {
		elapsed := time.Since(start)
		fmt.Printf("%d Files Searched\n", pt.FilesSearched)
		fmt.Printf("%s Elapsed\n", elapsed)
	}

	if pt.FileMatchCount == 0 && pt.MatchCount == 0 {
		os.Exit(1)
	}
}