github.com/DerekStrickland/consul@v1.4.5/command/connect/ca/get/connect_ca_get.go (about)

     1  package get
     2  
     3  import (
     4  	"encoding/json"
     5  	"flag"
     6  	"fmt"
     7  
     8  	"github.com/hashicorp/consul/api"
     9  	"github.com/hashicorp/consul/command/flags"
    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  
    26  func (c *cmd) init() {
    27  	c.flags = flag.NewFlagSet("", flag.ContinueOnError)
    28  	c.http = &flags.HTTPFlags{}
    29  	flags.Merge(c.flags, c.http.ClientFlags())
    30  	flags.Merge(c.flags, c.http.ServerFlags())
    31  	c.help = flags.Usage(help, c.flags)
    32  }
    33  
    34  func (c *cmd) Run(args []string) int {
    35  	if err := c.flags.Parse(args); err != nil {
    36  		if err == flag.ErrHelp {
    37  			return 0
    38  		}
    39  		c.UI.Error(fmt.Sprintf("Failed to parse args: %v", err))
    40  		return 1
    41  	}
    42  
    43  	// Set up a client.
    44  	client, err := c.http.APIClient()
    45  	if err != nil {
    46  		c.UI.Error(fmt.Sprintf("Error initializing client: %s", err))
    47  		return 1
    48  	}
    49  
    50  	// Fetch the current configuration.
    51  	opts := &api.QueryOptions{
    52  		AllowStale: c.http.Stale(),
    53  	}
    54  	config, _, err := client.Connect().CAGetConfig(opts)
    55  	if err != nil {
    56  		c.UI.Error(fmt.Sprintf("Error querying CA configuration: %s", err))
    57  		return 1
    58  	}
    59  	output, err := json.MarshalIndent(config, "", "\t")
    60  	if err != nil {
    61  		c.UI.Error(fmt.Sprintf("Error formatting CA configuration: %s", err))
    62  	}
    63  	c.UI.Output(string(output))
    64  
    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 = "Display the current Connect Certificate Authority (CA) configuration"
    77  const help = `
    78  Usage: consul connect ca get-config [options]
    79  
    80    Displays the current Connect Certificate Authority (CA) configuration.
    81  `