2022-08-15 17:43:29 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2022-09-09 21:55:20 +00:00
|
|
|
"errors"
|
2022-08-15 17:43:29 +00:00
|
|
|
"fmt"
|
2022-09-09 21:55:20 +00:00
|
|
|
"io/fs"
|
2022-08-15 17:43:29 +00:00
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/muesli/reflow/wordwrap"
|
|
|
|
"github.com/posener/complete"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// DefaultHclVarInitName is the default name we use when initializing the
|
|
|
|
// example var file in HCL format
|
2022-09-12 20:26:18 +00:00
|
|
|
DefaultHclVarInitName = "spec.nv.hcl"
|
2022-08-15 17:43:29 +00:00
|
|
|
|
|
|
|
// DefaultHclVarInitName is the default name we use when initializing the
|
|
|
|
// example var file in JSON format
|
2022-09-12 20:26:18 +00:00
|
|
|
DefaultJsonVarInitName = "spec.nv.json"
|
2022-08-15 17:43:29 +00:00
|
|
|
)
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// VarInitCommand generates a new variable specification
|
2022-08-15 17:43:29 +00:00
|
|
|
type VarInitCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *VarInitCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: nomad var init <filename>
|
|
|
|
|
2022-09-09 21:55:20 +00:00
|
|
|
Creates an example variable specification file that can be used as a starting
|
|
|
|
point to customize further. When no filename is supplied, a default filename
|
2022-09-12 20:26:18 +00:00
|
|
|
of "spec.nv.hcl" or "spec.nv.json" will be used depending on the output
|
2022-09-09 21:55:20 +00:00
|
|
|
format.
|
2022-08-15 17:43:29 +00:00
|
|
|
|
|
|
|
Init Options:
|
|
|
|
|
2022-09-09 21:55:20 +00:00
|
|
|
-out (hcl | json)
|
|
|
|
Format of generated variable specification. Defaults to "hcl".
|
2022-08-15 17:43:29 +00:00
|
|
|
|
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *VarInitCommand) Synopsis() string {
|
2022-08-26 18:03:56 +00:00
|
|
|
return "Create an example variable specification file"
|
2022-08-15 17:43:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *VarInitCommand) AutocompleteFlags() complete.Flags {
|
|
|
|
return complete.Flags{
|
2022-09-09 21:55:20 +00:00
|
|
|
"-out": complete.PredictSet("hcl", "json"),
|
2022-08-15 17:43:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *VarInitCommand) AutocompleteArgs() complete.Predictor {
|
|
|
|
return complete.PredictNothing
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *VarInitCommand) Name() string { return "var init" }
|
|
|
|
|
|
|
|
func (c *VarInitCommand) Run(args []string) int {
|
2022-09-09 21:55:20 +00:00
|
|
|
var outFmt string
|
2022-08-15 17:43:29 +00:00
|
|
|
var quiet bool
|
|
|
|
|
|
|
|
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
|
|
|
|
flags.Usage = func() { c.Ui.Output(c.Help()) }
|
2022-09-09 21:55:20 +00:00
|
|
|
flags.StringVar(&outFmt, "out", "hcl", "")
|
2022-08-15 17:43:29 +00:00
|
|
|
|
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we get no arguments
|
|
|
|
args = flags.Args()
|
|
|
|
if l := len(args); l > 1 {
|
|
|
|
c.Ui.Error("This command takes no arguments or one: <filename>")
|
|
|
|
c.Ui.Error(commandErrorText(c))
|
|
|
|
return 1
|
|
|
|
}
|
2022-09-09 21:55:20 +00:00
|
|
|
var fileName, fileContent string
|
|
|
|
switch outFmt {
|
|
|
|
case "hcl":
|
|
|
|
fileName = DefaultHclVarInitName
|
|
|
|
fileContent = defaultHclVarSpec
|
|
|
|
case "json":
|
2022-08-15 17:43:29 +00:00
|
|
|
fileName = DefaultJsonVarInitName
|
|
|
|
fileContent = defaultJsonVarSpec
|
|
|
|
}
|
2022-09-09 21:55:20 +00:00
|
|
|
|
2022-08-15 17:43:29 +00:00
|
|
|
if len(args) == 1 {
|
|
|
|
fileName = args[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the file already exists
|
|
|
|
_, err := os.Stat(fileName)
|
2022-09-09 21:55:20 +00:00
|
|
|
if err == nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("File %q already exists", fileName))
|
2022-08-15 17:43:29 +00:00
|
|
|
return 1
|
|
|
|
}
|
2022-09-09 21:55:20 +00:00
|
|
|
if err != nil && !errors.Is(err, fs.ErrNotExist) {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to stat %q: %v", fileName, err))
|
2022-08-15 17:43:29 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write out the example
|
2022-09-09 21:55:20 +00:00
|
|
|
err = os.WriteFile(fileName, []byte(fileContent), 0660)
|
2022-08-15 17:43:29 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to write %q: %v", fileName, err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Success
|
|
|
|
if !quiet {
|
2022-09-09 21:55:20 +00:00
|
|
|
if outFmt == "json" {
|
|
|
|
c.Ui.Info(wrapString(tidyRawString(strings.ReplaceAll(msgOnlyItemsRequired, "items", "Items")), 70))
|
|
|
|
c.Ui.Warn(wrapString(tidyRawString(strings.ReplaceAll(msgWarnKeys, "items", "Items")), 70))
|
|
|
|
}
|
2022-08-26 18:03:56 +00:00
|
|
|
c.Ui.Output(fmt.Sprintf("Example variable specification written to %s", fileName))
|
2022-08-15 17:43:29 +00:00
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
msgWarnKeys = `
|
|
|
|
REMINDER: While keys in the items map can contain dots, using them in
|
|
|
|
templates is easier when they do not. As a best practice, avoid dotted
|
|
|
|
keys when possible.`
|
2022-09-09 21:55:20 +00:00
|
|
|
msgOnlyItemsRequired = `
|
|
|
|
The items map is the only strictly required part of a variable
|
|
|
|
specification, since path and namespace can be set via other means. It
|
|
|
|
contains the sensitive material to encrypt and store as a Nomad variable.
|
|
|
|
The entire items map is encrypted and decrypted as a single unit.`
|
2022-08-15 17:43:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var defaultHclVarSpec = strings.TrimSpace(`
|
2022-08-26 18:03:56 +00:00
|
|
|
# A variable path can be specified in the specification file
|
2022-08-15 17:43:29 +00:00
|
|
|
# and will be used when writing the variable without specifying a
|
|
|
|
# path in the command or when writing JSON directly to the `+"`/var/`"+`
|
|
|
|
# HTTP API endpoint
|
2022-08-26 18:03:56 +00:00
|
|
|
# path = "path/to/variable"
|
2022-08-15 17:43:29 +00:00
|
|
|
|
2022-09-09 21:55:20 +00:00
|
|
|
# The Namespace to write the variable can be included in the specification. This
|
|
|
|
# value can be overridden by specifying the "-namespace" flag on the "put"
|
|
|
|
# command.
|
2022-08-15 17:43:29 +00:00
|
|
|
# namespace = "default"
|
|
|
|
|
2022-09-09 21:55:20 +00:00
|
|
|
`+makeHCLComment(msgOnlyItemsRequired)+`
|
2022-08-15 17:43:29 +00:00
|
|
|
|
2022-09-09 21:55:20 +00:00
|
|
|
`+makeHCLComment(msgWarnKeys)+`
|
2022-08-15 17:43:29 +00:00
|
|
|
items {
|
|
|
|
key1 = "value 1"
|
|
|
|
key2 = "value 2"
|
|
|
|
}
|
|
|
|
`) + "\n"
|
|
|
|
|
|
|
|
var defaultJsonVarSpec = strings.TrimSpace(`
|
|
|
|
{
|
2022-09-09 21:55:20 +00:00
|
|
|
"Namespace": "default",
|
|
|
|
"Path": "path/to/variable",
|
2022-08-15 17:43:29 +00:00
|
|
|
"Items": {
|
|
|
|
"key1": "value 1",
|
|
|
|
"key2": "value 2"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`) + "\n"
|
|
|
|
|
2022-09-09 21:55:20 +00:00
|
|
|
// makeHCLComment is a helper function that will take the contents of a raw
|
|
|
|
// string, tidy them, wrap them to 68 characters and add a leading comment
|
|
|
|
// marker plus a space.
|
|
|
|
func makeHCLComment(in string) string {
|
|
|
|
return wrapAndPrepend(tidyRawString(in), 70, "# ")
|
2022-08-15 17:43:29 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 21:55:20 +00:00
|
|
|
// wrapString is a convenience func to abstract away the word wrapping
|
2022-08-15 17:43:29 +00:00
|
|
|
// implementation
|
2022-09-09 21:55:20 +00:00
|
|
|
func wrapString(input string, lineLen int) string {
|
2022-08-15 17:43:29 +00:00
|
|
|
return wordwrap.String(input, lineLen)
|
|
|
|
}
|
|
|
|
|
2022-09-09 21:55:20 +00:00
|
|
|
// wrapAndPrepend will word wrap the input string to lineLen characters and
|
2022-08-15 17:43:29 +00:00
|
|
|
// prepend the provided prefix to every line. The total length of each returned
|
|
|
|
// line will be at most len(input[line])+len(prefix)
|
2022-09-09 21:55:20 +00:00
|
|
|
func wrapAndPrepend(input string, lineLen int, prefix string) string {
|
|
|
|
ss := strings.Split(wrapString(input, lineLen-len(prefix)), "\n")
|
2022-08-15 17:43:29 +00:00
|
|
|
prefixStringList(ss, prefix)
|
|
|
|
return strings.Join(ss, "\n")
|
|
|
|
}
|
|
|
|
|
2022-09-09 21:55:20 +00:00
|
|
|
// tidyRawString will convert a wrapped and indented raw string into a single
|
2022-08-15 17:43:29 +00:00
|
|
|
// long string suitable for rewrapping with another tool. It trims leading and
|
|
|
|
// trailing whitespace and then consume groups of tabs, newlines, and spaces
|
|
|
|
// replacing them with a single space
|
2022-09-09 21:55:20 +00:00
|
|
|
func tidyRawString(raw string) string {
|
2022-08-15 17:43:29 +00:00
|
|
|
re := regexp.MustCompile("[\t\n ]+")
|
|
|
|
return re.ReplaceAllString(strings.TrimSpace(raw), " ")
|
|
|
|
}
|
|
|
|
|
2022-09-09 21:55:20 +00:00
|
|
|
// prefixStringList is a helper function that prepends each item in a slice of
|
|
|
|
// string with a provided prefix.
|
2022-08-15 17:43:29 +00:00
|
|
|
func prefixStringList(ss []string, prefix string) []string {
|
|
|
|
for i, s := range ss {
|
|
|
|
ss[i] = prefix + s
|
|
|
|
}
|
|
|
|
return ss
|
|
|
|
}
|