kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/services/cli/command_nodes.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  	gpb "kythe.io/kythe/proto/graph_go_proto"
    27  )
    28  
    29  type nodesCommand struct {
    30  	baseKytheCommand
    31  	nodeFilters       string
    32  	factSizeThreshold int
    33  }
    34  
    35  func (nodesCommand) Name() string     { return "nodes" }
    36  func (nodesCommand) Synopsis() string { return "retrieve a node's facts" }
    37  func (c *nodesCommand) SetFlags(flag *flag.FlagSet) {
    38  	flag.StringVar(&c.nodeFilters, "filters", "", "Comma-separated list of node fact filters (default returns all)")
    39  	flag.IntVar(&c.factSizeThreshold, "max_fact_size", 64,
    40  		"Maximum size of fact values to display.  Facts with byte lengths longer than this value will only have their fact names displayed.")
    41  }
    42  func (c nodesCommand) Run(ctx context.Context, flag *flag.FlagSet, api API) error {
    43  	if c.factSizeThreshold < 0 {
    44  		return fmt.Errorf("invalid --max_fact_size value (must be non-negative): %d", c.factSizeThreshold)
    45  	}
    46  
    47  	req := &gpb.NodesRequest{
    48  		Ticket: flag.Args(),
    49  	}
    50  	if c.nodeFilters != "" {
    51  		req.Filter = strings.Split(c.nodeFilters, ",")
    52  	}
    53  	LogRequest(req)
    54  	reply, err := api.GraphService.Nodes(ctx, req)
    55  	if err != nil {
    56  		return err
    57  	}
    58  	return c.displayNodes(reply.Nodes)
    59  }
    60  
    61  func (c *nodesCommand) displayNodes(nodes map[string]*cpb.NodeInfo) error {
    62  	if DisplayJSON {
    63  		return PrintJSON(nodes)
    64  	}
    65  
    66  	for ticket, n := range nodes {
    67  		if _, err := fmt.Fprintln(out, ticket); err != nil {
    68  			return err
    69  		}
    70  		for name, value := range n.Facts {
    71  			if len(value) <= c.factSizeThreshold {
    72  				if _, err := fmt.Fprintf(out, "  %s\t%s\n", name, value); err != nil {
    73  					return err
    74  				}
    75  			} else {
    76  				if _, err := fmt.Fprintf(out, "  %s\t%s<truncated>\n", name, value[:c.factSizeThreshold]); err != nil {
    77  					return err
    78  				}
    79  			}
    80  		}
    81  	}
    82  	return nil
    83  }