Files
opentf/internal/command/get.go
Kuba Martin 3b5b8ba719 Update internal/command to use OpenTF in user-provided strings. (#87)
* Update `internal/command` to use OpenTF

Signed-off-by: Jakub Martin <kubam@spacelift.io>

* Fix e2e tests.

Signed-off-by: Jakub Martin <kubam@spacelift.io>

* Fix e2e tests.

Signed-off-by: Jakub Martin <kubam@spacelift.io>

* Fix

Signed-off-by: Jakub Martin <kubam@spacelift.io>

* Rewrite message.

Signed-off-by: Jakub Martin <kubam@spacelift.io>

---------

Signed-off-by: Jakub Martin <kubam@spacelift.io>
2023-08-24 10:56:05 +02:00

95 lines
2.5 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package command
import (
"context"
"fmt"
"strings"
"github.com/placeholderplaceholderplaceholder/opentf/internal/tfdiags"
)
// GetCommand is a Command implementation that takes a Terraform
// configuration and downloads all the modules.
type GetCommand struct {
Meta
}
func (c *GetCommand) Run(args []string) int {
var update bool
var testsDirectory string
args = c.Meta.process(args)
cmdFlags := c.Meta.defaultFlagSet("get")
cmdFlags.BoolVar(&update, "update", false, "update")
cmdFlags.StringVar(&testsDirectory, "test-directory", "tests", "test-directory")
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
return 1
}
// Initialization can be aborted by interruption signals
ctx, done := c.InterruptibleContext(c.CommandContext())
defer done()
path, err := ModulePath(cmdFlags.Args())
if err != nil {
c.Ui.Error(err.Error())
return 1
}
path = c.normalizePath(path)
abort, diags := getModules(ctx, &c.Meta, path, testsDirectory, update)
c.showDiagnostics(diags)
if abort || diags.HasErrors() {
return 1
}
return 0
}
func (c *GetCommand) Help() string {
helpText := `
Usage: opentf [global options] get [options]
Downloads and installs modules needed for the configuration in the
current working directory.
This recursively downloads all modules needed, such as modules
imported by modules imported by the root and so on. If a module is
already downloaded, it will not be redownloaded or checked for updates
unless the -update flag is specified.
Module installation also happens automatically by default as part of
the "opentf init" command, so you should rarely need to run this
command separately.
Options:
-update Check already-downloaded modules for available updates
and install the newest versions available.
-no-color Disable text coloring in the output.
-test-directory=path Set the OpenTF test directory, defaults to "tests".
`
return strings.TrimSpace(helpText)
}
func (c *GetCommand) Synopsis() string {
return "Install or upgrade remote OpenTF modules"
}
func getModules(ctx context.Context, m *Meta, path string, testsDir string, upgrade bool) (abort bool, diags tfdiags.Diagnostics) {
hooks := uiModuleInstallHooks{
Ui: m.Ui,
ShowLocalPaths: true,
}
return m.installModules(ctx, path, testsDir, upgrade, true, hooks)
}