github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/command/license.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"time"
     7  
     8  	"github.com/hashicorp/nomad/api"
     9  	"github.com/mitchellh/cli"
    10  )
    11  
    12  var _ cli.Command = &LicenseCommand{}
    13  
    14  type LicenseCommand struct {
    15  	Meta
    16  }
    17  
    18  func (l *LicenseCommand) Help() string {
    19  	helpText := `
    20  Usage: nomad license <subcommand> [options] [args]
    21  	
    22  This command has subcommands for managing the Nomad Enterprise license.
    23  For more detailed examples see:
    24  https://www.nomadproject.io/docs/commands/license/
    25  
    26  Install a new license from a file:
    27  	$ nomad license put <path>
    28  
    29  Install a new license from stdin:
    30  	$ nomad license put -
    31  
    32  Retrieve the current license:
    33  
    34  	$ nomad license get
    35  
    36  	`
    37  	return strings.TrimSpace(helpText)
    38  }
    39  
    40  func (l *LicenseCommand) Synopsis() string {
    41  	return "Interact with Nomad Enterprise License"
    42  }
    43  
    44  func (l *LicenseCommand) Name() string { return "license" }
    45  
    46  func (l *LicenseCommand) Run(args []string) int {
    47  	return cli.RunResultHelp
    48  }
    49  
    50  func OutputLicenseReply(ui cli.Ui, resp *api.LicenseReply) int {
    51  	var validity string
    52  	now := time.Now()
    53  	if resp.License.ExpirationTime.Before(now) {
    54  		validity = "expired!"
    55  		outputLicenseInfo(ui, resp.License, true, validity)
    56  		return 1
    57  	}
    58  	validity = "valid"
    59  	outputLicenseInfo(ui, resp.License, false, validity)
    60  	return 0
    61  }
    62  
    63  func outputLicenseInfo(ui cli.Ui, lic *api.License, expired bool, validity string) {
    64  	expStr := ""
    65  	if expired {
    66  		expStr = fmt.Sprintf("Expired At|%s", lic.ExpirationTime.String())
    67  	} else {
    68  		expStr = fmt.Sprintf("Expires At|%s", lic.ExpirationTime.String())
    69  	}
    70  
    71  	output := []string{
    72  		fmt.Sprintf("Product|%s", lic.Product),
    73  		fmt.Sprintf("License Status|%s", validity),
    74  		fmt.Sprintf("License ID|%s", lic.LicenseID),
    75  		fmt.Sprintf("Customer ID|%s", lic.CustomerID),
    76  		expStr,
    77  		fmt.Sprintf("Terminates At|%s", lic.TerminationTime.String()),
    78  		fmt.Sprintf("Datacenter|%s", lic.InstallationID),
    79  	}
    80  	ui.Output(formatKV(output))
    81  
    82  	if len(lic.Modules) > 0 {
    83  		ui.Output("Modules:")
    84  		for _, mod := range lic.Modules {
    85  			ui.Output(fmt.Sprintf("\t%s", mod))
    86  		}
    87  	}
    88  	if len(lic.Features) > 0 {
    89  		ui.Output("Licensed Features:")
    90  		for _, f := range lic.Features {
    91  			ui.Output(fmt.Sprintf("\t%s", f))
    92  		}
    93  	}
    94  }