mirror of https://github.com/hashicorp/terraform
parent
c7729926fa
commit
af7783eb62
@ -0,0 +1,52 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package arguments
|
||||
|
||||
import "github.com/hashicorp/terraform/internal/tfdiags"
|
||||
|
||||
// ProvidersLock represents the command-line arguments for the providers lock
|
||||
// command.
|
||||
type ProvidersLock struct {
|
||||
Platforms FlagStringSlice
|
||||
FSMirrorDir string
|
||||
NetMirrorURL string
|
||||
TestsDirectory string
|
||||
EnablePluginCache bool
|
||||
Providers []string
|
||||
}
|
||||
|
||||
// ParseProvidersLock processes CLI arguments, returning a ProvidersLock value
|
||||
// and errors. If errors are encountered, a ProvidersLock value is still
|
||||
// returned representing the best effort interpretation of the arguments.
|
||||
func ParseProvidersLock(args []string) (*ProvidersLock, tfdiags.Diagnostics) {
|
||||
var diags tfdiags.Diagnostics
|
||||
providersLock := &ProvidersLock{}
|
||||
|
||||
cmdFlags := defaultFlagSet("providers lock")
|
||||
cmdFlags.Var(&providersLock.Platforms, "platform", "target platform")
|
||||
cmdFlags.StringVar(&providersLock.FSMirrorDir, "fs-mirror", "", "filesystem mirror directory")
|
||||
cmdFlags.StringVar(&providersLock.NetMirrorURL, "net-mirror", "", "network mirror base URL")
|
||||
cmdFlags.StringVar(&providersLock.TestsDirectory, "test-directory", "tests", "test-directory")
|
||||
cmdFlags.BoolVar(&providersLock.EnablePluginCache, "enable-plugin-cache", false, "")
|
||||
|
||||
if err := cmdFlags.Parse(args); err != nil {
|
||||
diags = diags.Append(tfdiags.Sourceless(
|
||||
tfdiags.Error,
|
||||
"Failed to parse command-line flags",
|
||||
err.Error(),
|
||||
))
|
||||
}
|
||||
|
||||
if providersLock.FSMirrorDir != "" && providersLock.NetMirrorURL != "" {
|
||||
diags = diags.Append(tfdiags.Sourceless(
|
||||
tfdiags.Error,
|
||||
"Invalid installation method options",
|
||||
"The -fs-mirror and -net-mirror command line options are mutually-exclusive.",
|
||||
))
|
||||
}
|
||||
|
||||
providersLock.Providers = cmdFlags.Args()
|
||||
|
||||
return providersLock, diags
|
||||
}
|
||||
@ -0,0 +1,124 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package arguments
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/hashicorp/terraform/internal/tfdiags"
|
||||
)
|
||||
|
||||
func TestParseProvidersLock_valid(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
args []string
|
||||
want *ProvidersLock
|
||||
}{
|
||||
"defaults": {
|
||||
nil,
|
||||
&ProvidersLock{
|
||||
TestsDirectory: "tests",
|
||||
},
|
||||
},
|
||||
"all options": {
|
||||
[]string{
|
||||
"-platform=linux_amd64",
|
||||
"-platform=darwin_arm64",
|
||||
"-fs-mirror=mirror",
|
||||
"-test-directory=integration-tests",
|
||||
"-enable-plugin-cache",
|
||||
"hashicorp/test",
|
||||
},
|
||||
&ProvidersLock{
|
||||
Platforms: FlagStringSlice{"linux_amd64", "darwin_arm64"},
|
||||
FSMirrorDir: "mirror",
|
||||
TestsDirectory: "integration-tests",
|
||||
EnablePluginCache: true,
|
||||
Providers: []string{"hashicorp/test"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
got, diags := ParseProvidersLock(tc.args)
|
||||
if len(diags) > 0 {
|
||||
t.Fatalf("unexpected diags: %v", diags)
|
||||
}
|
||||
if diff := cmp.Diff(tc.want, got); diff != "" {
|
||||
t.Fatalf("unexpected result\n%s", diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseProvidersLock_invalid(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
args []string
|
||||
want *ProvidersLock
|
||||
wantDiags tfdiags.Diagnostics
|
||||
}{
|
||||
"mirror collision": {
|
||||
[]string{
|
||||
"-fs-mirror=foo",
|
||||
"-net-mirror=https://example.com",
|
||||
},
|
||||
&ProvidersLock{
|
||||
FSMirrorDir: "foo",
|
||||
NetMirrorURL: "https://example.com",
|
||||
TestsDirectory: "tests",
|
||||
Providers: []string{},
|
||||
},
|
||||
tfdiags.Diagnostics{
|
||||
tfdiags.Sourceless(
|
||||
tfdiags.Error,
|
||||
"Invalid installation method options",
|
||||
"The -fs-mirror and -net-mirror command line options are mutually-exclusive.",
|
||||
),
|
||||
},
|
||||
},
|
||||
"unknown flag": {
|
||||
[]string{"-wat"},
|
||||
&ProvidersLock{
|
||||
TestsDirectory: "tests",
|
||||
Providers: []string{},
|
||||
},
|
||||
tfdiags.Diagnostics{
|
||||
tfdiags.Sourceless(
|
||||
tfdiags.Error,
|
||||
"Failed to parse command-line flags",
|
||||
"flag provided but not defined: -wat",
|
||||
),
|
||||
},
|
||||
},
|
||||
"unknown flag and mirror collision": {
|
||||
[]string{
|
||||
"-wat",
|
||||
"-fs-mirror=foo",
|
||||
"-net-mirror=https://example.com",
|
||||
},
|
||||
&ProvidersLock{
|
||||
TestsDirectory: "tests",
|
||||
Providers: []string{"-fs-mirror=foo", "-net-mirror=https://example.com"},
|
||||
},
|
||||
tfdiags.Diagnostics{
|
||||
tfdiags.Sourceless(
|
||||
tfdiags.Error,
|
||||
"Failed to parse command-line flags",
|
||||
"flag provided but not defined: -wat",
|
||||
),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
got, gotDiags := ParseProvidersLock(tc.args)
|
||||
if diff := cmp.Diff(tc.want, got); diff != "" {
|
||||
t.Fatalf("unexpected result\n%s", diff)
|
||||
}
|
||||
tfdiags.AssertDiagnosticsMatch(t, gotDiags, tc.wantDiags)
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue