github.com/kjdelisle/consul@v1.4.5/command/intention/get/get.go (about)

     1  package get
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"io"
     7  	"sort"
     8  	"time"
     9  
    10  	"github.com/hashicorp/consul/command/flags"
    11  	"github.com/hashicorp/consul/command/intention/finder"
    12  	"github.com/mitchellh/cli"
    13  	"github.com/ryanuber/columnize"
    14  )
    15  
    16  func New(ui cli.Ui) *cmd {
    17  	c := &cmd{UI: ui}
    18  	c.init()
    19  	return c
    20  }
    21  
    22  type cmd struct {
    23  	UI    cli.Ui
    24  	flags *flag.FlagSet
    25  	http  *flags.HTTPFlags
    26  	help  string
    27  
    28  	// testStdin is the input for testing.
    29  	testStdin io.Reader
    30  }
    31  
    32  func (c *cmd) init() {
    33  	c.flags = flag.NewFlagSet("", flag.ContinueOnError)
    34  	c.http = &flags.HTTPFlags{}
    35  	flags.Merge(c.flags, c.http.ClientFlags())
    36  	flags.Merge(c.flags, c.http.ServerFlags())
    37  	c.help = flags.Usage(help, c.flags)
    38  }
    39  
    40  func (c *cmd) Run(args []string) int {
    41  	if err := c.flags.Parse(args); err != nil {
    42  		return 1
    43  	}
    44  
    45  	// Create and test the HTTP client
    46  	client, err := c.http.APIClient()
    47  	if err != nil {
    48  		c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
    49  		return 1
    50  	}
    51  
    52  	// Get the intention ID to load
    53  	f := &finder.Finder{Client: client}
    54  	id, err := f.IDFromArgs(c.flags.Args())
    55  	if err != nil {
    56  		c.UI.Error(fmt.Sprintf("Error: %s", err))
    57  		return 1
    58  	}
    59  
    60  	// Read the intention
    61  	ixn, _, err := client.Connect().IntentionGet(id, nil)
    62  	if err != nil {
    63  		c.UI.Error(fmt.Sprintf("Error reading the intention: %s", err))
    64  		return 1
    65  	}
    66  
    67  	// Format the tabular data
    68  	data := []string{
    69  		fmt.Sprintf("Source:|%s", ixn.SourceString()),
    70  		fmt.Sprintf("Destination:|%s", ixn.DestinationString()),
    71  		fmt.Sprintf("Action:|%s", ixn.Action),
    72  		fmt.Sprintf("ID:|%s", ixn.ID),
    73  	}
    74  	if v := ixn.Description; v != "" {
    75  		data = append(data, fmt.Sprintf("Description:|%s", v))
    76  	}
    77  	if len(ixn.Meta) > 0 {
    78  		var keys []string
    79  		for k := range ixn.Meta {
    80  			keys = append(keys, k)
    81  		}
    82  		sort.Strings(keys)
    83  		for _, k := range keys {
    84  			data = append(data, fmt.Sprintf("Meta[%s]:|%s", k, ixn.Meta[k]))
    85  		}
    86  	}
    87  	data = append(data,
    88  		fmt.Sprintf("Created At:|%s", ixn.CreatedAt.Local().Format(time.RFC850)),
    89  	)
    90  
    91  	c.UI.Output(columnize.SimpleFormat(data))
    92  	return 0
    93  }
    94  
    95  func (c *cmd) Synopsis() string {
    96  	return synopsis
    97  }
    98  
    99  func (c *cmd) Help() string {
   100  	return c.help
   101  }
   102  
   103  const synopsis = "Show information about an intention."
   104  const help = `
   105  Usage: consul intention get [options] SRC DST
   106  Usage: consul intention get [options] ID
   107  
   108    Read and show the details about an intention. The intention can be looked
   109    up via an exact source/destination match or via the unique intention ID.
   110  
   111        $ consul intention get web db
   112  
   113  `