driftctl/pkg/remote/github/provider.go

61 lines
1.3 KiB
Go

package github
import (
"os"
"github.com/cloudskiff/driftctl/pkg/remote/terraform"
tf "github.com/cloudskiff/driftctl/pkg/terraform"
)
type GithubTerraformProvider struct {
*terraform.TerraformProvider
}
type githubConfig struct {
Token string
Owner string `cty:"owner"`
Organization string
}
func NewGithubTerraformProvider() (*GithubTerraformProvider, error) {
p := &GithubTerraformProvider{}
providerKey := "github"
installer, err := tf.NewProviderInstaller(tf.ProviderConfig{
Key: providerKey,
Version: "4.4.0",
})
if err != nil {
return nil, err
}
tfProvider, err := terraform.NewTerraformProvider(installer, terraform.TerraformProviderConfig{
Name: providerKey,
DefaultAlias: p.getDefaultOwner(),
GetProviderConfig: func(owner string) interface{} {
return githubConfig{
Owner: p.getDefaultOwner(),
}
},
})
if err != nil {
return nil, err
}
p.TerraformProvider = tfProvider
return p, err
}
func (p GithubTerraformProvider) getDefaultOwner() string {
conf := p.GetConfig()
if conf.Organization != "" {
return conf.Organization
}
return conf.Owner
}
func (p GithubTerraformProvider) GetConfig() githubConfig {
return githubConfig{
Token: os.Getenv("GITHUB_TOKEN"),
Owner: os.Getenv("GITHUB_OWNER"),
Organization: os.Getenv("GITHUB_ORGANIZATION"),
}
}