2018-07-27 20:59:03 +02:00
|
|
|
package regsrc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
2018-11-28 17:26:16 +01:00
|
|
|
"strings"
|
2018-07-27 20:59:03 +02:00
|
|
|
|
2019-10-11 11:34:26 +02:00
|
|
|
"github.com/hashicorp/terraform-svchost"
|
2018-07-27 20:59:03 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// DefaultProviderNamespace represents the namespace for canonical
|
|
|
|
// HashiCorp-controlled providers.
|
2018-11-28 17:26:16 +01:00
|
|
|
DefaultProviderNamespace = "-"
|
2018-07-27 20:59:03 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// TerraformProvider describes a Terraform Registry Provider source.
|
|
|
|
type TerraformProvider struct {
|
|
|
|
RawHost *FriendlyHost
|
|
|
|
RawNamespace string
|
|
|
|
RawName string
|
|
|
|
OS string
|
|
|
|
Arch string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTerraformProvider constructs a new provider source.
|
2018-08-02 17:45:55 +02:00
|
|
|
func NewTerraformProvider(name, os, arch string) *TerraformProvider {
|
2018-07-27 20:59:03 +02:00
|
|
|
if os == "" {
|
|
|
|
os = runtime.GOOS
|
|
|
|
}
|
|
|
|
if arch == "" {
|
|
|
|
arch = runtime.GOARCH
|
|
|
|
}
|
|
|
|
|
2018-11-28 17:26:16 +01:00
|
|
|
// separate namespace if included
|
|
|
|
namespace := DefaultProviderNamespace
|
|
|
|
if names := strings.SplitN(name, "/", 2); len(names) == 2 {
|
|
|
|
namespace, name = names[0], names[1]
|
|
|
|
}
|
2018-07-27 20:59:03 +02:00
|
|
|
p := &TerraformProvider{
|
|
|
|
RawHost: PublicRegistryHost,
|
2018-11-28 17:26:16 +01:00
|
|
|
RawNamespace: namespace,
|
2018-07-27 20:59:03 +02:00
|
|
|
RawName: name,
|
|
|
|
OS: os,
|
|
|
|
Arch: arch,
|
|
|
|
}
|
|
|
|
|
2018-08-02 17:45:55 +02:00
|
|
|
return p
|
2018-07-27 20:59:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Provider returns just the registry ID of the provider
|
|
|
|
func (p *TerraformProvider) TerraformProvider() string {
|
|
|
|
return fmt.Sprintf("%s/%s", p.RawNamespace, p.RawName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SvcHost returns the svchost.Hostname for this provider. The
|
|
|
|
// default PublicRegistryHost is returned.
|
|
|
|
func (p *TerraformProvider) SvcHost() (svchost.Hostname, error) {
|
|
|
|
return svchost.ForComparison(PublicRegistryHost.Raw)
|
|
|
|
}
|