MCPcopy
hub / github.com/perkeep/perkeep / RunCommand

Method RunCommand

cmd/pk/search.go:72–142  ·  view source on GitHub ↗
(args []string)

Source from the content-addressed store, hash-verified

70}
71
72func (c *searchCmd) RunCommand(args []string) error {
73 if len(args) != 1 {
74 return cmdmain.UsageError("requires search expression or Constraint JSON")
75 }
76 q := args[0]
77 if q == "-" {
78 slurp, err := io.ReadAll(cmdmain.Stdin)
79 if err != nil {
80 return err
81 }
82 q = string(slurp)
83 }
84 q = strings.TrimSpace(q)
85
86 req := &search.SearchQuery{
87 Limit: c.limit,
88 Continue: c.cont,
89 }
90 if c.rawQuery {
91 req.Limit = 0 // clear it if they provided it
92 req.Continue = "" // clear this as well
93
94 if err := json.NewDecoder(strings.NewReader(q)).Decode(&req); err != nil {
95 if se, ok := err.(*json.SyntaxError); ok {
96 line, col, msg := errorutil.HighlightBytePosition(strings.NewReader(q), se.Offset)
97 fmt.Fprintf(os.Stderr, "JSON syntax error at line %d, column %d parsing SearchQuery (https://godoc.org/perkeep.org/pkg/search#SearchQuery):\n%s\n", line, col, msg)
98 }
99 return err
100 }
101 if c.limit != 0 {
102 req.Limit = c.limit
103 }
104 if c.cont != "" {
105 req.Continue = c.cont
106 }
107 } else if strutil.IsPlausibleJSON(q) {
108 cs := new(search.Constraint)
109 if err := json.NewDecoder(strings.NewReader(q)).Decode(&cs); err != nil {
110 if se, ok := err.(*json.SyntaxError); ok {
111 line, col, msg := errorutil.HighlightBytePosition(strings.NewReader(q), se.Offset)
112 fmt.Fprintf(os.Stderr, "JSON syntax error at line %d, column %d parsing Constraint (https://godoc.org/perkeep.org/pkg/search#Constraint):\n%s\n", line, col, msg)
113 }
114 return err
115 }
116 req.Constraint = cs
117 } else {
118 req.Expression = q
119 }
120 if c.describe {
121 req.Describe = &search.DescribeRequest{}
122 }
123
124 cl := newClient(c.server)
125 res, err := cl.Query(ctxbg, req)
126 if err != nil {
127 return err
128 }
129 if c.one {

Callers

nothing calls this directly

Calls 6

UsageErrorTypeAlias · 0.92
ReadAllMethod · 0.80
PrintlnMethod · 0.80
newClientFunction · 0.70
QueryMethod · 0.65
WriteMethod · 0.45

Tested by

no test coverage detected