-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: install multiple providers from YAML file
- Loading branch information
Showing
13 changed files
with
334 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,6 +87,22 @@ You can also specify the architecture and operating system. If not specified, th | |
|
||
<img alt="Install a provider" src="docs/gif/install.gif"/> | ||
|
||
In addition, it's possible to install multiple providers at once specifying a `providers.yml` file, making it easier to share and reuse installation requirements. For example: | ||
|
||
```yaml | ||
providers: | ||
- name: hashicorp/[email protected] | ||
- name: hashicorp/random | ||
os: | ||
- linux | ||
- darwin | ||
arch: | ||
- amd64 | ||
- arm64 | ||
``` | ||
<img alt="Install providers from file" src="docs/gif/install-from-file.gif"/> | ||
### List installed providers | ||
This will display on the screen the installed providers. Optionally, you can specify an output format. Valid output formats are: | ||
|
@@ -119,7 +135,6 @@ This will delete all installed providers from the current registry. | |
- [Terraform plugin caching](https://www.scalefactory.com/blog/2021/02/25/terraform-plugin-caching/) | ||
- [How to Speed Up Terraform in CI/CD Pipelines](https://infinitelambda.com/speed-up-terraform-cicd-pipeline/) | ||
|
||
|
||
## License | ||
|
||
This project is licensed under the [MIT license](LICENSE). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
Output install-from-file.gif | ||
|
||
Set FontSize 20 | ||
Set Width 1200 | ||
Set Height 600 | ||
|
||
Type "tpm install --from-file examples/01-basic.yml" | ||
Sleep 500ms | ||
Enter | ||
Sleep 4s | ||
|
||
Ctrl+L | ||
Type "tpm install -f examples/02-multi-os-arch.yml" | ||
Sleep 500ms | ||
Enter | ||
Sleep 6s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
providers: | ||
- name: hashicorp/[email protected] | ||
- name: hashicorp/null@latest | ||
- name: hashicorp/random |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
providers: | ||
- name: hashicorp/null@latest | ||
os: | ||
- windows | ||
arch: | ||
- amd64 | ||
- name: hashicorp/random | ||
os: | ||
- linux | ||
- darwin | ||
arch: | ||
- amd64 | ||
- arm64 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package parser | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
|
||
"github.com/Madh93/tpm/internal/terraform" | ||
) | ||
|
||
type InputParser interface { | ||
Parse(input []byte) ([]*terraform.Provider, error) | ||
} | ||
|
||
func NewParser(path string) (InputParser, error) { | ||
extension := filepath.Ext(path) | ||
|
||
switch extension { | ||
case ".yml", ".yaml": | ||
return &YAMLParser{}, nil | ||
} | ||
|
||
return nil, fmt.Errorf("unsupported '%s' input format", extension) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package parser_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/Madh93/tpm/internal/parser" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewParser(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
path string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "valid 'yml' output format", | ||
path: "providers.yml", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "valid 'yaml' output format", | ||
path: "whatever.yaml", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "invalid input format", | ||
path: "providers.json", | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
p, err := parser.NewParser(tt.path) | ||
|
||
if tt.wantErr { | ||
assert.Error(t, err) | ||
assert.Nil(t, p) | ||
} else { | ||
assert.NoError(t, err) | ||
assert.NotZero(t, p) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package parser | ||
|
||
import ( | ||
"runtime" | ||
|
||
"github.com/Madh93/tpm/internal/terraform" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
type YAMLProvidersFile struct { | ||
Providers []struct { | ||
Name string `yaml:"name"` | ||
OS []string `yaml:"os"` | ||
Arch []string `yaml:"arch"` | ||
} `yaml:"providers"` | ||
} | ||
|
||
type YAMLParser struct{} | ||
|
||
func (f *YAMLParser) Parse(data []byte) (providers []*terraform.Provider, err error) { | ||
// Decode YAML | ||
var config YAMLProvidersFile | ||
err = yaml.Unmarshal(data, &config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Parse providers | ||
for _, provider := range config.Providers { | ||
osList := getListOrDefault(provider.OS, []string{runtime.GOOS}) | ||
archList := getListOrDefault(provider.Arch, []string{runtime.GOARCH}) | ||
for _, os := range osList { | ||
for _, arch := range archList { | ||
providerName, err := terraform.ParseProviderName(provider.Name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
providers = append(providers, terraform.NewProvider(providerName, os, arch)) | ||
} | ||
} | ||
} | ||
|
||
return providers, nil | ||
} | ||
|
||
func getListOrDefault(list, fallback []string) []string { | ||
if len(list) == 0 { | ||
return fallback | ||
} | ||
return list | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package parser_test | ||
|
||
import ( | ||
"reflect" | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/Madh93/tpm/internal/parser" | ||
"github.com/Madh93/tpm/internal/terraform" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestYAMLParserParse(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
expected []*terraform.Provider | ||
}{ | ||
{ | ||
name: "no providers definition", | ||
input: "", | ||
expected: nil, | ||
}, | ||
{ | ||
name: "one simple provider definition", | ||
input: `providers: | ||
- name: hashicorp/[email protected]`, | ||
expected: []*terraform.Provider{terraform.NewProvider(terraform.NewProviderName("hashicorp", "http", "3.2.1"), runtime.GOOS, runtime.GOARCH)}, | ||
}, | ||
{ | ||
name: "one provider definition with multiple arch and os", | ||
input: `providers: | ||
- name: hashicorp/[email protected] | ||
os: | ||
- linux | ||
- darwin | ||
arch: | ||
- amd64 | ||
- arm64`, | ||
expected: []*terraform.Provider{ | ||
terraform.NewProvider(terraform.NewProviderName("hashicorp", "http", "3.2.1"), "linux", "amd64"), | ||
terraform.NewProvider(terraform.NewProviderName("hashicorp", "http", "3.2.1"), "linux", "arm64"), | ||
terraform.NewProvider(terraform.NewProviderName("hashicorp", "http", "3.2.1"), "darwin", "amd64"), | ||
terraform.NewProvider(terraform.NewProviderName("hashicorp", "http", "3.2.1"), "darwin", "arm64"), | ||
}, | ||
}, | ||
{ | ||
name: "multiple providers definitions", | ||
input: `providers: | ||
- name: cloudflare/[email protected] | ||
- name: digitalocean/[email protected] | ||
- name: hashicorp/[email protected]`, | ||
expected: []*terraform.Provider{ | ||
terraform.NewProvider(terraform.NewProviderName("cloudflare", "cloudflare", "4.4.0"), runtime.GOOS, runtime.GOARCH), | ||
terraform.NewProvider(terraform.NewProviderName("digitalocean", "digitalocean", "2.28.0"), runtime.GOOS, runtime.GOARCH), | ||
terraform.NewProvider(terraform.NewProviderName("hashicorp", "aws", "4.64.0"), runtime.GOOS, runtime.GOARCH), | ||
}, | ||
}, | ||
{ | ||
name: "multiple providers definitions with multiple arch and os", | ||
input: `providers: | ||
- name: cloudflare/[email protected] | ||
os: | ||
- darwin | ||
arch: | ||
- amd64 | ||
- arm64 | ||
- name: digitalocean/[email protected] | ||
os: | ||
- windows | ||
- linux | ||
arch: | ||
- amd64 | ||
- name: hashicorp/[email protected] | ||
os: | ||
- linux | ||
- darwin | ||
arch: | ||
- amd64 | ||
- arm64`, | ||
expected: []*terraform.Provider{ | ||
terraform.NewProvider(terraform.NewProviderName("cloudflare", "cloudflare", "4.4.0"), "darwin", "amd64"), | ||
terraform.NewProvider(terraform.NewProviderName("cloudflare", "cloudflare", "4.4.0"), "darwin", "arm64"), | ||
terraform.NewProvider(terraform.NewProviderName("digitalocean", "digitalocean", "2.28.0"), "windows", "amd64"), | ||
terraform.NewProvider(terraform.NewProviderName("digitalocean", "digitalocean", "2.28.0"), "linux", "amd64"), | ||
terraform.NewProvider(terraform.NewProviderName("hashicorp", "aws", "4.64.0"), "linux", "amd64"), | ||
terraform.NewProvider(terraform.NewProviderName("hashicorp", "aws", "4.64.0"), "linux", "arm64"), | ||
terraform.NewProvider(terraform.NewProviderName("hashicorp", "aws", "4.64.0"), "darwin", "amd64"), | ||
terraform.NewProvider(terraform.NewProviderName("hashicorp", "aws", "4.64.0"), "darwin", "arm64"), | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
yamlParser := &parser.YAMLParser{} | ||
providers, err := yamlParser.Parse([]byte(tt.input)) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, len(tt.expected), len(providers)) | ||
if !reflect.DeepEqual(providers, tt.expected) { | ||
t.Errorf("TestYAMLParserParse(%q): expected provider %v, but got %v", tt.input, tt.expected, providers) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.