github.com/hernad/nomad@v1.6.112/command/node_pool_init.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package command
     5  
     6  import (
     7  	"errors"
     8  	"fmt"
     9  	"io/fs"
    10  	"os"
    11  	"strings"
    12  
    13  	"github.com/hernad/nomad/command/asset"
    14  	"github.com/posener/complete"
    15  )
    16  
    17  const (
    18  	// DefaultHclNodePoolInitName is the default name we use when initializing
    19  	// the example node pool spec file in HCL format
    20  	DefaultHclNodePoolInitName = "pool.nomad.hcl"
    21  
    22  	// DefaultJsonNodePoolInitName is the default name we use when initializing
    23  	// the example node pool spec in JSON format
    24  	DefaultJsonNodePoolInitName = "pool.nomad.json"
    25  )
    26  
    27  // NodePoolInitCommand generates a new variable specification
    28  type NodePoolInitCommand struct {
    29  	Meta
    30  }
    31  
    32  func (c *NodePoolInitCommand) Help() string {
    33  	helpText := `
    34  Usage: nomad node pool init <filename>
    35  
    36    Creates an example node pool specification file that can be used as a starting
    37    point to customize further. When no filename is supplied, a default filename
    38    of "pool.nomad.hcl" or "pool.nomad.json" will be used depending on the output
    39    format.
    40  
    41  Init Options:
    42  
    43    -out (hcl | json)
    44      Format of generated node pool specification. Defaults to "hcl".
    45  
    46    -quiet
    47      Do not print success message.
    48  
    49  `
    50  	return strings.TrimSpace(helpText)
    51  }
    52  
    53  func (c *NodePoolInitCommand) Synopsis() string {
    54  	return "Create an example node pool specification file"
    55  }
    56  
    57  func (c *NodePoolInitCommand) AutocompleteFlags() complete.Flags {
    58  	return complete.Flags{
    59  		"-out":   complete.PredictSet("hcl", "json"),
    60  		"-quiet": complete.PredictNothing,
    61  	}
    62  }
    63  
    64  func (c *NodePoolInitCommand) AutocompleteArgs() complete.Predictor {
    65  	return complete.PredictNothing
    66  }
    67  
    68  func (c *NodePoolInitCommand) Name() string { return "node pool init" }
    69  
    70  func (c *NodePoolInitCommand) Run(args []string) int {
    71  	var outFmt string
    72  	var quiet bool
    73  
    74  	flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
    75  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    76  	flags.StringVar(&outFmt, "out", "hcl", "")
    77  	flags.BoolVar(&quiet, "quiet", false, "")
    78  
    79  	if err := flags.Parse(args); err != nil {
    80  		return 1
    81  	}
    82  
    83  	// Check that we get no arguments
    84  	args = flags.Args()
    85  	if l := len(args); l > 1 {
    86  		c.Ui.Error("This command takes no arguments or one: <filename>")
    87  		c.Ui.Error(commandErrorText(c))
    88  		return 1
    89  	}
    90  	var fileName string
    91  	var fileContent []byte
    92  	switch outFmt {
    93  	case "hcl":
    94  		fileName = DefaultHclNodePoolInitName
    95  		fileContent = asset.NodePoolSpec
    96  	case "json":
    97  		fileName = DefaultJsonNodePoolInitName
    98  		fileContent = asset.NodePoolSpecJSON
    99  	}
   100  
   101  	if len(args) == 1 {
   102  		fileName = args[0]
   103  	}
   104  
   105  	// Check if the file already exists
   106  	_, err := os.Stat(fileName)
   107  	if err == nil {
   108  		c.Ui.Error(fmt.Sprintf("File %q already exists", fileName))
   109  		return 1
   110  	}
   111  	if err != nil && !errors.Is(err, fs.ErrNotExist) {
   112  		c.Ui.Error(fmt.Sprintf("Failed to stat %q: %v", fileName, err))
   113  		return 1
   114  	}
   115  
   116  	// Write out the example
   117  	err = os.WriteFile(fileName, fileContent, 0660)
   118  	if err != nil {
   119  		c.Ui.Error(fmt.Sprintf("Failed to write %q: %v", fileName, err))
   120  		return 1
   121  	}
   122  
   123  	// Success
   124  	if !quiet {
   125  		c.Ui.Output(fmt.Sprintf("Example node pool specification written to %s", fileName))
   126  	}
   127  	return 0
   128  }