kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/services/cli/command_identifiers.go (about) 1 /* 2 * Copyright 2017 The Kythe Authors. All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package cli 18 19 import ( 20 "context" 21 "errors" 22 "flag" 23 "fmt" 24 "strings" 25 26 ipb "kythe.io/kythe/proto/identifier_go_proto" 27 ) 28 29 type identCommand struct { 30 baseKytheCommand 31 corpora, languages string 32 } 33 34 func (identCommand) Name() string { return "identifier" } 35 func (identCommand) Synopsis() string { return "list tickets associated with a given identifier" } 36 func (identCommand) Usage() string { return "<identifier>" } 37 func (c *identCommand) SetFlags(flag *flag.FlagSet) { 38 flag.StringVar(&c.corpora, "corpora", "", "Comma-separated list of corpora with which to restrict matches") 39 flag.StringVar(&c.languages, "languages", "", "Comma-separated list of languages with which to restrict matches") 40 } 41 func (c identCommand) Run(ctx context.Context, flag *flag.FlagSet, api API) error { 42 if flag.NArg() == 0 { 43 return errors.New("identifier missing") 44 } else if flag.NArg() > 1 { 45 return fmt.Errorf("only 1 identifier may be given; found: %v", flag.Args()) 46 } 47 48 req := &ipb.FindRequest{ 49 Identifier: flag.Arg(0), 50 } 51 if c.corpora != "" { 52 req.Corpus = strings.Split(c.corpora, ",") 53 } 54 if c.languages != "" { 55 req.Languages = strings.Split(c.languages, ",") 56 } 57 58 LogRequest(req) 59 reply, err := api.IdentifierService.Find(ctx, req) 60 if err != nil { 61 return err 62 } 63 64 return c.displayMatches(reply) 65 } 66 67 func (c identCommand) displayMatches(reply *ipb.FindReply) error { 68 if DisplayJSON { 69 return PrintJSONMessage(reply) 70 } 71 72 for _, m := range reply.Matches { 73 kind := m.NodeKind 74 if m.NodeSubkind != "" { 75 kind += "/" + m.NodeSubkind 76 } 77 fmt.Printf("%s [kind: %s]\n", m.Ticket, kind) 78 } 79 return nil 80 }