github.com/jrxfive/nomad@v0.6.1-0.20170802162750-1fef470e89bf/main.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"os"
     7  	"sort"
     8  	"strings"
     9  
    10  	"github.com/mitchellh/cli"
    11  	"github.com/sean-/seed"
    12  )
    13  
    14  func init() {
    15  	seed.Init()
    16  }
    17  
    18  func main() {
    19  	os.Exit(Run(os.Args[1:]))
    20  }
    21  
    22  func Run(args []string) int {
    23  	return RunCustom(args, Commands(nil))
    24  }
    25  
    26  func RunCustom(args []string, commands map[string]cli.CommandFactory) int {
    27  	// Build the commands to include in the help now.
    28  	commandsInclude := make([]string, 0, len(commands))
    29  	for k, _ := range commands {
    30  		switch k {
    31  		case "check":
    32  		case "deployment list", "deployment status", "deployment pause",
    33  			"deployment resume", "deployment fail", "deployment promote":
    34  		case "executor":
    35  		case "fs ls", "fs cat", "fs stat":
    36  		case "job deployments", "job dispatch", "job history", "job promote", "job revert":
    37  		case "operator raft", "operator raft list-peers", "operator raft remove-peer":
    38  		case "syslog":
    39  		default:
    40  			commandsInclude = append(commandsInclude, k)
    41  		}
    42  	}
    43  
    44  	cli := &cli.CLI{
    45  		Name:         "nomad",
    46  		Version:      PrettyVersion(GetVersionParts()),
    47  		Args:         args,
    48  		Commands:     commands,
    49  		Autocomplete: true,
    50  		HelpFunc:     cli.FilteredHelpFunc(commandsInclude, helpFunc),
    51  	}
    52  
    53  	exitCode, err := cli.Run()
    54  	if err != nil {
    55  		fmt.Fprintf(os.Stderr, "Error executing CLI: %s\n", err.Error())
    56  		return 1
    57  	}
    58  
    59  	return exitCode
    60  }
    61  
    62  // helpFunc is a custom help function. At the moment it is essentially a copy of
    63  // the cli.BasicHelpFunc that includes flags demonstrating how to use the
    64  // autocomplete flags.
    65  func helpFunc(commands map[string]cli.CommandFactory) string {
    66  	var buf bytes.Buffer
    67  	buf.WriteString("Usage: nomad [-version] [-help] [-autocomplete-(un)install] <command> [<args>]\n\n")
    68  	buf.WriteString("Available commands are:\n")
    69  
    70  	// Get the list of keys so we can sort them, and also get the maximum
    71  	// key length so they can be aligned properly.
    72  	keys := make([]string, 0, len(commands))
    73  	maxKeyLen := 0
    74  	for key := range commands {
    75  		if len(key) > maxKeyLen {
    76  			maxKeyLen = len(key)
    77  		}
    78  
    79  		keys = append(keys, key)
    80  	}
    81  	sort.Strings(keys)
    82  
    83  	for _, key := range keys {
    84  		commandFunc, ok := commands[key]
    85  		if !ok {
    86  			// This should never happen since we JUST built the list of
    87  			// keys.
    88  			panic("command not found: " + key)
    89  		}
    90  
    91  		command, err := commandFunc()
    92  		if err != nil {
    93  			fmt.Fprintf(os.Stderr, "[ERR] cli: Command '%s' failed to load: %s", key, err)
    94  			continue
    95  		}
    96  
    97  		key = fmt.Sprintf("%s%s", key, strings.Repeat(" ", maxKeyLen-len(key)))
    98  		buf.WriteString(fmt.Sprintf("    %s    %s\n", key, command.Synopsis()))
    99  	}
   100  
   101  	return buf.String()
   102  }