2016-03-22 12:41:02 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
// helpFunc is a cli.HelpFunc that can is used to output the help for Terraform.
|
|
|
|
func helpFunc(commands map[string]cli.CommandFactory) string {
|
|
|
|
// Determine the maximum key length, and classify based on type
|
2020-10-23 18:13:41 -05:00
|
|
|
var otherCommands []string
|
2016-03-22 12:41:02 -05:00
|
|
|
maxKeyLen := 0
|
2020-10-23 18:13:41 -05:00
|
|
|
|
|
|
|
for key := range commands {
|
2020-10-23 17:48:46 -05:00
|
|
|
if _, ok := HiddenCommands[key]; ok {
|
|
|
|
// We don't consider hidden commands when deciding the
|
|
|
|
// maximum command length.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-03-22 12:41:02 -05:00
|
|
|
if len(key) > maxKeyLen {
|
|
|
|
maxKeyLen = len(key)
|
|
|
|
}
|
|
|
|
|
2020-10-23 18:13:41 -05:00
|
|
|
isOther := true
|
|
|
|
for _, candidate := range PrimaryCommands {
|
|
|
|
if candidate == key {
|
|
|
|
isOther = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if isOther {
|
|
|
|
otherCommands = append(otherCommands, key)
|
2016-03-22 12:41:02 -05:00
|
|
|
}
|
|
|
|
}
|
2020-10-23 18:13:41 -05:00
|
|
|
sort.Strings(otherCommands)
|
2016-03-22 12:41:02 -05:00
|
|
|
|
2016-11-24 11:22:18 -06:00
|
|
|
// The output produced by this is included in the docs at
|
|
|
|
// website/source/docs/commands/index.html.markdown; if you
|
|
|
|
// change this then consider updating that to match.
|
2016-10-10 13:47:35 -05:00
|
|
|
helpText := fmt.Sprintf(`
|
main: new global option -chdir
This new option is intended to address the previous inconsistencies where
some older subcommands supported partially changing the target directory
(where Terraform would use the new directory inconsistently) where newer
commands did not support that override at all.
Instead, now Terraform will accept a -chdir command at the start of the
command line (before the subcommand) and will interpret it as a request
to direct all actions that would normally be taken in the current working
directory into the target directory instead. This is similar to options
offered by some other similar tools, such as the -C option in "make".
The new option is only accepted at the start of the command line (before
the subcommand) as a way to reflect that it is a global command (not
specific to a particular subcommand) and that it takes effect _before_
executing the subcommand. This also means it'll be forced to appear before
any other command-specific arguments that take file paths, which hopefully
communicates that those other arguments are interpreted relative to the
overridden path.
As a measure of pragmatism for existing uses, the path.cwd object in
the Terraform language will continue to return the _original_ working
directory (ignoring -chdir), in case that is important in some exceptional
workflows. The path.root object gives the root module directory, which
will always match the overriden working directory unless the user
simultaneously uses one of the legacy directory override arguments, which
is not a pattern we intend to support in the long run.
As a first step down the deprecation path, this commit adjusts the
documentation to de-emphasize the inconsistent old command line arguments,
including specific guidance on what to use instead for the main three
workflow commands, but all of those options remain supported in the same
way as they were before. In a later commit we'll make those arguments
produce a visible deprecation warning in Terraform's output, and then
in an even later commit we'll remove them entirely so that -chdir is the
single supported way to run Terraform from a directory other than the
one containing the root module configuration.
2020-09-01 17:45:12 -05:00
|
|
|
Usage: terraform [global options] <subcommand> [args]
|
2016-10-10 13:47:35 -05:00
|
|
|
|
|
|
|
The available commands for execution are listed below.
|
2020-10-23 18:13:41 -05:00
|
|
|
The primary workflow commands are given first, followed by
|
|
|
|
less common or more advanced commands.
|
2016-10-10 13:47:35 -05:00
|
|
|
|
2020-10-23 18:13:41 -05:00
|
|
|
Main commands:
|
2016-10-10 13:47:35 -05:00
|
|
|
%s
|
|
|
|
All other commands:
|
|
|
|
%s
|
main: new global option -chdir
This new option is intended to address the previous inconsistencies where
some older subcommands supported partially changing the target directory
(where Terraform would use the new directory inconsistently) where newer
commands did not support that override at all.
Instead, now Terraform will accept a -chdir command at the start of the
command line (before the subcommand) and will interpret it as a request
to direct all actions that would normally be taken in the current working
directory into the target directory instead. This is similar to options
offered by some other similar tools, such as the -C option in "make".
The new option is only accepted at the start of the command line (before
the subcommand) as a way to reflect that it is a global command (not
specific to a particular subcommand) and that it takes effect _before_
executing the subcommand. This also means it'll be forced to appear before
any other command-specific arguments that take file paths, which hopefully
communicates that those other arguments are interpreted relative to the
overridden path.
As a measure of pragmatism for existing uses, the path.cwd object in
the Terraform language will continue to return the _original_ working
directory (ignoring -chdir), in case that is important in some exceptional
workflows. The path.root object gives the root module directory, which
will always match the overriden working directory unless the user
simultaneously uses one of the legacy directory override arguments, which
is not a pattern we intend to support in the long run.
As a first step down the deprecation path, this commit adjusts the
documentation to de-emphasize the inconsistent old command line arguments,
including specific guidance on what to use instead for the main three
workflow commands, but all of those options remain supported in the same
way as they were before. In a later commit we'll make those arguments
produce a visible deprecation warning in Terraform's output, and then
in an even later commit we'll remove them entirely so that -chdir is the
single supported way to run Terraform from a directory other than the
one containing the root module configuration.
2020-09-01 17:45:12 -05:00
|
|
|
Global options (use these before the subcommand, if any):
|
2020-10-23 18:13:41 -05:00
|
|
|
-chdir=DIR Switch to a different working directory before executing
|
|
|
|
the given subcommand.
|
|
|
|
-help Show this help output, or the help for a specified
|
|
|
|
subcommand.
|
|
|
|
-version An alias for the "version" subcommand.
|
|
|
|
`, listCommands(commands, PrimaryCommands, maxKeyLen), listCommands(commands, otherCommands, maxKeyLen))
|
2016-10-10 13:47:35 -05:00
|
|
|
|
|
|
|
return strings.TrimSpace(helpText)
|
2016-03-22 12:41:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// listCommands just lists the commands in the map with the
|
|
|
|
// given maximum key length.
|
2020-10-23 18:13:41 -05:00
|
|
|
func listCommands(allCommands map[string]cli.CommandFactory, order []string, maxKeyLen int) string {
|
2016-03-22 12:41:02 -05:00
|
|
|
var buf bytes.Buffer
|
|
|
|
|
2020-10-23 18:13:41 -05:00
|
|
|
for _, key := range order {
|
|
|
|
commandFunc, ok := allCommands[key]
|
2016-03-22 12:41:02 -05:00
|
|
|
if !ok {
|
2020-10-23 18:13:41 -05:00
|
|
|
// This suggests an inconsistency in the command table definitions
|
|
|
|
// in commands.go .
|
2016-03-22 12:41:02 -05:00
|
|
|
panic("command not found: " + key)
|
|
|
|
}
|
|
|
|
|
|
|
|
command, err := commandFunc()
|
|
|
|
if err != nil {
|
2020-10-23 18:13:41 -05:00
|
|
|
// This would be really weird since there's no good reason for
|
|
|
|
// any of our command factories to fail.
|
2016-03-22 12:41:02 -05:00
|
|
|
log.Printf("[ERR] cli: Command '%s' failed to load: %s",
|
|
|
|
key, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
key = fmt.Sprintf("%s%s", key, strings.Repeat(" ", maxKeyLen-len(key)))
|
|
|
|
buf.WriteString(fmt.Sprintf(" %s %s\n", key, command.Synopsis()))
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String()
|
|
|
|
}
|