2016-03-22 18:41:02 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
2022-01-24 11:52:37 +01:00
|
|
|
// helpFunc is a cli.HelpFunc that can be used to output the help CLI instructions for Terraform.
|
2016-03-22 18:41:02 +01:00
|
|
|
func helpFunc(commands map[string]cli.CommandFactory) string {
|
|
|
|
// Determine the maximum key length, and classify based on type
|
2020-10-24 01:13:41 +02:00
|
|
|
var otherCommands []string
|
2016-03-22 18:41:02 +01:00
|
|
|
maxKeyLen := 0
|
2020-10-24 01:13:41 +02:00
|
|
|
|
|
|
|
for key := range commands {
|
2020-10-24 00:48:46 +02:00
|
|
|
if _, ok := HiddenCommands[key]; ok {
|
|
|
|
// We don't consider hidden commands when deciding the
|
|
|
|
// maximum command length.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-03-22 18:41:02 +01:00
|
|
|
if len(key) > maxKeyLen {
|
|
|
|
maxKeyLen = len(key)
|
|
|
|
}
|
|
|
|
|
2020-10-24 01:13:41 +02:00
|
|
|
isOther := true
|
|
|
|
for _, candidate := range PrimaryCommands {
|
|
|
|
if candidate == key {
|
|
|
|
isOther = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if isOther {
|
|
|
|
otherCommands = append(otherCommands, key)
|
2016-03-22 18:41:02 +01:00
|
|
|
}
|
|
|
|
}
|
2020-10-24 01:13:41 +02:00
|
|
|
sort.Strings(otherCommands)
|
2016-03-22 18:41:02 +01:00
|
|
|
|
2016-11-24 18:22:18 +01:00
|
|
|
// The output produced by this is included in the docs at
|
2021-01-19 22:53:40 +01:00
|
|
|
// website/source/docs/cli/commands/index.html.markdown; if you
|
2016-11-24 18:22:18 +01:00
|
|
|
// change this then consider updating that to match.
|
2016-10-10 20:47:35 +02: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-02 00:45:12 +02:00
|
|
|
Usage: terraform [global options] <subcommand> [args]
|
2016-10-10 20:47:35 +02:00
|
|
|
|
|
|
|
The available commands for execution are listed below.
|
2020-10-24 01:13:41 +02:00
|
|
|
The primary workflow commands are given first, followed by
|
|
|
|
less common or more advanced commands.
|
2016-10-10 20:47:35 +02:00
|
|
|
|
2020-10-24 01:13:41 +02:00
|
|
|
Main commands:
|
2016-10-10 20:47:35 +02: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-02 00:45:12 +02:00
|
|
|
Global options (use these before the subcommand, if any):
|
2020-10-24 01:59:29 +02: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.
|
2020-10-24 01:13:41 +02:00
|
|
|
`, listCommands(commands, PrimaryCommands, maxKeyLen), listCommands(commands, otherCommands, maxKeyLen))
|
2016-10-10 20:47:35 +02:00
|
|
|
|
|
|
|
return strings.TrimSpace(helpText)
|
2016-03-22 18:41:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// listCommands just lists the commands in the map with the
|
|
|
|
// given maximum key length.
|
2020-10-24 01:13:41 +02:00
|
|
|
func listCommands(allCommands map[string]cli.CommandFactory, order []string, maxKeyLen int) string {
|
2016-03-22 18:41:02 +01:00
|
|
|
var buf bytes.Buffer
|
|
|
|
|
2020-10-24 01:13:41 +02:00
|
|
|
for _, key := range order {
|
|
|
|
commandFunc, ok := allCommands[key]
|
2016-03-22 18:41:02 +01:00
|
|
|
if !ok {
|
2020-10-24 01:13:41 +02:00
|
|
|
// This suggests an inconsistency in the command table definitions
|
|
|
|
// in commands.go .
|
2016-03-22 18:41:02 +01:00
|
|
|
panic("command not found: " + key)
|
|
|
|
}
|
|
|
|
|
|
|
|
command, err := commandFunc()
|
|
|
|
if err != nil {
|
2020-10-24 01:13:41 +02:00
|
|
|
// This would be really weird since there's no good reason for
|
|
|
|
// any of our command factories to fail.
|
2016-03-22 18:41:02 +01: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)))
|
2020-10-24 01:59:29 +02:00
|
|
|
buf.WriteString(fmt.Sprintf(" %s %s\n", key, command.Synopsis()))
|
2016-03-22 18:41:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String()
|
|
|
|
}
|