kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/services/cli/command_diagnostics.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 "flag" 22 "fmt" 23 "strings" 24 25 cpb "kythe.io/kythe/proto/common_go_proto" 26 xpb "kythe.io/kythe/proto/xref_go_proto" 27 ) 28 29 type diagnosticsCommand struct { 30 baseDecorCommand 31 } 32 33 func (diagnosticsCommand) Name() string { return "diagnostics" } 34 func (diagnosticsCommand) Aliases() []string { return []string{"diags"} } 35 func (diagnosticsCommand) Synopsis() string { return "list a file's diagnostics" } 36 func (diagnosticsCommand) Usage() string { return "" } 37 func (c *diagnosticsCommand) SetFlags(flag *flag.FlagSet) { 38 c.baseDecorCommand.SetFlags(flag) 39 } 40 func (c diagnosticsCommand) Run(ctx context.Context, flag *flag.FlagSet, api API) error { 41 req, err := c.baseRequest(flag) 42 if err != nil { 43 return err 44 } 45 req.Diagnostics = true 46 47 LogRequest(req) 48 reply, err := api.XRefService.Decorations(ctx, req) 49 if err != nil { 50 return err 51 } 52 53 return c.displayDiagnostics(reply) 54 } 55 56 func (c diagnosticsCommand) displayDiagnostics(decor *xpb.DecorationsReply) error { 57 if DisplayJSON { 58 return PrintJSONMessage(decor) 59 } 60 61 for _, d := range decor.Diagnostic { 62 span := spanToString(d.Span) 63 if span != "" { 64 span += " " 65 } 66 fmt.Fprintf(out, "* %s%s\n", span, d.Message) 67 if d.ContextUrl != "" { 68 fmt.Fprintf(out, " - Context: %s\n", d.ContextUrl) 69 } 70 if d.Details != "" { 71 fmt.Fprintln(out, " - Details:") 72 for _, line := range strings.Split(d.Details, "\n") { 73 fmt.Fprintf(out, " > %s\n", line) 74 } 75 } 76 } 77 return nil 78 } 79 80 func spanToString(s *cpb.Span) string { 81 if s == nil { 82 return "" 83 } 84 return fmt.Sprintf("%d:%d-%d:%d", 85 s.Start.GetLineNumber(), s.Start.GetColumnOffset(), 86 s.End.GetLineNumber(), s.End.GetColumnOffset()) 87 }