File: package_search.go

package info (click to toggle)
aptly 1.6.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 49,928 kB
  • sloc: python: 10,398; sh: 252; makefile: 184
file content (69 lines) | stat: -rw-r--r-- 1,597 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package cmd

import (
	"fmt"

	"github.com/aptly-dev/aptly/deb"
	"github.com/aptly-dev/aptly/query"
	"github.com/smira/commander"
	"github.com/smira/flag"
)

func aptlyPackageSearch(cmd *commander.Command, args []string) error {
	var (
		err error
		q   deb.PackageQuery
	)

	if len(args) > 1 {
		cmd.Usage()
		return commander.ErrCommandError
	}

	if len(args) == 1 {
		value, err := GetStringOrFileContent(args[0])
		if err != nil {
			return fmt.Errorf("unable to read package query from file %s: %w", args[0], err)
		}
		q, err = query.Parse(value)
		if err != nil {
			return fmt.Errorf("unable to search: %s", err)
		}
	} else {
		q = &deb.MatchAllQuery{}
	}

	collectionFactory := context.NewCollectionFactory()
	result := q.Query(collectionFactory.PackageCollection())
	if result.Len() == 0 {
		return fmt.Errorf("no results")
	}

	format := context.Flags().Lookup("format").Value.String()
	_ = PrintPackageList(result, format, "")

	return err
}

func makeCmdPackageSearch() *commander.Command {
	cmd := &commander.Command{
		Run:       aptlyPackageSearch,
		UsageLine: "search [<package-query>]",
		Short:     "search for packages matching query",
		Long: `
Command search displays list of packages in whole DB that match package query.

Use '@file' to read query from file or '@-' for stdin.
If query is not specified, all the packages are displayed.

Example:

    $ aptly package search '$Architecture (i386), Name (% *-dev)'
`,
		Flag: *flag.NewFlagSet("aptly-package-search", flag.ExitOnError),
	}

	cmd.Flag.String("format", "", "custom format for result printing")

	return cmd
}