searchfiles

package module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 4, 2023 License: BSD-3-Clause Imports: 2 Imported by: 0

README

searchfiles

PkgGoDev

Example: Simple

This uses a simple internal search implementation. It doesn't call out to any external processes, so it's fast to start up, but if you have to search a lot of files it might be quite slow.

package main

import (
  "context"
  "flag"
  "fmt"
  "strings"

  "fknsrs.biz/p/searchfiles"
  _ "fknsrs.biz/p/searchfiles/driver/native"
)

var (
  flagDirectory string
  flagQuery     string
)

func init() {
  flag.StringVar(&flagDirectory, "directory", ".", "Directory to search in.")
  flag.StringVar(&flagQuery, "query", "", "Query to search for.")
}

func main() {
  flag.Parse()

  files, err := searchfiles.SearchLiteral(context.Background(), flagDirectory, flagQuery)
  if err != nil {
    panic(err)
  }

  fmt.Println(strings.Join(files, "\n"))
}

Example: Detect

This tries to detect the "best" driver available. The Detect strategy assumes you have a lot of files to search.

package main

import (
  "context"
  "flag"
  "fmt"
  "os"
  "strings"

  "fknsrs.biz/p/searchfiles"
  "fknsrs.biz/p/searchfiles/detect"
)

var (
  flagDirectory string
  flagQuery     string
)

func init() {
  flag.StringVar(&flagDirectory, "directory", ".", "Directory to search in.")
  flag.StringVar(&flagQuery, "query", "", "Query to search for.")
}

func main() {
  flag.Parse()

  driverName, err := detect.Detect(context.Background(), nil)
  if err != nil {
    panic(err)
  }
  fmt.Fprintf(os.Stderr, "> best detected driver: %s\n", driverName)

  files, err := searchfiles.SearchLiteral(context.Background(), flagDirectory, flagQuery)
  if err != nil {
    panic(err)
  }

  fmt.Println(strings.Join(files, "\n"))
}

Example: Advanced

This shows most of the options of the library.

package main

import (
  "context"
  "flag"
  "fmt"
  "strings"

  "fknsrs.biz/p/searchfiles"
  _ "fknsrs.biz/p/searchfiles/driver/ag"
  _ "fknsrs.biz/p/searchfiles/driver/grep"
  _ "fknsrs.biz/p/searchfiles/driver/native"
  _ "fknsrs.biz/p/searchfiles/driver/pt"
  _ "fknsrs.biz/p/searchfiles/driver/rg"
)

var (
  flagDirectory string
  flagQuery     string
  flagRegexp    bool
  flagDriver    string
)

func init() {
  flag.StringVar(&flagDirectory, "directory", ".", "Directory to search in.")
  flag.StringVar(&flagQuery, "query", "", "Query to search for.")
  flag.BoolVar(&flagRegexp, "regexp", false, "Search for a regular expression rather than a static string.")
  flag.StringVar(&flagDriver, "driver", "native", "Choose a driver to use (ag, grep, native, pt, rg).")
}

func main() {
  flag.Parse()

  var files []string

  if flagRegexp {
    a, err := searchfiles.SearchRegexpUsing(context.Background(), flagDriver, flagDirectory, flagQuery)
    if err != nil {
      panic(err)
    }
    files = a
  } else {
    a, err := searchfiles.SearchLiteralUsing(context.Background(), flagDriver, flagDirectory, flagQuery)
    if err != nil {
      panic(err)
    }
    files = a
  }

  fmt.Println(strings.Join(files, "\n"))
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnimplemented = fmt.Errorf("unimplemented")
	ErrUnknownDriver = fmt.Errorf("no driver found with this name")
	ErrNoDrivers     = fmt.Errorf("no drivers registered; try using fknsrs.biz/p/searchfiles/detect or fknsrs.biz/p/searchfiles/driver/native")
)

Functions

func DriverNames

func DriverNames() []string

func Register

func Register(driverName string, driver Driver)

func SearchLiteral

func SearchLiteral(ctx context.Context, directory, query string) ([]string, error)

func SearchLiteralUsing

func SearchLiteralUsing(ctx context.Context, driverName string, directory, query string) ([]string, error)

func SearchRegexp

func SearchRegexp(ctx context.Context, directory, query string) ([]string, error)

func SearchRegexpUsing

func SearchRegexpUsing(ctx context.Context, driverName string, directory, query string) ([]string, error)

func SetPreferredDriver

func SetPreferredDriver(driverName string)

func TestDriver

func TestDriver(ctx context.Context, driverName string) error

Types

type Driver

type Driver interface {
	SelfTest(ctx context.Context) error
	SearchLiteral(ctx context.Context, directory, query string) ([]string, error)
	SearchRegexp(ctx context.Context, directory, query string) ([]string, error)
}

Directories

Path Synopsis
driver
ag
pt
rg
examples
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL