github.com/criteo-forks/consul@v1.4.5-criteonogrpc/command/intention/delete/delete.go (about)

     1  package delete
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"io"
     7  
     8  	"github.com/hashicorp/consul/command/flags"
     9  	"github.com/hashicorp/consul/command/intention/finder"
    10  	"github.com/mitchellh/cli"
    11  )
    12  
    13  func New(ui cli.Ui) *cmd {
    14  	c := &cmd{UI: ui}
    15  	c.init()
    16  	return c
    17  }
    18  
    19  type cmd struct {
    20  	UI    cli.Ui
    21  	flags *flag.FlagSet
    22  	http  *flags.HTTPFlags
    23  	help  string
    24  
    25  	// testStdin is the input for testing.
    26  	testStdin io.Reader
    27  }
    28  
    29  func (c *cmd) init() {
    30  	c.flags = flag.NewFlagSet("", flag.ContinueOnError)
    31  	c.http = &flags.HTTPFlags{}
    32  	flags.Merge(c.flags, c.http.ClientFlags())
    33  	flags.Merge(c.flags, c.http.ServerFlags())
    34  	c.help = flags.Usage(help, c.flags)
    35  }
    36  
    37  func (c *cmd) Run(args []string) int {
    38  	if err := c.flags.Parse(args); err != nil {
    39  		return 1
    40  	}
    41  
    42  	// Create and test the HTTP client
    43  	client, err := c.http.APIClient()
    44  	if err != nil {
    45  		c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
    46  		return 1
    47  	}
    48  
    49  	// Get the intention ID to load
    50  	f := &finder.Finder{Client: client}
    51  	id, err := f.IDFromArgs(c.flags.Args())
    52  	if err != nil {
    53  		c.UI.Error(fmt.Sprintf("Error: %s", err))
    54  		return 1
    55  	}
    56  
    57  	// Read the intention
    58  	_, err = client.Connect().IntentionDelete(id, nil)
    59  	if err != nil {
    60  		c.UI.Error(fmt.Sprintf("Error reading the intention: %s", err))
    61  		return 1
    62  	}
    63  
    64  	c.UI.Output(fmt.Sprintf("Intention deleted."))
    65  	return 0
    66  }
    67  
    68  func (c *cmd) Synopsis() string {
    69  	return synopsis
    70  }
    71  
    72  func (c *cmd) Help() string {
    73  	return c.help
    74  }
    75  
    76  const synopsis = "Delete an intention."
    77  const help = `
    78  Usage: consul intention delete [options] SRC DST
    79  Usage: consul intention delete [options] ID
    80  
    81    Delete an intention. This cannot be reversed. The intention can be looked
    82    up via an exact source/destination match or via the unique intention ID.
    83  
    84        $ consul intention delete web db
    85  
    86  `