From b374c3885f4b62ee5c2ee7e51443d63661521153 Mon Sep 17 00:00:00 2001 From: hiddenMedic <124312252+hiddenMedic@users.noreply.github.com> Date: Mon, 25 Sep 2023 02:40:21 +0200 Subject: [PATCH 1/8] small search refactor --- src/search/module_map.go | 45 ++++++++++++++++ src/search/search.go | 111 ++++----------------------------------- 2 files changed, 55 insertions(+), 101 deletions(-) create mode 100644 src/search/module_map.go diff --git a/src/search/module_map.go b/src/search/module_map.go new file mode 100644 index 00000000..012df1f7 --- /dev/null +++ b/src/search/module_map.go @@ -0,0 +1,45 @@ +package search + +import ( + "context" + + "github.com/tminaorg/brzaguza/src/bucket" + "github.com/tminaorg/brzaguza/src/config" + "github.com/tminaorg/brzaguza/src/engines" + "github.com/tminaorg/brzaguza/src/engines/bing" + "github.com/tminaorg/brzaguza/src/engines/brave" + "github.com/tminaorg/brzaguza/src/engines/duckduckgo" + "github.com/tminaorg/brzaguza/src/engines/etools" + "github.com/tminaorg/brzaguza/src/engines/google" + "github.com/tminaorg/brzaguza/src/engines/mojeek" + "github.com/tminaorg/brzaguza/src/engines/presearch" + "github.com/tminaorg/brzaguza/src/engines/qwant" + "github.com/tminaorg/brzaguza/src/engines/startpage" + "github.com/tminaorg/brzaguza/src/engines/swisscows" + "github.com/tminaorg/brzaguza/src/engines/yahoo" + "github.com/tminaorg/brzaguza/src/engines/yep" +) + +type EngineSearch func(context.Context, string, *bucket.Relay, engines.Options, config.Settings) error + +func NewEngineStarter() []EngineSearch { + // I would be a happy man if we could do this function at compile time + mm := make([]EngineSearch, 100) + + //alphabetically sorted + mm[engines.Bing] = bing.Search + mm[engines.Brave] = brave.Search + mm[engines.DuckDuckGo] = duckduckgo.Search + mm[engines.Etools] = etools.Search + mm[engines.Google] = google.Search + mm[engines.Mojeek] = mojeek.Search + mm[engines.Presearch] = presearch.Search + mm[engines.Qwant] = qwant.Search + mm[engines.Startpage] = startpage.Search + mm[engines.Swisscows] = swisscows.Search + mm[engines.Yahoo] = yahoo.Search + //mm[engines.Yandex] = yandex.Search + mm[engines.Yep] = yep.Search + + return mm +} diff --git a/src/search/search.go b/src/search/search.go index 6cdc6aa4..833cc0b5 100644 --- a/src/search/search.go +++ b/src/search/search.go @@ -11,18 +11,6 @@ import ( "github.com/tminaorg/brzaguza/src/bucket/result" "github.com/tminaorg/brzaguza/src/config" "github.com/tminaorg/brzaguza/src/engines" - "github.com/tminaorg/brzaguza/src/engines/bing" - "github.com/tminaorg/brzaguza/src/engines/brave" - "github.com/tminaorg/brzaguza/src/engines/duckduckgo" - "github.com/tminaorg/brzaguza/src/engines/etools" - "github.com/tminaorg/brzaguza/src/engines/google" - "github.com/tminaorg/brzaguza/src/engines/mojeek" - "github.com/tminaorg/brzaguza/src/engines/presearch" - "github.com/tminaorg/brzaguza/src/engines/qwant" - "github.com/tminaorg/brzaguza/src/engines/startpage" - "github.com/tminaorg/brzaguza/src/engines/swisscows" - "github.com/tminaorg/brzaguza/src/engines/yahoo" - "github.com/tminaorg/brzaguza/src/engines/yep" "github.com/tminaorg/brzaguza/src/rank" ) @@ -56,96 +44,17 @@ func PerformSearch(query string, options engines.Options, config *config.Config) func runEngines(engineMap map[string]config.Engine, query string, worker *conc.WaitGroup, relay *bucket.Relay, options engines.Options) { log.Info().Msgf("Enabled engines: %v", config.EnabledEngines) + engineStarter := NewEngineStarter() for name, engine := range engineMap { - if engineName, err := engines.NameString(name); err == nil { - switch engineName { - case engines.Google: - worker.Go(func() { - err := google.Search(context.Background(), query, relay, options, engine.Settings) - if err != nil { - log.Error().Err(err).Msgf("Failed searching %v", google.Info.Domain) - } - }) - case engines.DuckDuckGo: - worker.Go(func() { - err := duckduckgo.Search(context.Background(), query, relay, options, engine.Settings) - if err != nil { - log.Error().Err(err).Msgf("Failed searching %v", duckduckgo.Info.Domain) - } - }) - case engines.Mojeek: - worker.Go(func() { - err := mojeek.Search(context.Background(), query, relay, options, engine.Settings) - if err != nil { - log.Error().Err(err).Msgf("Failed searching %v", mojeek.Info.Domain) - } - }) - case engines.Qwant: - worker.Go(func() { - err := qwant.Search(context.Background(), query, relay, options, engine.Settings) - if err != nil { - log.Error().Err(err).Msgf("Failed searching %v", qwant.Info.Domain) - } - }) - case engines.Etools: - worker.Go(func() { - err := etools.Search(context.Background(), query, relay, options, engine.Settings) - if err != nil { - log.Error().Err(err).Msgf("Failed searching %v", etools.Info.Domain) - } - }) - case engines.Swisscows: - worker.Go(func() { - err := swisscows.Search(context.Background(), query, relay, options, engine.Settings) - if err != nil { - log.Error().Err(err).Msgf("Failed searching %v", swisscows.Info.Domain) - } - }) - case engines.Brave: - worker.Go(func() { - err := brave.Search(context.Background(), query, relay, options, engine.Settings) - if err != nil { - log.Error().Err(err).Msgf("Failed searching %v", brave.Info.Domain) - } - }) - case engines.Bing: - worker.Go(func() { - err := bing.Search(context.Background(), query, relay, options, engine.Settings) - if err != nil { - log.Error().Err(err).Msgf("Failed searching %v", bing.Info.Domain) - } - }) - case engines.Startpage: - worker.Go(func() { - err := startpage.Search(context.Background(), query, relay, options, engine.Settings) - if err != nil { - log.Error().Err(err).Msgf("Failed searching %v", startpage.Info.Domain) - } - }) - case engines.Yep: - worker.Go(func() { - err := yep.Search(context.Background(), query, relay, options, engine.Settings) - if err != nil { - log.Error().Err(err).Msgf("Failed searching %v", yep.Info.Domain) - } - }) - case engines.Yahoo: - worker.Go(func() { - err := yahoo.Search(context.Background(), query, relay, options, engine.Settings) - if err != nil { - log.Error().Err(err).Msgf("Failed searching %v", yahoo.Info.Domain) - } - }) - case engines.Presearch: - worker.Go(func() { - err := presearch.Search(context.Background(), query, relay, options, engine.Settings) - if err != nil { - log.Error().Err(err).Msgf("Failed searching %v", presearch.Info.Domain) - } - }) - } - } else { - log.Panic().Err(err).Msg("failed converting string to engine name") + engineName, nameErr := engines.NameString(name) + if nameErr != nil { + log.Panic().Err(nameErr).Msg("failed converting string to engine name") + return + } + + err := engineStarter[engineName](context.Background(), query, relay, options, engine.Settings) + if err != nil { + log.Error().Err(err).Msgf("failed searching %v", engineName) } } } From c66d0f38e6d5c5d976cb58cceba63f559bf588f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksa=20Siri=C5=A1ki?= <31509435+aleksasiriski@users.noreply.github.com> Date: Tue, 26 Sep 2023 21:56:08 +0200 Subject: [PATCH 2/8] Added searcher - go generator --- .gitignore | 3 +- generate/searcher/searcher.go | 427 ++++++++++++++++++++++++++++++++++ generate/searcher/structs.go | 3 + generate/searcher/util.go | 39 ++++ go.mod | 3 + go.sum | 9 +- src/engines/name.go | 1 + src/search/module_map.go | 45 ---- src/search/structs.go | 11 + 9 files changed, 493 insertions(+), 48 deletions(-) create mode 100644 generate/searcher/searcher.go create mode 100644 generate/searcher/structs.go create mode 100644 generate/searcher/util.go delete mode 100644 src/search/module_map.go create mode 100644 src/search/structs.go diff --git a/.gitignore b/.gitignore index 7f4f37ab..17c3214f 100644 --- a/.gitignore +++ b/.gitignore @@ -36,4 +36,5 @@ log/*.log # go generate *_stringer.go -*_enumer.go \ No newline at end of file +*_enumer.go +*_searcher.go \ No newline at end of file diff --git a/generate/searcher/searcher.go b/generate/searcher/searcher.go new file mode 100644 index 00000000..ff5a6feb --- /dev/null +++ b/generate/searcher/searcher.go @@ -0,0 +1,427 @@ +package main + +import ( + "bytes" + "flag" + "fmt" + "go/ast" + "go/constant" + "go/format" + "go/token" + "go/types" + "log" + "os" + "path/filepath" + "strings" + + "golang.org/x/tools/go/packages" +) + +var ( + typeName = flag.String("type", "", "type name; must be set") + output = flag.String("output", "", "output file name; default srcdir/_searcher.go") + trimprefix = flag.String("trimprefix", "", "trim the `prefix` from the generated constant names") + linecomment = flag.Bool("linecomment", false, "use line comment text as printed text when present") + buildTags = flag.String("tags", "", "comma-separated list of build tags to apply") + packageName = flag.String("packagename", "", "name of the package for generated code; default current package") + enginesImport = flag.String("enginesimport", "github.com/tminaorg/brzaguza/src/engines", "source of the engines import, which is prefixed to imports for consts; default github.com/tminaorg/brzaguza/src/engines") +) + +// Usage is a replacement usage function for the flags package. +func Usage() { + fmt.Fprintf(os.Stderr, "Usage of searcher:\n") + fmt.Fprintf(os.Stderr, "\tsearcher [flags] -type T [directory]\n") + fmt.Fprintf(os.Stderr, "\tsearcher [flags] -type T files... # Must be a single package\n") + fmt.Fprintf(os.Stderr, "Flags:\n") + flag.PrintDefaults() +} + +func main() { + log.SetFlags(0) + log.SetPrefix("searcher: ") + flag.Usage = Usage + flag.Parse() + if len(*typeName) == 0 { + flag.Usage() + os.Exit(2) + } + /* ---------------------------------- + //! Should be comma seperated list of type names, currently is only the first type name + ---------------------------------- */ + types := strings.Split(*typeName, ",") + var tags []string + if len(*buildTags) > 0 { + tags = strings.Split(*buildTags, ",") + } + + // We accept either one directory or a list of files. Which do we have? + args := flag.Args() + if len(args) == 0 { + // Default: process whole package in current directory. + args = []string{"."} + } + + // Parse the package once. + var dir string + g := Generator{ + trimPrefix: *trimprefix, + lineComment: *linecomment, + } + // TODO(suzmue): accept other patterns for packages (directories, list of files, import paths, etc). + if len(args) == 1 && isDirectory(args[0]) { + dir = args[0] + } else { + if len(tags) != 0 { + log.Fatal("-tags option applies only to directories, not when files are specified") + } + dir = filepath.Dir(args[0]) + } + + g.parsePackage(args, tags) + + // Print the header and package clause. + g.Printf("// Code generated by \"searcher %s\"; DO NOT EDIT.\n", strings.Join(os.Args[1:], " ")) + g.Printf("\n") + var pkgName string + if *packageName == "" { + pkgName = g.pkg.name + } else { + pkgName = *packageName + } + g.Printf("package %s", pkgName) + g.Printf("\n") + g.Printf("import \"%s\"\n", *enginesImport) // Used by all methods. + + // Run generate for each type. + for _, typeName := range types { + g.generate(typeName) + } + + // Format the output. + src := g.format() + + // Write to file. + outputName := *output + if outputName == "" { + baseName := fmt.Sprintf("%s_searcher.go", types[0]) + outputName = filepath.Join(dir, strings.ToLower(baseName)) + } + err := os.WriteFile(outputName, src, 0644) + if err != nil { + log.Fatalf("writing output: %s", err) + } +} + +// isDirectory reports whether the named file is a directory. +func isDirectory(name string) bool { + info, err := os.Stat(name) + if err != nil { + log.Fatal(err) + } + return info.IsDir() +} + +// Generator holds the state of the analysis. Primarily used to buffer +// the output for format.Source. +type Generator struct { + buf bytes.Buffer // Accumulated output. + pkg *Package // Package we are scanning. + + trimPrefix string + lineComment bool + + logf func(format string, args ...interface{}) // test logging hook; nil when not testing +} + +func (g *Generator) Printf(format string, args ...interface{}) { + fmt.Fprintf(&g.buf, format, args...) +} + +// File holds a single parsed file and associated data. +type File struct { + pkg *Package // Package to which this file belongs. + file *ast.File // Parsed AST. + // These fields are reset for each type being generated. + typeName string // Name of the constant type. + values Values // Accumulator for constant values of that type. + + trimPrefix string + lineComment bool +} + +type Package struct { + name string + defs map[*ast.Ident]types.Object + files []*File +} + +// parsePackage analyzes the single package constructed from the patterns and tags. +// parsePackage exits if there is an error. +func (g *Generator) parsePackage(patterns []string, tags []string) { + cfg := &packages.Config{ + Mode: packages.NeedName | packages.NeedTypes | packages.NeedTypesInfo | packages.NeedSyntax, + // TODO: Need to think about constants in test files. Maybe write type_string_test.go + // in a separate pass? For later. + Tests: false, + BuildFlags: []string{fmt.Sprintf("-tags=%s", strings.Join(tags, " "))}, + Logf: g.logf, + } + pkgs, err := packages.Load(cfg, patterns...) + if err != nil { + log.Fatal(err) + } + if len(pkgs) != 1 { + log.Fatalf("error: %d packages matching %v", len(pkgs), strings.Join(patterns, " ")) + } + g.addPackage(pkgs[0]) +} + +// addPackage adds a type checked Package and its syntax files to the generator. +func (g *Generator) addPackage(pkg *packages.Package) { + g.pkg = &Package{ + name: pkg.Name, + defs: pkg.TypesInfo.Defs, + files: make([]*File, len(pkg.Syntax)), + } + + for i, file := range pkg.Syntax { + g.pkg.files[i] = &File{ + file: file, + pkg: g.pkg, + trimPrefix: g.trimPrefix, + lineComment: g.lineComment, + } + } +} + +// generate produces imports and the NewEngineStarter method for the named type. +func (g *Generator) generate(typeName string) { + values := make(Values, 0, 100) + for _, file := range g.pkg.files { + // Set the state for this run of the walker. + file.typeName = typeName + file.values = nil + if file.file != nil { + ast.Inspect(file.file, file.genDecl) + values = append(values, file.values...) + } + } + + if len(values) == 0 { + log.Fatalf("no values defined for type %s", typeName) + } + + // Generate code for importing engines + for _, v := range values { + if validConst(v) { + g.Printf("import \"%s/%s\"\n", *enginesImport, strings.ToLower(v.originalName)) + } else { + values = values.Delete(v) + } + } + + // Generate code that will fail if the constants change value. + g.Printf("func _() {\n") + g.Printf("\t// An \"invalid array index\" compiler error signifies that the constant values have changed.\n") + g.Printf("\t// Re-run the searcher command to generate them again.\n") + g.Printf("\tvar x [1]struct{}\n") + for _, v := range values { + origName := v.originalName + if *packageName != "" { + origName = fmt.Sprintf("%s.%s", g.pkg.name, v.originalName) + } + g.Printf("\t_ = x[%s - (%s)]\n", origName, v.str) + } + g.Printf("}\n") + + g.buildOneRun(values, typeName) +} + +// format returns the gofmt-ed contents of the Generator's buffer. +func (g *Generator) format() []byte { + src, err := format.Source(g.buf.Bytes()) + if err != nil { + // Should never happen, but can arise when developing this code. + // The user can compile the output to see the error. + log.Printf("warning: internal error: invalid Go generated: %s", err) + log.Printf("warning: compile the package to analyze the error") + return g.buf.Bytes() + } + return src +} + +// Value represents a declared constant. +type Value struct { + originalName string // The name of the constant. + name string // The name with trimmed prefix. + // The value is stored as a bit pattern alone. The boolean tells us + // whether to interpret it as an int64 or a uint64; the only place + // this matters is when sorting. + // Much of the time the str field is all we need; it is printed + // by Value.String. + value uint64 // Will be converted to int64 when needed. + signed bool // Whether the constant is a signed type. + str string // The string representation given by the "go/constant" package. +} + +func (v *Value) String() string { + return v.str +} + +// genDecl processes one declaration clause. +func (f *File) genDecl(node ast.Node) bool { + decl, ok := node.(*ast.GenDecl) + if !ok || decl.Tok != token.CONST { + // We only care about const declarations. + return true + } + // The name of the type of the constants we are declaring. + // Can change if this is a multi-element declaration. + typ := "" + // Loop over the elements of the declaration. Each element is a ValueSpec: + // a list of names possibly followed by a type, possibly followed by values. + // If the type and value are both missing, we carry down the type (and value, + // but the "go/types" package takes care of that). + for _, spec := range decl.Specs { + vspec := spec.(*ast.ValueSpec) // Guaranteed to succeed as this is CONST. + if vspec.Type == nil && len(vspec.Values) > 0 { + // "X = 1". With no type but a value. If the constant is untyped, + // skip this vspec and reset the remembered type. + typ = "" + + // If this is a simple type conversion, remember the type. + // We don't mind if this is actually a call; a qualified call won't + // be matched (that will be SelectorExpr, not Ident), and only unusual + // situations will result in a function call that appears to be + // a type conversion. + ce, ok := vspec.Values[0].(*ast.CallExpr) + if !ok { + continue + } + id, ok := ce.Fun.(*ast.Ident) + if !ok { + continue + } + typ = id.Name + } + if vspec.Type != nil { + // "X T". We have a type. Remember it. + ident, ok := vspec.Type.(*ast.Ident) + if !ok { + continue + } + typ = ident.Name + } + if typ != f.typeName { + // This is not the type we're looking for. + continue + } + // We now have a list of names (from one line of source code) all being + // declared with the desired type. + // Grab their names and actual values and store them in f.values. + for _, name := range vspec.Names { + if name.Name == "_" { + continue + } + // This dance lets the type checker find the values for us. It's a + // bit tricky: look up the object declared by the name, find its + // types.Const, and extract its value. + obj, ok := f.pkg.defs[name] + if !ok { + log.Fatalf("no value for constant %s", name) + } + info := obj.Type().Underlying().(*types.Basic).Info() + if info&types.IsInteger == 0 { + log.Fatalf("can't handle non-integer constant type %s", typ) + } + value := obj.(*types.Const).Val() // Guaranteed to succeed as this is CONST. + if value.Kind() != constant.Int { + log.Fatalf("can't happen: constant is not an integer %s", name) + } + i64, isInt := constant.Int64Val(value) + u64, isUint := constant.Uint64Val(value) + if !isInt && !isUint { + log.Fatalf("internal error: value of %s is not an integer: %s", name, value.String()) + } + if !isInt { + u64 = uint64(i64) + } + v := Value{ + originalName: name.Name, + value: u64, + signed: info&types.IsUnsigned == 0, + str: value.String(), + } + if c := vspec.Comment; f.lineComment && c != nil && len(c.List) == 1 { + v.name = strings.TrimSpace(c.Text()) + } else { + v.name = strings.TrimPrefix(v.originalName, f.trimPrefix) + } + f.values = append(f.values, v) + } + } + return false +} + +// Helpers + +// usize returns the number of bits of the smallest unsigned integer +// type that will hold n. Used to create the smallest possible slice of +// integers to use as indexes into the concatenated strings. +func usize(n int) int { + switch { + case n < 1<<8: + return 8 + case n < 1<<16: + return 16 + default: + // 2^32 is enough constants for anyone. + return 32 + } +} + +// declareIndexAndNameVar is the single-run version of declareIndexAndNameVars +func (g *Generator) declareIndexAndNameVar(run Values, typeName string) { + index, name := g.createIndexAndNameDecl(run, typeName, "") + g.Printf("const %s\n", name) + g.Printf("var %s\n", index) +} + +// createIndexAndNameDecl returns the pair of declarations for the run. The caller will add "const" and "var". +func (g *Generator) createIndexAndNameDecl(run Values, typeName string, suffix string) (string, string) { + b := new(bytes.Buffer) + indexes := make([]int, len(run)) + for i := range run { + b.WriteString(run[i].name) + indexes[i] = b.Len() + } + nameConst := fmt.Sprintf("_%s_name%s = %q", typeName, suffix, b.String()) + nameLen := b.Len() + b.Reset() + fmt.Fprintf(b, "_%s_index%s = [...]uint%d{0, ", typeName, suffix, usize(nameLen)) + for i, v := range indexes { + if i > 0 { + fmt.Fprintf(b, ", ") + } + fmt.Fprintf(b, "%d", v) + } + fmt.Fprintf(b, "}") + return b.String(), nameConst +} + +// buildOneRun generates the variables and String method for a single run of contiguous values. +func (g *Generator) buildOneRun(values Values, typeName string) { + g.Printf("\n") + g.declareIndexAndNameVar(values, typeName) + // The generated code is simple enough to write as a Printf format. + g.Printf(` +func NewEngineStarter() []EngineSearch { + mm := make([]EngineSearch, %d)`, len(values)) + for _, v := range values { + g.Printf(` + mm[%s.%s] = %s.Search`, g.pkg.name, v.name, strings.ToLower(v.name)) + } + g.Printf(` + return mm +}`) +} diff --git a/generate/searcher/structs.go b/generate/searcher/structs.go new file mode 100644 index 00000000..aa401124 --- /dev/null +++ b/generate/searcher/structs.go @@ -0,0 +1,3 @@ +package main + +type Values []Value diff --git a/generate/searcher/util.go b/generate/searcher/util.go new file mode 100644 index 00000000..d2576d6d --- /dev/null +++ b/generate/searcher/util.go @@ -0,0 +1,39 @@ +package main + +import ( + "os" + "strings" +) + +func (s Values) Delete(v Value) []Value { + index := s.IndexOf(v) + ret := make([]Value, 0) + ret = append(ret, s[:index]...) + return append(ret, s[index+1:]...) +} + +func (s Values) IndexOf(x Value) int { + for i, v := range s { + if x == v { + return i + } + } + return -1 +} + +func validConst(v Value) bool { + lowerName := strings.ToLower(v.name) + return lowerName != "undefined" && exists(lowerName) +} + +// exists returns whether the given file or directory exists +func exists(path string) bool { + _, err := os.Stat(path) + if err == nil { + return true + } + if os.IsNotExist(err) { + return false + } + return false +} diff --git a/go.mod b/go.mod index b7991339..df9e58a3 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/natefinch/lumberjack v2.0.0+incompatible github.com/robertkrimen/otto v0.2.1 github.com/rs/zerolog v1.30.0 + golang.org/x/tools v0.13.0 ) require ( @@ -28,6 +29,7 @@ require ( github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/validator/v10 v10.15.3 // indirect github.com/goccy/go-json v0.10.2 // indirect + github.com/google/go-cmp v0.5.8 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/cpuid/v2 v2.2.5 // indirect github.com/knadh/koanf/maps v0.1.1 // indirect @@ -43,6 +45,7 @@ require ( github.com/ugorji/go/codec v1.2.11 // indirect golang.org/x/arch v0.5.0 // indirect golang.org/x/crypto v0.13.0 // indirect + golang.org/x/mod v0.12.0 // indirect gopkg.in/sourcemap.v1 v1.0.5 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index a286cb84..e8d1e56c 100644 --- a/go.sum +++ b/go.sum @@ -91,8 +91,9 @@ github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5a github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= +github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM= github.com/jawher/mow.cli v1.1.0/go.mod h1:aNaQlc7ozF3vw6IJ2dHjp2ZFiA4ozMIYY6PyuRJwlUg= @@ -192,6 +193,8 @@ golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvx golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= +golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -218,6 +221,7 @@ golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -261,8 +265,9 @@ golang.org/x/tools v0.0.0-20190606124116-d0a3d012864b/go.mod h1:/rFqwRUd4F7ZHNgw golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= +golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ= +golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= diff --git a/src/engines/name.go b/src/engines/name.go index cd8d976c..669df07f 100644 --- a/src/engines/name.go +++ b/src/engines/name.go @@ -5,6 +5,7 @@ import "strings" type Name uint8 //go:generate enumer -type=Name -json -text -yaml -sql +//go:generate go run github.com/tminaorg/brzaguza/generate/searcher -type=Name -packagename search -output ../search/engine_searcher.go const ( Undefined Name = iota Bing diff --git a/src/search/module_map.go b/src/search/module_map.go deleted file mode 100644 index 012df1f7..00000000 --- a/src/search/module_map.go +++ /dev/null @@ -1,45 +0,0 @@ -package search - -import ( - "context" - - "github.com/tminaorg/brzaguza/src/bucket" - "github.com/tminaorg/brzaguza/src/config" - "github.com/tminaorg/brzaguza/src/engines" - "github.com/tminaorg/brzaguza/src/engines/bing" - "github.com/tminaorg/brzaguza/src/engines/brave" - "github.com/tminaorg/brzaguza/src/engines/duckduckgo" - "github.com/tminaorg/brzaguza/src/engines/etools" - "github.com/tminaorg/brzaguza/src/engines/google" - "github.com/tminaorg/brzaguza/src/engines/mojeek" - "github.com/tminaorg/brzaguza/src/engines/presearch" - "github.com/tminaorg/brzaguza/src/engines/qwant" - "github.com/tminaorg/brzaguza/src/engines/startpage" - "github.com/tminaorg/brzaguza/src/engines/swisscows" - "github.com/tminaorg/brzaguza/src/engines/yahoo" - "github.com/tminaorg/brzaguza/src/engines/yep" -) - -type EngineSearch func(context.Context, string, *bucket.Relay, engines.Options, config.Settings) error - -func NewEngineStarter() []EngineSearch { - // I would be a happy man if we could do this function at compile time - mm := make([]EngineSearch, 100) - - //alphabetically sorted - mm[engines.Bing] = bing.Search - mm[engines.Brave] = brave.Search - mm[engines.DuckDuckGo] = duckduckgo.Search - mm[engines.Etools] = etools.Search - mm[engines.Google] = google.Search - mm[engines.Mojeek] = mojeek.Search - mm[engines.Presearch] = presearch.Search - mm[engines.Qwant] = qwant.Search - mm[engines.Startpage] = startpage.Search - mm[engines.Swisscows] = swisscows.Search - mm[engines.Yahoo] = yahoo.Search - //mm[engines.Yandex] = yandex.Search - mm[engines.Yep] = yep.Search - - return mm -} diff --git a/src/search/structs.go b/src/search/structs.go new file mode 100644 index 00000000..2542fd89 --- /dev/null +++ b/src/search/structs.go @@ -0,0 +1,11 @@ +package search + +import ( + "context" + + "github.com/tminaorg/brzaguza/src/bucket" + "github.com/tminaorg/brzaguza/src/config" + "github.com/tminaorg/brzaguza/src/engines" +) + +type EngineSearch func(context.Context, string, *bucket.Relay, engines.Options, config.Settings) error From 9b56fafa4883369eafaef9c57461b8d5c8dad4f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksa=20Siri=C5=A1ki?= <31509435+aleksasiriski@users.noreply.github.com> Date: Tue, 26 Sep 2023 21:59:57 +0200 Subject: [PATCH 3/8] Refactor --- generate/searcher/searcher.go | 68 ----------------------------------- generate/searcher/structs.go | 50 ++++++++++++++++++++++++++ generate/searcher/util.go | 25 +++++++++++++ 3 files changed, 75 insertions(+), 68 deletions(-) diff --git a/generate/searcher/searcher.go b/generate/searcher/searcher.go index ff5a6feb..3b7622ae 100644 --- a/generate/searcher/searcher.go +++ b/generate/searcher/searcher.go @@ -112,49 +112,10 @@ func main() { } } -// isDirectory reports whether the named file is a directory. -func isDirectory(name string) bool { - info, err := os.Stat(name) - if err != nil { - log.Fatal(err) - } - return info.IsDir() -} - -// Generator holds the state of the analysis. Primarily used to buffer -// the output for format.Source. -type Generator struct { - buf bytes.Buffer // Accumulated output. - pkg *Package // Package we are scanning. - - trimPrefix string - lineComment bool - - logf func(format string, args ...interface{}) // test logging hook; nil when not testing -} - func (g *Generator) Printf(format string, args ...interface{}) { fmt.Fprintf(&g.buf, format, args...) } -// File holds a single parsed file and associated data. -type File struct { - pkg *Package // Package to which this file belongs. - file *ast.File // Parsed AST. - // These fields are reset for each type being generated. - typeName string // Name of the constant type. - values Values // Accumulator for constant values of that type. - - trimPrefix string - lineComment bool -} - -type Package struct { - name string - defs map[*ast.Ident]types.Object - files []*File -} - // parsePackage analyzes the single package constructed from the patterns and tags. // parsePackage exits if there is an error. func (g *Generator) parsePackage(patterns []string, tags []string) { @@ -250,20 +211,6 @@ func (g *Generator) format() []byte { return src } -// Value represents a declared constant. -type Value struct { - originalName string // The name of the constant. - name string // The name with trimmed prefix. - // The value is stored as a bit pattern alone. The boolean tells us - // whether to interpret it as an int64 or a uint64; the only place - // this matters is when sorting. - // Much of the time the str field is all we need; it is printed - // by Value.String. - value uint64 // Will be converted to int64 when needed. - signed bool // Whether the constant is a signed type. - str string // The string representation given by the "go/constant" package. -} - func (v *Value) String() string { return v.str } @@ -365,21 +312,6 @@ func (f *File) genDecl(node ast.Node) bool { // Helpers -// usize returns the number of bits of the smallest unsigned integer -// type that will hold n. Used to create the smallest possible slice of -// integers to use as indexes into the concatenated strings. -func usize(n int) int { - switch { - case n < 1<<8: - return 8 - case n < 1<<16: - return 16 - default: - // 2^32 is enough constants for anyone. - return 32 - } -} - // declareIndexAndNameVar is the single-run version of declareIndexAndNameVars func (g *Generator) declareIndexAndNameVar(run Values, typeName string) { index, name := g.createIndexAndNameDecl(run, typeName, "") diff --git a/generate/searcher/structs.go b/generate/searcher/structs.go index aa401124..6c131b6c 100644 --- a/generate/searcher/structs.go +++ b/generate/searcher/structs.go @@ -1,3 +1,53 @@ package main +import ( + "bytes" + "go/ast" + "go/types" +) + +// Value represents a declared constant. +type Value struct { + originalName string // The name of the constant. + name string // The name with trimmed prefix. + // The value is stored as a bit pattern alone. The boolean tells us + // whether to interpret it as an int64 or a uint64; the only place + // this matters is when sorting. + // Much of the time the str field is all we need; it is printed + // by Value.String. + value uint64 // Will be converted to int64 when needed. + signed bool // Whether the constant is a signed type. + str string // The string representation given by the "go/constant" package. +} + type Values []Value + +// Generator holds the state of the analysis. Primarily used to buffer +// the output for format.Source. +type Generator struct { + buf bytes.Buffer // Accumulated output. + pkg *Package // Package we are scanning. + + trimPrefix string + lineComment bool + + logf func(format string, args ...interface{}) // test logging hook; nil when not testing +} + +// File holds a single parsed file and associated data. +type File struct { + pkg *Package // Package to which this file belongs. + file *ast.File // Parsed AST. + // These fields are reset for each type being generated. + typeName string // Name of the constant type. + values Values // Accumulator for constant values of that type. + + trimPrefix string + lineComment bool +} + +type Package struct { + name string + defs map[*ast.Ident]types.Object + files []*File +} diff --git a/generate/searcher/util.go b/generate/searcher/util.go index d2576d6d..d2c51a65 100644 --- a/generate/searcher/util.go +++ b/generate/searcher/util.go @@ -1,6 +1,7 @@ package main import ( + "log" "os" "strings" ) @@ -37,3 +38,27 @@ func exists(path string) bool { } return false } + +// isDirectory reports whether the named file is a directory. +func isDirectory(name string) bool { + info, err := os.Stat(name) + if err != nil { + log.Fatal(err) + } + return info.IsDir() +} + +// usize returns the number of bits of the smallest unsigned integer +// type that will hold n. Used to create the smallest possible slice of +// integers to use as indexes into the concatenated strings. +func usize(n int) int { + switch { + case n < 1<<8: + return 8 + case n < 1<<16: + return 16 + default: + // 2^32 is enough constants for anyone. + return 32 + } +} From e03ae5379f067317a0609037e0ecede6a56d0ae3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksa=20Siri=C5=A1ki?= <31509435+aleksasiriski@users.noreply.github.com> Date: Tue, 26 Sep 2023 22:04:22 +0200 Subject: [PATCH 4/8] rename exists --- generate/searcher/searcher.go | 2 +- generate/searcher/util.go | 22 ++++++++++------------ 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/generate/searcher/searcher.go b/generate/searcher/searcher.go index 3b7622ae..df94d103 100644 --- a/generate/searcher/searcher.go +++ b/generate/searcher/searcher.go @@ -68,7 +68,7 @@ func main() { lineComment: *linecomment, } // TODO(suzmue): accept other patterns for packages (directories, list of files, import paths, etc). - if len(args) == 1 && isDirectory(args[0]) { + if len(args) == 1 && isDirectoryFatal(args[0]) { dir = args[0] } else { if len(tags) != 0 { diff --git a/generate/searcher/util.go b/generate/searcher/util.go index d2c51a65..327e7f83 100644 --- a/generate/searcher/util.go +++ b/generate/searcher/util.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "log" "os" "strings" @@ -19,29 +20,26 @@ func (s Values) IndexOf(x Value) int { return i } } + log.Fatal(fmt.Errorf("indexOf func failed")) return -1 } func validConst(v Value) bool { lowerName := strings.ToLower(v.name) - return lowerName != "undefined" && exists(lowerName) + return lowerName != "undefined" && isDirectory(lowerName) } -// exists returns whether the given file or directory exists -func exists(path string) bool { - _, err := os.Stat(path) - if err == nil { - return true - } - if os.IsNotExist(err) { +// isDirectory reports whether the named file is a directory. +func isDirectory(path string) bool { + info, err := os.Stat(path) + if err != nil { return false } - return false + return info.IsDir() } -// isDirectory reports whether the named file is a directory. -func isDirectory(name string) bool { - info, err := os.Stat(name) +func isDirectoryFatal(path string) bool { + info, err := os.Stat(path) if err != nil { log.Fatal(err) } From 6895fc7710aa60778d92b539b42c20a4bafc7552 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksa=20Siri=C5=A1ki?= <31509435+aleksasiriski@users.noreply.github.com> Date: Tue, 26 Sep 2023 22:49:38 +0200 Subject: [PATCH 5/8] Fixed out of band slice --- generate/searcher/searcher.go | 54 +++++------------------------------ generate/searcher/util.go | 33 --------------------- 2 files changed, 7 insertions(+), 80 deletions(-) diff --git a/generate/searcher/searcher.go b/generate/searcher/searcher.go index df94d103..d1bfece5 100644 --- a/generate/searcher/searcher.go +++ b/generate/searcher/searcher.go @@ -1,7 +1,6 @@ package main import ( - "bytes" "flag" "fmt" "go/ast" @@ -67,7 +66,7 @@ func main() { trimPrefix: *trimprefix, lineComment: *linecomment, } - // TODO(suzmue): accept other patterns for packages (directories, list of files, import paths, etc). + if len(args) == 1 && isDirectoryFatal(args[0]) { dir = args[0] } else { @@ -120,9 +119,7 @@ func (g *Generator) Printf(format string, args ...interface{}) { // parsePackage exits if there is an error. func (g *Generator) parsePackage(patterns []string, tags []string) { cfg := &packages.Config{ - Mode: packages.NeedName | packages.NeedTypes | packages.NeedTypesInfo | packages.NeedSyntax, - // TODO: Need to think about constants in test files. Maybe write type_string_test.go - // in a separate pass? For later. + Mode: packages.NeedName | packages.NeedTypes | packages.NeedTypesInfo | packages.NeedSyntax, Tests: false, BuildFlags: []string{fmt.Sprintf("-tags=%s", strings.Join(tags, " "))}, Logf: g.logf, @@ -176,8 +173,6 @@ func (g *Generator) generate(typeName string) { for _, v := range values { if validConst(v) { g.Printf("import \"%s/%s\"\n", *enginesImport, strings.ToLower(v.originalName)) - } else { - values = values.Delete(v) } } @@ -310,50 +305,15 @@ func (f *File) genDecl(node ast.Node) bool { return false } -// Helpers - -// declareIndexAndNameVar is the single-run version of declareIndexAndNameVars -func (g *Generator) declareIndexAndNameVar(run Values, typeName string) { - index, name := g.createIndexAndNameDecl(run, typeName, "") - g.Printf("const %s\n", name) - g.Printf("var %s\n", index) -} - -// createIndexAndNameDecl returns the pair of declarations for the run. The caller will add "const" and "var". -func (g *Generator) createIndexAndNameDecl(run Values, typeName string, suffix string) (string, string) { - b := new(bytes.Buffer) - indexes := make([]int, len(run)) - for i := range run { - b.WriteString(run[i].name) - indexes[i] = b.Len() - } - nameConst := fmt.Sprintf("_%s_name%s = %q", typeName, suffix, b.String()) - nameLen := b.Len() - b.Reset() - fmt.Fprintf(b, "_%s_index%s = [...]uint%d{0, ", typeName, suffix, usize(nameLen)) - for i, v := range indexes { - if i > 0 { - fmt.Fprintf(b, ", ") - } - fmt.Fprintf(b, "%d", v) - } - fmt.Fprintf(b, "}") - return b.String(), nameConst -} - // buildOneRun generates the variables and String method for a single run of contiguous values. func (g *Generator) buildOneRun(values Values, typeName string) { g.Printf("\n") - g.declareIndexAndNameVar(values, typeName) // The generated code is simple enough to write as a Printf format. - g.Printf(` -func NewEngineStarter() []EngineSearch { - mm := make([]EngineSearch, %d)`, len(values)) + g.Printf("\nfunc NewEngineStarter() []EngineSearch {\n\tmm := make([]EngineSearch, %d)", len(values)) for _, v := range values { - g.Printf(` - mm[%s.%s] = %s.Search`, g.pkg.name, v.name, strings.ToLower(v.name)) + if validConst(v) { + g.Printf("\n\tmm[%s.%s] = %s.Search", g.pkg.name, v.name, strings.ToLower(v.name)) + } } - g.Printf(` - return mm -}`) + g.Printf("\n\treturn mm\n}") } diff --git a/generate/searcher/util.go b/generate/searcher/util.go index 327e7f83..450bb35c 100644 --- a/generate/searcher/util.go +++ b/generate/searcher/util.go @@ -1,29 +1,11 @@ package main import ( - "fmt" "log" "os" "strings" ) -func (s Values) Delete(v Value) []Value { - index := s.IndexOf(v) - ret := make([]Value, 0) - ret = append(ret, s[:index]...) - return append(ret, s[index+1:]...) -} - -func (s Values) IndexOf(x Value) int { - for i, v := range s { - if x == v { - return i - } - } - log.Fatal(fmt.Errorf("indexOf func failed")) - return -1 -} - func validConst(v Value) bool { lowerName := strings.ToLower(v.name) return lowerName != "undefined" && isDirectory(lowerName) @@ -45,18 +27,3 @@ func isDirectoryFatal(path string) bool { } return info.IsDir() } - -// usize returns the number of bits of the smallest unsigned integer -// type that will hold n. Used to create the smallest possible slice of -// integers to use as indexes into the concatenated strings. -func usize(n int) int { - switch { - case n < 1<<8: - return 8 - case n < 1<<16: - return 16 - default: - // 2^32 is enough constants for anyone. - return 32 - } -} From 519730b35f746a5af6f12e91a491922c5f09a824 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksa=20Siri=C5=A1ki?= <31509435+aleksasiriski@users.noreply.github.com> Date: Tue, 26 Sep 2023 22:55:10 +0200 Subject: [PATCH 6/8] Fixed comment --- generate/searcher/searcher.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generate/searcher/searcher.go b/generate/searcher/searcher.go index d1bfece5..79825c72 100644 --- a/generate/searcher/searcher.go +++ b/generate/searcher/searcher.go @@ -305,7 +305,7 @@ func (f *File) genDecl(node ast.Node) bool { return false } -// buildOneRun generates the variables and String method for a single run of contiguous values. +// buildOneRun generates the variables and NewEngineStarter func for a single run of contiguous values. func (g *Generator) buildOneRun(values Values, typeName string) { g.Printf("\n") // The generated code is simple enough to write as a Printf format. From 21b9ed4ad275e57d403070d96d73d4bc1aa3eaba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksa=20Siri=C5=A1ki?= <31509435+aleksasiriski@users.noreply.github.com> Date: Tue, 26 Sep 2023 23:00:53 +0200 Subject: [PATCH 7/8] Switch filepath to path --- generate/searcher/searcher.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/generate/searcher/searcher.go b/generate/searcher/searcher.go index 79825c72..05d8e34f 100644 --- a/generate/searcher/searcher.go +++ b/generate/searcher/searcher.go @@ -10,7 +10,7 @@ import ( "go/types" "log" "os" - "path/filepath" + "path" "strings" "golang.org/x/tools/go/packages" @@ -73,7 +73,7 @@ func main() { if len(tags) != 0 { log.Fatal("-tags option applies only to directories, not when files are specified") } - dir = filepath.Dir(args[0]) + dir = path.Dir(args[0]) } g.parsePackage(args, tags) @@ -103,7 +103,7 @@ func main() { outputName := *output if outputName == "" { baseName := fmt.Sprintf("%s_searcher.go", types[0]) - outputName = filepath.Join(dir, strings.ToLower(baseName)) + outputName = path.Join(dir, strings.ToLower(baseName)) } err := os.WriteFile(outputName, src, 0644) if err != nil { From 503c714bf6438c5f7bff767264f48779988bcf5f Mon Sep 17 00:00:00 2001 From: hiddenMedic <124312252+hiddenMedic@users.noreply.github.com> Date: Tue, 26 Sep 2023 23:27:51 +0200 Subject: [PATCH 8/8] improved gitignore, removed search/structs.go --- .gitignore | 7 ++----- src/search/search.go | 3 +++ src/search/structs.go | 11 ----------- 3 files changed, 5 insertions(+), 16 deletions(-) delete mode 100644 src/search/structs.go diff --git a/.gitignore b/.gitignore index 17c3214f..2ea9ee15 100644 --- a/.gitignore +++ b/.gitignore @@ -25,11 +25,8 @@ brzaguza.* .vscode/* -src/engines/yandex/site/* -src/engines/etools/site/* -src/engines/swisscows/site/* -src/engines/metager/site/* -src/engines/presearch/site/* +src/engines/*/site/* +!src/engines/_engines_test logdump/*.html log/*.log diff --git a/src/search/search.go b/src/search/search.go index 833cc0b5..0151a074 100644 --- a/src/search/search.go +++ b/src/search/search.go @@ -41,6 +41,9 @@ func PerformSearch(query string, options engines.Options, config *config.Config) return results } +// engine_searcher, NewEngineStarter() use this. +type EngineSearch func(context.Context, string, *bucket.Relay, engines.Options, config.Settings) error + func runEngines(engineMap map[string]config.Engine, query string, worker *conc.WaitGroup, relay *bucket.Relay, options engines.Options) { log.Info().Msgf("Enabled engines: %v", config.EnabledEngines) diff --git a/src/search/structs.go b/src/search/structs.go deleted file mode 100644 index 2542fd89..00000000 --- a/src/search/structs.go +++ /dev/null @@ -1,11 +0,0 @@ -package search - -import ( - "context" - - "github.com/tminaorg/brzaguza/src/bucket" - "github.com/tminaorg/brzaguza/src/config" - "github.com/tminaorg/brzaguza/src/engines" -) - -type EngineSearch func(context.Context, string, *bucket.Relay, engines.Options, config.Settings) error