mirror of https://github.com/hashicorp/terraform
parent
c15f0409ed
commit
0066780049
@ -0,0 +1,48 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package arguments
|
||||
|
||||
import "github.com/hashicorp/terraform/internal/tfdiags"
|
||||
|
||||
// ProvidersMirror represents the command-line arguments for the providers
|
||||
// mirror command.
|
||||
type ProvidersMirror struct {
|
||||
Platforms FlagStringSlice
|
||||
LockFile bool
|
||||
OutputDir string
|
||||
}
|
||||
|
||||
// ParseProvidersMirror processes CLI arguments, returning a ProvidersMirror
|
||||
// value and errors. If errors are encountered, a ProvidersMirror value is
|
||||
// still returned representing the best effort interpretation of the arguments.
|
||||
func ParseProvidersMirror(args []string) (*ProvidersMirror, tfdiags.Diagnostics) {
|
||||
var diags tfdiags.Diagnostics
|
||||
providersMirror := &ProvidersMirror{}
|
||||
|
||||
cmdFlags := defaultFlagSet("providers mirror")
|
||||
cmdFlags.Var(&providersMirror.Platforms, "platform", "target platform")
|
||||
cmdFlags.BoolVar(&providersMirror.LockFile, "lock-file", true, "use lock file")
|
||||
|
||||
if err := cmdFlags.Parse(args); err != nil {
|
||||
diags = diags.Append(tfdiags.Sourceless(
|
||||
tfdiags.Error,
|
||||
"Failed to parse command-line flags",
|
||||
err.Error(),
|
||||
))
|
||||
}
|
||||
|
||||
args = cmdFlags.Args()
|
||||
|
||||
if len(args) != 1 {
|
||||
return providersMirror, diags.Append(tfdiags.Sourceless(
|
||||
tfdiags.Error,
|
||||
"No output directory specified",
|
||||
"The providers mirror command requires an output directory as a command-line argument.",
|
||||
))
|
||||
}
|
||||
|
||||
providersMirror.OutputDir = args[0]
|
||||
|
||||
return providersMirror, diags
|
||||
}
|
||||
@ -0,0 +1,114 @@
|
||||
// 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 TestParseProvidersMirror_valid(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
args []string
|
||||
want *ProvidersMirror
|
||||
}{
|
||||
"defaults": {
|
||||
[]string{"./mirror"},
|
||||
&ProvidersMirror{
|
||||
LockFile: true,
|
||||
OutputDir: "./mirror",
|
||||
},
|
||||
},
|
||||
"all options": {
|
||||
[]string{
|
||||
"-platform=linux_amd64",
|
||||
"-platform=darwin_arm64",
|
||||
"-lock-file=false",
|
||||
"./mirror",
|
||||
},
|
||||
&ProvidersMirror{
|
||||
Platforms: FlagStringSlice{"linux_amd64", "darwin_arm64"},
|
||||
LockFile: false,
|
||||
OutputDir: "./mirror",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
got, diags := ParseProvidersMirror(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 TestParseProvidersMirror_invalid(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
args []string
|
||||
want *ProvidersMirror
|
||||
wantDiags tfdiags.Diagnostics
|
||||
}{
|
||||
"missing output directory": {
|
||||
nil,
|
||||
&ProvidersMirror{
|
||||
LockFile: true,
|
||||
},
|
||||
tfdiags.Diagnostics{
|
||||
tfdiags.Sourceless(
|
||||
tfdiags.Error,
|
||||
"No output directory specified",
|
||||
"The providers mirror command requires an output directory as a command-line argument.",
|
||||
),
|
||||
},
|
||||
},
|
||||
"too many arguments": {
|
||||
[]string{"./mirror", "./extra"},
|
||||
&ProvidersMirror{
|
||||
LockFile: true,
|
||||
},
|
||||
tfdiags.Diagnostics{
|
||||
tfdiags.Sourceless(
|
||||
tfdiags.Error,
|
||||
"No output directory specified",
|
||||
"The providers mirror command requires an output directory as a command-line argument.",
|
||||
),
|
||||
},
|
||||
},
|
||||
"unknown flag and missing output directory": {
|
||||
[]string{"-wat"},
|
||||
&ProvidersMirror{
|
||||
LockFile: true,
|
||||
},
|
||||
tfdiags.Diagnostics{
|
||||
tfdiags.Sourceless(
|
||||
tfdiags.Error,
|
||||
"Failed to parse command-line flags",
|
||||
"flag provided but not defined: -wat",
|
||||
),
|
||||
tfdiags.Sourceless(
|
||||
tfdiags.Error,
|
||||
"No output directory specified",
|
||||
"The providers mirror command requires an output directory as a command-line argument.",
|
||||
),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
got, gotDiags := ParseProvidersMirror(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